-
Notifications
You must be signed in to change notification settings - Fork 685
/
Copy pathAutoMapper.cs
211 lines (174 loc) · 8.08 KB
/
AutoMapper.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
#if USE_NULLABLE
#nullable enable
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using FluentNHibernate.Conventions;
using FluentNHibernate.MappingModel;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.Utils;
using FluentNHibernate.Utils.Reflection;
namespace FluentNHibernate.Automapping;
public class AutoMapper(
IAutomappingConfiguration cfg,
IConventionFinder conventionFinder,
IEnumerable<InlineOverride> inlineOverrides)
{
List<AutoMapType>? mappingTypes;
void ApplyOverrides(Type classType, IList<Member> mappedMembers, ClassMappingBase mapping)
{
var autoMapType = ReflectionHelper.AutomappingTypeForEntityType(classType);
var autoMap = Activator.CreateInstance(autoMapType, mappedMembers)!;
inlineOverrides
.Where(x => x.CanOverride(classType))
.Each(x => x.Apply(autoMap));
((IAutoClasslike)autoMap).AlterModel(mapping);
}
public ClassMappingBase MergeMap(Type classType, ClassMappingBase mapping, IList<Member> mappedMembers)
{
// map class first, then subclasses - this way subclasses can inspect the class model
// to see which properties have already been mapped
ApplyOverrides(classType, mappedMembers, mapping);
ProcessClass(mapping, classType, mappedMembers);
if (mappingTypes is not null)
MapInheritanceTree(classType, mapping, mappedMembers, mappingTypes);
return mapping;
}
void MapInheritanceTree(Type classType, ClassMappingBase mapping, IList<Member> mappedMembers, List<AutoMapType> mappingTypes)
{
var discriminatorSet = HasDiscriminator(mapping);
var isDiscriminated = cfg.IsDiscriminated(classType) || discriminatorSet;
var mappingTypesWithLogicalParents = GetMappingTypesWithLogicalParents(mappingTypes);
foreach (var inheritedClass in mappingTypesWithLogicalParents
.Where(x => x.Value is not null && x.Value.Type == classType)
.Select(x => x.Key))
{
var tempMapping = mapping as ClassMapping;
if (isDiscriminated && !discriminatorSet && tempMapping is not null)
{
var discriminatorColumn = cfg.GetDiscriminatorColumn(classType);
var discriminator = new DiscriminatorMapping
{
ContainingEntityType = classType,
};
discriminator.Set(x => x.Type, Layer.Defaults, new TypeReference(typeof(string)));
var columnMapping = new ColumnMapping();
columnMapping.Set(x => x.Name, Layer.Defaults, discriminatorColumn);
discriminator.AddColumn(Layer.Defaults, columnMapping);
tempMapping.Set(x => x.Discriminator, Layer.Defaults, discriminator);
discriminatorSet = true;
}
SubclassMapping subclassMapping;
var tempSubClassMap = mapping as SubclassMapping;
if(tempMapping is not null && tempMapping.IsUnionSubclass || tempSubClassMap is not null && tempSubClassMap.SubclassType == SubclassType.UnionSubclass)
{
subclassMapping = new SubclassMapping(SubclassType.UnionSubclass);
subclassMapping.Set(x => x.Type, Layer.Defaults, inheritedClass.Type);
}
else if (isDiscriminated || tempSubClassMap is not null && tempSubClassMap.SubclassType == SubclassType.Subclass)
{
subclassMapping = new SubclassMapping(SubclassType.Subclass);
subclassMapping.Set(x => x.Type, Layer.Defaults, inheritedClass.Type);
}
else
{
subclassMapping = new SubclassMapping(SubclassType.JoinedSubclass);
subclassMapping.Set(x => x.Type, Layer.Defaults, inheritedClass.Type);
subclassMapping.Set(x => x.Key, Layer.Defaults, new KeyMapping());
var columnMapping = new ColumnMapping();
columnMapping.Set(x => x.Name, Layer.Defaults, mapping.Type.Name + "_id");
subclassMapping.Key.AddColumn(Layer.Defaults, columnMapping);
}
// track separate set of properties for each sub-tree within inheritance hierarchy
var subclassMembers = new List<Member>(mappedMembers);
MapSubclass(subclassMembers, subclassMapping, inheritedClass);
mapping.AddSubclass(subclassMapping);
MergeMap(inheritedClass.Type, subclassMapping, subclassMembers);
}
}
static bool HasDiscriminator(ClassMappingBase? mapping)
{
if (mapping is ClassMapping && ((ClassMapping) mapping).Discriminator is not null)
return true;
return false;
}
Dictionary<AutoMapType, AutoMapType?> GetMappingTypesWithLogicalParents(List<AutoMapType> mappingTypes)
{
var excludedTypes = mappingTypes
.Where(x => cfg.IsConcreteBaseType(x.Type.BaseType))
.ToArray();
var availableTypes = mappingTypes.Except(excludedTypes).ToDictionary(x => x.Type);
var mappingTypesWithLogicalParents = new Dictionary<AutoMapType, AutoMapType?>();
foreach (var type in availableTypes)
mappingTypesWithLogicalParents.Add(type.Value, GetLogicalParent(type.Key, availableTypes));
return mappingTypesWithLogicalParents;
}
static AutoMapType? GetLogicalParent(Type type, IDictionary<Type, AutoMapType> availableTypes)
{
if (type.BaseType == typeof(object) || type.BaseType is null)
return null;
if (availableTypes.TryGetValue(type.BaseType, out var baseType))
return baseType;
return GetLogicalParent(type.BaseType, availableTypes);
}
void MapSubclass(IList<Member> mappedMembers, SubclassMapping subclass, AutoMapType inheritedClass)
{
subclass.Set(x => x.Name, Layer.Defaults, inheritedClass.Type.AssemblyQualifiedName);
subclass.Set(x => x.Type, Layer.Defaults, inheritedClass.Type);
ApplyOverrides(inheritedClass.Type, mappedMembers, subclass);
ProcessClass(subclass, inheritedClass.Type, mappedMembers);
inheritedClass.IsMapped = true;
}
public virtual void ProcessClass(ClassMappingBase mapping, Type entityType, IList<Member> mappedMembers)
{
entityType.GetInstanceMembers()
.Where(cfg.ShouldMap)
.Each(x => TryMapProperty(mapping, x, mappedMembers));
}
void TryMapProperty(ClassMappingBase mapping, Member member, IList<Member> mappedMembers)
{
if (member.HasIndexParameters) return;
foreach (var rule in cfg.GetMappingSteps(this, conventionFinder))
{
if (!rule.ShouldMap(member)) continue;
if (mappedMembers.Contains(member)) continue;
rule.Map(mapping, member);
mappedMembers.Add(member);
break;
}
}
public ClassMapping Map(Type classType, List<AutoMapType> types)
{
var classMap = new ClassMapping();
classMap.Set(x => x.Type, Layer.Defaults, classType);
classMap.Set(x => x.Name, Layer.Defaults, classType.AssemblyQualifiedName);
classMap.Set(x => x.TableName, Layer.Defaults, GetDefaultTableName(classType));
mappingTypes = types;
return (ClassMapping)MergeMap(classType, classMap, new List<Member>());
}
static string GetDefaultTableName(Type type)
{
var tableName = type.Name;
if (type.IsGenericType)
{
// special case for generics: GenericType_GenericParameterType
tableName = type.Name.Substring(0, type.Name.IndexOf('`'));
foreach (var argument in type.GetGenericArguments())
{
tableName += "_";
tableName += argument.Name;
}
}
return "`" + tableName + "`";
}
/// <summary>
/// Flags a type as already mapped, stop it from being auto-mapped.
/// </summary>
public void FlagAsMapped(Type type)
{
mappingTypes
?.Where(x => x.Type == type)
.Each(x => x.IsMapped = true);
}
}