diff options
Initial Commit - Add all project files
5 files changed, 79 insertions, 0 deletions
diff --git a/src/Common/AutoMapperConfiguration/AutoMapperConfig.cs b/src/Common/AutoMapperConfiguration/AutoMapperConfig.cs new file mode 100644 index 0000000..32e9564 --- /dev/null +++ b/src/Common/AutoMapperConfiguration/AutoMapperConfig.cs @@ -0,0 +1,46 @@ +using AutoMapper; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AutoMapperConfiguration +{ + public class AutoMapperConfig + { + public static IMapper Mapper; + + public static void RegisterMappings(params Type[][] typeCollections) + { + MapperConfiguration mapperConfiguration = new MapperConfiguration(mapperConfig => + { + foreach (var typeCollection in typeCollections) + { + foreach (var type in typeCollection) + { + List<Type> mapFromTypes = type.GetInterfaces() + .Where(i => i.Name.Contains("IMapFrom")) + .Select(i => i.GetGenericArguments()[0]) + .ToList(); + + foreach (var fromType in mapFromTypes) + { + mapperConfig.CreateMap(fromType, type); + } + + List<Type> mapToTypes = type.GetInterfaces() + .Where(i => i.Name.Contains("IMapTo")) + .Select(i => i.GetGenericArguments()[0]) + .ToList(); + + foreach (var toType in mapToTypes) + { + mapperConfig.CreateMap(type, toType); + } + } + } + }); + + Mapper = mapperConfiguration.CreateMapper(); + } + } +} diff --git a/src/Common/AutoMapperConfiguration/AutoMapperConfiguration.csproj b/src/Common/AutoMapperConfiguration/AutoMapperConfiguration.csproj new file mode 100644 index 0000000..f41290e --- /dev/null +++ b/src/Common/AutoMapperConfiguration/AutoMapperConfiguration.csproj @@ -0,0 +1,11 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>netcoreapp3.1</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="AutoMapper" Version="10.1.1" /> + </ItemGroup> + +</Project> diff --git a/src/Common/AutoMapperConfiguration/AutoMapperExtensions.cs b/src/Common/AutoMapperConfiguration/AutoMapperExtensions.cs new file mode 100644 index 0000000..a17e1bb --- /dev/null +++ b/src/Common/AutoMapperConfiguration/AutoMapperExtensions.cs @@ -0,0 +1,10 @@ +namespace AutoMapperConfiguration +{ + public static class AutoMapperExtensions + { + public static T To<T>(this object source) + { + return AutoMapperConfig.Mapper.Map<T>(source); + } + } +} diff --git a/src/Common/AutoMapperConfiguration/IMapFrom.cs b/src/Common/AutoMapperConfiguration/IMapFrom.cs new file mode 100644 index 0000000..e8bc1a2 --- /dev/null +++ b/src/Common/AutoMapperConfiguration/IMapFrom.cs @@ -0,0 +1,6 @@ +namespace AutoMapperConfiguration +{ + public interface IMapFrom<T> + { + } +} diff --git a/src/Common/AutoMapperConfiguration/IMapTo.cs b/src/Common/AutoMapperConfiguration/IMapTo.cs new file mode 100644 index 0000000..514d046 --- /dev/null +++ b/src/Common/AutoMapperConfiguration/IMapTo.cs @@ -0,0 +1,6 @@ +namespace AutoMapperConfiguration +{ + public interface IMapTo<T> + { + } +} |