Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Common / AutoMapperConfiguration
diff options
context:
space:
mode:
Commit77b7ff6ca840866605204e5d501fe23dd184bd5c (patch)
Author
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-24 09:25:15 +0200
Committer
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-24 09:25:15 +0200
Filesb2c8fac505ad91fa829256cd70c5c63e61739de8 /src/Common/AutoMapperConfiguration
pc-craft-77b7ff6ca840866605204e5d501fe23dd184bd5c.tar
pc-craft-77b7ff6ca840866605204e5d501fe23dd184bd5c.tar.gz
pc-craft-77b7ff6ca840866605204e5d501fe23dd184bd5c.zip
Parenta7270c59ccc3a9bdfd0101c00e763ad910057a6f (diff)
Initial Commit - Add all project files
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>
+ {
+ }
+}