forked from yuzd/AntMgr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoMapper.cs
More file actions
161 lines (143 loc) · 5.55 KB
/
Copy pathAutoMapper.cs
File metadata and controls
161 lines (143 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace Mapping
{
public class AutoMapper
{
public static MapperConfiguration Configuration { get; private set; }
public static T MapperTo<T>(object source)
{
var _configuration = new MapperConfiguration(cfg =>
{
cfg.CreateMap(source.GetType(), typeof(T));
});
return _configuration.CreateMapper().Map<T>(source);
}
public static T MapperTo<T1, T>(T1 source)
{
var _configuration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<T1, T>() ;
});
return _configuration.CreateMapper().Map<T>(source);
}
public static T MapperToWithIgnore<T1, T>(T1 source, params string[] ignoreName)
{
var _configuration = new MapperConfiguration(cfg =>
{
var exp = cfg.CreateMap<T1, T>();
foreach (var name in ignoreName)
{
exp.ForMember(name, m => m.Ignore());
}
});
return _configuration.CreateMapper().Map<T>(source);
}
public static List<T> MapperToList<T1, T>(List<T1> source)
{
var _configuration = new MapperConfiguration(cfg => cfg.CreateMap<T1, T>());
return _configuration.CreateMapper().Map<List<T1>, List<T>>(source);
}
public static List<T> MapperToListDoAfter<T1, T>(List<T1> source, Action<List<T1>, List<T>> action)
{
var _configuration = new MapperConfiguration(cfg => cfg.CreateMap<T1, T>());
return _configuration.CreateMapper().Map<List<T1>, List<T>>(source, ops =>
{
try
{
ops.AfterMap(action);
}
catch
{
//ignore
}
});
}
public void Execute(Assembly assembly)
{
Configuration = new MapperConfiguration(
cfg =>
{
var types = assembly.GetExportedTypes();
LoadStandardMappings(types, cfg);
LoadReverseMappings(types, cfg);
LoadCustomMappings(types, cfg);
});
}
public void ExecuteByAssemblyName(params string[] assemblys)
{
if (assemblys.Length < 1)
{
throw new ArgumentException("assemblys");
}
var all = AppDomain.CurrentDomain.GetAssemblies();
var modelAss = all.Where(assembly => assemblys.Contains(assembly.GetName().Name)).ToArray();
if (!modelAss.Any())
{
return;
}
Configuration = new MapperConfiguration(
cfg =>
{
foreach (var assembly in modelAss)
{
var types = assembly.GetExportedTypes();
LoadStandardMappings(types, cfg);
LoadReverseMappings(types, cfg);
LoadCustomMappings(types, cfg);
}
});
}
private static void LoadStandardMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration)
{
var maps = (from t in types
from i in t.GetInterfaces()
where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>) &&
!t.IsAbstract &&
!t.IsInterface
select new
{
Source = i.GetGenericArguments()[0],
Destination = t
}).ToArray();
foreach (var map in maps)
{
mapperConfiguration.CreateMap(map.Source, map.Destination);
}
}
private static void LoadReverseMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration)
{
var maps = (from t in types
from i in t.GetInterfaces()
where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapTo<>) &&
!t.IsAbstract &&
!t.IsInterface
select new
{
Destination = i.GetGenericArguments()[0],
Source = t
}).ToArray();
foreach (var map in maps)
{
mapperConfiguration.CreateMap(map.Source, map.Destination);
}
}
private static void LoadCustomMappings(IEnumerable<Type> types, IMapperConfigurationExpression mapperConfiguration)
{
var maps = (from t in types
from i in t.GetInterfaces()
where typeof(IHaveCustomMappings).IsAssignableFrom(t) &&
!t.IsAbstract &&
!t.IsInterface
select (IHaveCustomMappings)Activator.CreateInstance(t)).ToArray();
foreach (var map in maps)
{
map.CreateMappings(mapperConfiguration);
}
}
}
}