-
Notifications
You must be signed in to change notification settings - Fork 21
/
MapperOptions.cs
252 lines (215 loc) · 8.96 KB
/
MapperOptions.cs
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using Detached.Annotations;
using Detached.Mappers.Annotations;
using Detached.Mappers.Annotations.Handlers;
using Detached.Mappers.TypeBinders;
using Detached.Mappers.TypeBinders.Binders;
using Detached.Mappers.TypeMappers;
using Detached.Mappers.TypeMappers.Entity.Collection;
using Detached.Mappers.TypeMappers.Entity.Complex;
using Detached.Mappers.TypeMappers.POCO.Abstract;
using Detached.Mappers.TypeMappers.POCO.Collection;
using Detached.Mappers.TypeMappers.POCO.Complex;
using Detached.Mappers.TypeMappers.POCO.Inherited;
using Detached.Mappers.TypeMappers.POCO.Nullable;
using Detached.Mappers.TypeMappers.POCO.Primitive;
using Detached.Mappers.TypePairs;
using Detached.Mappers.TypePairs.Conventions;
using Detached.Mappers.Types;
using Detached.Mappers.Types.Class;
using Detached.Mappers.Types.Class.Builder;
using Detached.Mappers.Types.Conventions;
using Detached.Mappers.Types.Dictionary;
using Detached.PatchTypes;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace Detached.Mappers
{
public class MapperOptions : IPatchTypeInfoProvider
{
readonly ConcurrentDictionary<Type, IType> _configuredTypes = new();
readonly ConcurrentDictionary<Type, IType> _allTypes = new();
readonly ConcurrentDictionary<TypeMapperKey, TypePair> _typePairs = new();
public MapperOptions()
{
Primitives = new HashSet<Type>
{
typeof(bool),
typeof(string),
typeof(char),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(short),
typeof(ushort),
typeof(byte),
typeof(float),
typeof(double),
typeof(decimal),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
typeof(Guid)
};
TypeFactories = new List<ITypeFactory>
{
new ClassTypeFactory(),
new DictionaryTypeFactory()
};
TypeConventions = new List<ITypeConvention>
{
new KeyConvention()
};
TypePairFactory = new TypePairFactory();
TypeMapperFactories = new List<ITypeMapperFactory>
{
new CollectionTypeMapperFactory(),
new ComplexTypeMapperFactory(),
new PrimitiveTypeMapperFactory(),
new AbstractTypeMapperFactory(),
new NullableTypeMapperFactory(),
new InheritedTypeMapperFactory(),
new EntityCollectionTypeMapperFactory(),
new EntityTypeMapperFactory()
};
ConcreteTypes = new Dictionary<Type, Type>
{
{ typeof(IList<>), typeof(List<>) },
{ typeof(IEnumerable<>), typeof(List<>) },
{ typeof(ICollection<>), typeof(List<>) },
{ typeof(IDictionary<,>), typeof(Dictionary<,>) }
};
AnnotationHandlers = new Dictionary<Type, IAnnotationHandler>
{
{ typeof(KeyAttribute), new KeyAnnotationHandler() },
{ typeof(AggregationAttribute), new AggregationAnnotationHandler() },
{ typeof(CompositionAttribute), new CompositionAnnotationHandler() },
{ typeof(EntityAttribute), new EntityAnnotationHandler() },
{ typeof(MapIgnoreAttribute), new MapIgnoreAnnotationHandler() },
{ typeof(ParentAttribute), new ParentAnnotationHandler() },
{ typeof(AbstractAttribute), new AbstractAnnotationHandler() },
{ typeof(PrimitiveAttribute), new PrimitiveAnnotationHandler() },
{ typeof(MapToAttribute), new MapToAnnotationHandler() },
{ typeof(MapFromAttribute), new MapFromAnnotationHandler() },
{ typeof(DiscriminatorNameAttribute), new DiscriminatorNameAnnotationHandler() },
{ typeof(DiscriminatorValueAttribute), new DiscriminatorValueAnnotationHandler() }
};
TypeBinders = new List<ITypeBinder>
{
new PrimitiveTypeBinder(),
new ComplexTypeBinder(),
new ForeignKeyTypeBinder(),
new CollectionTypeBinder(),
new InheritedTypeBinder(),
new NullableTypeBinder()
};
MemberNameConventions = new List<IMemberNameConvention>
{
new ForeignKeyMemberNameConvention(),
new DefaultMemberNameConvention(),
new MapToMemberNameConvention(),
new MapFromMemberNameConvention()
};
}
public virtual HashSet<Type> Primitives { get; }
public Dictionary<Type, IAnnotationHandler> AnnotationHandlers { get; }
public virtual List<ITypeFactory> TypeFactories { get; }
public virtual ITypePairFactory TypePairFactory { get; set; }
public List<ITypeMapperFactory> TypeMapperFactories { get; }
public virtual List<ITypeConvention> TypeConventions { get; }
public virtual List<IMemberNameConvention> MemberNameConventions { get; }
public virtual Dictionary<Type, Type> ConcreteTypes { get; }
public virtual List<ITypeBinder> TypeBinders { get; }
public virtual ClassTypeBuilder<TType> Type<TType>()
{
IType type = GetTypeConfiguration(typeof(TType));
return new ClassTypeBuilder<TType>((ClassType)type, this);
}
public virtual IType GetType(Type clrType)
{
return _allTypes.GetOrAdd(clrType, clrType =>
{
IType type = GetTypeConfiguration(clrType);
ApplyAnnotations(type);
return type;
});
}
IType GetTypeConfiguration(Type clrType)
{
return _configuredTypes.GetOrAdd(clrType, clrType =>
{
IType type = null;
for (int i = TypeFactories.Count - 1; i >= 0; i--)
{
type = TypeFactories[i].Create(this, clrType);
if (type != null)
break;
}
if (type == null)
{
throw new InvalidOperationException($"Can't get options for type {clrType.GetFriendlyName()}.");
}
return type;
});
}
void ApplyAnnotations(IType type)
{
foreach (Attribute annotation in type.ClrType.GetCustomAttributes())
{
if (AnnotationHandlers.TryGetValue(annotation.GetType(), out IAnnotationHandler handler))
{
handler.Apply(annotation, this, type, null);
}
}
if (type.MemberNames != null)
{
foreach (var memberName in type.MemberNames)
{
var member = type.GetMember(memberName);
var propInfo = member.GetPropertyInfo();
if (propInfo != null)
{
foreach (Attribute annotation in propInfo.GetCustomAttributes())
{
if (AnnotationHandlers.TryGetValue(annotation.GetType(), out IAnnotationHandler handler))
{
handler.Apply(annotation, this, type, member);
}
}
}
}
}
for (int i = TypeConventions.Count - 1; i >= 0; i--)
{
TypeConventions[i].Apply(this, type);
}
}
public TypePair GetTypePair(IType sourceType, IType targetType, TypePairMember parentMember)
{
return _typePairs.GetOrAdd(new TypeMapperKey(sourceType, targetType, parentMember), key =>
{
return TypePairFactory.Create(this, sourceType, targetType, parentMember);
});
}
public virtual bool IsPrimitive(Type type)
{
return Primitives.Contains(type)
|| type.IsEnum
|| type.IsGenericType && Primitives.Contains(type.GetGenericTypeDefinition());
}
public virtual bool ShouldMap(IType sourceType, IType targetType)
{
return sourceType != targetType
|| sourceType.IsAbstract()
|| targetType.IsAbstract()
|| (targetType.IsComplex() || targetType.IsCollection() && GetType(targetType.ItemClrType).IsComplex());
}
public virtual bool ShouldPatch(Type type)
{
return !typeof(IPatch).IsAssignableFrom(type) && GetType(type).IsComplex();
}
}
}