Skip to content

Commit

Permalink
Added AutoMapper example
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Mar 18, 2023
1 parent 68ba0c9 commit 0a50507
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 0 deletions.
10 changes: 10 additions & 0 deletions AutoMapper/AutoMapper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
</PropertyGroup>

</Project>
16 changes: 16 additions & 0 deletions AutoMapper/AutoMapper.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoMapper", "AutoMapper.csproj", "{A3FA8FA1-6587-44BB-A0D9-3F134FE1E773}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A3FA8FA1-6587-44BB-A0D9-3F134FE1E773}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3FA8FA1-6587-44BB-A0D9-3F134FE1E773}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3FA8FA1-6587-44BB-A0D9-3F134FE1E773}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3FA8FA1-6587-44BB-A0D9-3F134FE1E773}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
8 changes: 8 additions & 0 deletions AutoMapper/BlogPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AutoMapper;

public class BlogPost
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public DateOnly PublishedDate { get; set; }
}
8 changes: 8 additions & 0 deletions AutoMapper/BlogPostDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AutoMapper;

public class BlogPostDto
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public DateOnly PublishedDate { get; set; }
}
26 changes: 26 additions & 0 deletions AutoMapper/Mapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace AutoMapper;

public static class Mapper
{
public static TResult Map<TIn, TResult>(TIn obj) where TResult : new()
{
var result = new TResult();

var inputProperties = typeof(TIn).GetProperties();
var resultProperties = typeof(TResult).GetProperties();

foreach (var inputProperty in inputProperties)
{
// Find the property that has the same name and type
var resultProperty = resultProperties.FirstOrDefault(prop => prop.Name == inputProperty.Name && prop.PropertyType == inputProperty.PropertyType);

// If it isn't writeable, don't try to write the value
if (resultProperty != null && resultProperty.CanWrite)
{
resultProperty.SetValue(result, inputProperty.GetValue(obj));
}
}

return result;
}
}
6 changes: 6 additions & 0 deletions AutoMapper/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Threading.Channels;
using AutoMapper;

var blogPost = new BlogPost { Id = 1, Title = "Write your own AutoMapper in C#", PublishedDate = new DateOnly(2023, 3, 18) };
var dto = Mapper.Map<BlogPost, BlogPostDto>(blogPost);
Console.Write($"Blog Post: '{dto.Title}' was published at: {dto.PublishedDate}");
9 changes: 9 additions & 0 deletions AutoMapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Write your own AutoMapper in C#

Sometimes you have to map an object to another representation in C#. And you think: Why isn't C# duck-typing capable?

You might hear of libraries like [**AutoMapper**](https://automapper.org/) that do the tedious work of mapping one object to another with the same structure. This blog post will give a super simple introduction to how those libraries are working internally.

In the end a bit of a subjective topic on whether or not I would use such libraries.

Found [here](https://steven-giesel.com/blogPost/16bf84c2-1464-40ab-8ef6-58f225e21c15)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv

| BlogPost | Publish Date |
| -------------------------------------------------------------------------------------------------------------- | ------------ |
| [Write your own AutoMapper in C#](AutoMapper/) | 18.03.2023 |
| [How to write your own cron Job scheduler in ASP.NET Core (like Quartz, Hangfire, ...)](CronBackgroundWorker/) | 09.03.2023 |
| [Performance (ReadOnly)List vs Immutable collection types](ImmutablePerformance/) | 26.02.2023 |
| [Multi-Tenancy with RavenDB and ASP.NET Core](MultiTenantRavenDB/) | 05.02.2023 |
Expand Down

0 comments on commit 0a50507

Please sign in to comment.