From 77b7ff6ca840866605204e5d501fe23dd184bd5c Mon Sep 17 00:00:00 2001 From: "ivaylo.zhelev" Date: Sun, 24 Jan 2021 09:25:15 +0200 Subject: Initial Commit - Add all project files --- .../AutoMapperConfiguration/AutoMapperConfig.cs | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Common/AutoMapperConfiguration/AutoMapperConfig.cs (limited to 'src/Common/AutoMapperConfiguration/AutoMapperConfig.cs') 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 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 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(); + } + } +} -- cgit v1.3.1-26-gb76a