-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68ba0c9
commit 0a50507
Showing
8 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters