forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractExplicitlyDeclaredModel.cs
595 lines (517 loc) · 17.6 KB
/
AbstractExplicitlyDeclaredModel.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace NHibernate.Mapping.ByCode
{
public abstract class AbstractExplicitlyDeclaredModel : IModelExplicitDeclarationsHolder
{
private readonly HashSet<MemberInfo> any = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> arrays = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> bags = new HashSet<MemberInfo>();
private readonly HashSet<System.Type> components = new HashSet<System.Type>();
private readonly HashSet<MemberInfo> dictionaries = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> idBags = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> lists = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> keyManyToManyRelations = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> itemManyToManyRelations = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> manyToAnyRelations = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> manyToOneRelations = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> naturalIds = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> composedIds = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> oneToManyRelations = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> oneToOneRelations = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> poids = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> properties = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> dynamicComponents = new HashSet<MemberInfo>();
private readonly Dictionary<MemberInfo, System.Type> dynamicComponentTemplates = new Dictionary<MemberInfo, System.Type>();
private readonly HashSet<MemberInfo> persistentMembers = new HashSet<MemberInfo>();
private readonly HashSet<System.Type> rootEntities = new HashSet<System.Type>();
private readonly HashSet<MemberInfo> sets = new HashSet<MemberInfo>();
private readonly HashSet<System.Type> tablePerClassEntities = new HashSet<System.Type>();
private readonly HashSet<System.Type> tablePerClassHierarchyEntities = new HashSet<System.Type>();
private readonly HashSet<System.Type> tablePerConcreteClassEntities = new HashSet<System.Type>();
private readonly HashSet<MemberInfo> versionProperties = new HashSet<MemberInfo>();
private readonly Dictionary<System.Type, HashSet<string>> typeSplitGroups = new Dictionary<System.Type, HashSet<string>>();
private readonly Dictionary<MemberInfo, string> memberSplitGroup = new Dictionary<MemberInfo, string>();
private readonly HashSet<SplitDefinition> splitDefinitions = new HashSet<SplitDefinition>();
#region Delayed registrations
// To allow a free organization of mappings, especially in presence of a IModelMapper not based exclusivelly on pure declarative mapping, and then
// provide a descriptive exception when there are some ambiguity, we have to delay the registration as late as we can
// (in practice to the moment we have to give a certain asnwer about the strategy used to represent a System.Type hierarchy).
private readonly Queue<System.Action> delayedRootEntityRegistrations = new Queue<System.Action>();
private readonly Dictionary<System.Type, List<Action<System.Type>>> delayedEntityRegistrations = new Dictionary<System.Type, List<Action<System.Type>>>();
#endregion
public IEnumerable<System.Type> RootEntities
{
get { return rootEntities; }
}
public IEnumerable<System.Type> Components
{
get { return components; }
}
public IEnumerable<System.Type> TablePerClassEntities
{
get { return tablePerClassEntities; }
}
public IEnumerable<System.Type> TablePerClassHierarchyEntities
{
get { return tablePerClassHierarchyEntities; }
}
public IEnumerable<System.Type> TablePerConcreteClassEntities
{
get { return tablePerConcreteClassEntities; }
}
public IEnumerable<MemberInfo> OneToOneRelations
{
get { return oneToOneRelations; }
}
public IEnumerable<MemberInfo> ManyToOneRelations
{
get { return manyToOneRelations; }
}
public IEnumerable<MemberInfo> KeyManyToManyRelations
{
get { return keyManyToManyRelations; }
}
public IEnumerable<MemberInfo> ItemManyToManyRelations
{
get { return itemManyToManyRelations; }
}
public IEnumerable<MemberInfo> OneToManyRelations
{
get { return oneToManyRelations; }
}
public IEnumerable<MemberInfo> ManyToAnyRelations
{
get { return manyToAnyRelations; }
}
public IEnumerable<MemberInfo> Any
{
get { return any; }
}
public IEnumerable<MemberInfo> Poids
{
get { return poids; }
}
public IEnumerable<MemberInfo> ComposedIds
{
get { return composedIds; }
}
public IEnumerable<MemberInfo> VersionProperties
{
get { return versionProperties; }
}
public IEnumerable<MemberInfo> NaturalIds
{
get { return naturalIds; }
}
public IEnumerable<MemberInfo> Sets
{
get { return sets; }
}
public IEnumerable<MemberInfo> Bags
{
get { return bags; }
}
public IEnumerable<MemberInfo> IdBags
{
get { return idBags; }
}
public IEnumerable<MemberInfo> Lists
{
get { return lists; }
}
public IEnumerable<MemberInfo> Arrays
{
get { return arrays; }
}
public IEnumerable<MemberInfo> Dictionaries
{
get { return dictionaries; }
}
public IEnumerable<MemberInfo> Properties
{
get { return properties; }
}
public IEnumerable<MemberInfo> DynamicComponents
{
get { return dynamicComponents; }
}
public IEnumerable<MemberInfo> PersistentMembers
{
get { return persistentMembers; }
}
public IEnumerable<SplitDefinition> SplitDefinitions
{
get { return splitDefinitions; }
}
public IEnumerable<string> GetSplitGroupsFor(System.Type type)
{
HashSet<string> splitGroupIds;
if (typeSplitGroups.TryGetValue(type, out splitGroupIds))
{
return splitGroupIds;
}
return Enumerable.Empty<string>();
}
public string GetSplitGroupFor(MemberInfo member)
{
var memberKey = member.GetMemberFromDeclaringType();
string splitGroup;
if (memberSplitGroup.TryGetValue(memberKey, out splitGroup))
{
return splitGroup;
}
return null;
}
public void AddAsRootEntity(System.Type type)
{
rootEntities.Add(type);
if (IsComponent(type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered as entity and as component", type.FullName));
}
}
public abstract bool IsComponent(System.Type type);
public void AddAsComponent(System.Type type)
{
components.Add(type);
var rootEntity = GetSingleRootEntityOrNull(type);
if (rootEntity != null)
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered as entity and as component", type.FullName));
}
}
public void AddAsTablePerClassEntity(System.Type type)
{
AddAsTablePerClassEntity(type, false);
}
protected virtual void AddAsTablePerClassEntity(System.Type type, bool rootEntityMustExists)
{
if(!rootEntityMustExists)
{
delayedRootEntityRegistrations.Enqueue(() => System.Array.ForEach(GetRootEntitiesOf(type).ToArray(), root=> tablePerClassEntities.Add(root)));
EnlistTypeRegistration(type, t => AddAsTablePerClassEntity(t, true));
return;
}
if (IsComponent(type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered as entity and as component", type.FullName));
}
var rootEntity = GetSingleRootEntityOrNull(type);
if (rootEntity != null)
{
if (rootEntity.Equals(type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-class strategy", type.FullName));
}
if (IsMappedFor(tablePerClassHierarchyEntities, type) || IsMappedFor(tablePerConcreteClassEntities, type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered with more than one class-hierarchy strategy", type.FullName));
}
}
else
{
throw new MappingException(string.Format("The root entity for {0} was never registered", type.FullName));
}
}
public void AddAsTablePerClassHierarchyEntity(System.Type type)
{
AddAsTablePerClassHierarchyEntity(type, false);
}
protected virtual void AddAsTablePerClassHierarchyEntity(System.Type type, bool rootEntityMustExists)
{
if (!rootEntityMustExists)
{
delayedRootEntityRegistrations.Enqueue(() => System.Array.ForEach(GetRootEntitiesOf(type).ToArray(), root => tablePerClassHierarchyEntities.Add(root)));
EnlistTypeRegistration(type, t => AddAsTablePerClassHierarchyEntity(t, true));
return;
}
if (IsComponent(type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered as entity and as component", type.FullName));
}
var rootEntity = GetSingleRootEntityOrNull(type);
if (rootEntity != null)
{
if (rootEntity.Equals(type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-class-hierarchy strategy", type.FullName));
}
if (IsMappedFor(tablePerClassEntities, type) || IsMappedFor(tablePerConcreteClassEntities, type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered with more than one class-hierarchy strategy", type.FullName));
}
}
else
{
throw new MappingException(string.Format("The root entity for {0} was never registered", type.FullName));
}
}
public void AddAsTablePerConcreteClassEntity(System.Type type)
{
AddAsTablePerConcreteClassEntity(type, false);
}
protected virtual void AddAsTablePerConcreteClassEntity(System.Type type, bool rootEntityMustExists)
{
if (!rootEntityMustExists)
{
delayedRootEntityRegistrations.Enqueue(() => System.Array.ForEach(GetRootEntitiesOf(type).ToArray(), root => tablePerConcreteClassEntities.Add(root)));
EnlistTypeRegistration(type, t => AddAsTablePerConcreteClassEntity(t, true));
return;
}
if (IsComponent(type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered as entity and as component", type.FullName));
}
var rootEntity = GetSingleRootEntityOrNull(type);
if (rootEntity != null)
{
if (rootEntity.Equals(type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered as root-entity and as subclass for table-per-concrete-class strategy", type.FullName));
}
if (IsMappedFor(tablePerClassEntities, type) || IsMappedFor(tablePerClassHierarchyEntities, type))
{
throw new MappingException(string.Format("Ambiguous mapping of {0}. It was registered with more than one class-hierarchy strategy", type.FullName));
}
}
else
{
throw new MappingException(string.Format("The root entity for {0} was never registered", type.FullName));
}
}
public void AddAsOneToOneRelation(MemberInfo member)
{
persistentMembers.Add(member);
oneToOneRelations.Add(member);
}
public void AddAsManyToOneRelation(MemberInfo member)
{
persistentMembers.Add(member);
manyToOneRelations.Add(member);
}
public void AddAsManyToManyKeyRelation(MemberInfo member)
{
persistentMembers.Add(member);
keyManyToManyRelations.Add(member);
}
public void AddAsManyToManyItemRelation(MemberInfo member)
{
persistentMembers.Add(member);
itemManyToManyRelations.Add(member);
}
public void AddAsOneToManyRelation(MemberInfo member)
{
persistentMembers.Add(member);
oneToManyRelations.Add(member);
}
public void AddAsManyToAnyRelation(MemberInfo member)
{
persistentMembers.Add(member);
manyToAnyRelations.Add(member);
}
public void AddAsAny(MemberInfo member)
{
persistentMembers.Add(member);
any.Add(member);
}
public void AddAsPoid(MemberInfo member)
{
persistentMembers.Add(member);
poids.Add(member);
}
public void AddAsPartOfComposedId(MemberInfo member)
{
persistentMembers.Add(member);
composedIds.Add(member);
}
public void AddAsVersionProperty(MemberInfo member)
{
persistentMembers.Add(member);
versionProperties.Add(member);
}
public void AddAsNaturalId(MemberInfo member)
{
persistentMembers.Add(member);
naturalIds.Add(member);
}
public void AddAsSet(MemberInfo member)
{
persistentMembers.Add(member);
sets.Add(member);
}
public void AddAsBag(MemberInfo member)
{
persistentMembers.Add(member);
bags.Add(member);
}
public void AddAsIdBag(MemberInfo member)
{
persistentMembers.Add(member);
idBags.Add(member);
}
public void AddAsList(MemberInfo member)
{
persistentMembers.Add(member);
lists.Add(member);
}
public void AddAsArray(MemberInfo member)
{
persistentMembers.Add(member);
arrays.Add(member);
}
public void AddAsMap(MemberInfo member)
{
persistentMembers.Add(member);
dictionaries.Add(member);
}
public void AddAsProperty(MemberInfo member)
{
persistentMembers.Add(member);
properties.Add(member);
}
public void AddAsPersistentMember(MemberInfo member)
{
persistentMembers.Add(member);
}
public void AddAsPropertySplit(SplitDefinition definition)
{
if (definition == null)
{
return;
}
/* Note: if the user "jump/exclude" a class and then map the property in two subclasses the usage of GetMemberFromDeclaringType() may cause a problem
for a legal usage... we will see when the case happen */
System.Type propertyContainer = definition.On;
string splitGroupId = definition.GroupId;
MemberInfo member = definition.Member;
var memberKey = member.GetMemberFromDeclaringType();
string splitGroup;
if (!memberSplitGroup.TryGetValue(memberKey, out splitGroup))
{
AddTypeSplits(propertyContainer, splitGroupId);
memberSplitGroup[memberKey] = splitGroupId;
}
splitDefinitions.Add(definition);
}
public void AddAsDynamicComponent(MemberInfo member, System.Type componentTemplate)
{
persistentMembers.Add(member);
dynamicComponents.Add(member);
dynamicComponentTemplates[member] = componentTemplate;
}
private void AddTypeSplits(System.Type propertyContainer, string splitGroupId)
{
HashSet<string> splitsGroupsIds;
typeSplitGroups.TryGetValue(propertyContainer, out splitsGroupsIds);
if(splitsGroupsIds == null)
{
splitsGroupsIds = new HashSet<string>();
typeSplitGroups[propertyContainer] = splitsGroupsIds;
}
splitsGroupsIds.Add(splitGroupId);
}
public virtual System.Type GetDynamicComponentTemplate(MemberInfo member)
{
System.Type template;
dynamicComponentTemplates.TryGetValue(member, out template);
return template ?? typeof(object);
}
protected System.Type GetSingleRootEntityOrNull(System.Type entityType)
{
var rootTypes = GetRootEntitiesOf(entityType).ToList();
if(rootTypes.Count > 1)
{
var sb = new StringBuilder(1024);
sb.AppendLine(string.Format("Ambiguous mapping for {0}. More than one root entities was found:", entityType.FullName));
foreach (var rootType in rootTypes.AsEnumerable().Reverse())
{
sb.AppendLine(rootType.FullName);
}
sb.AppendLine("Possible solutions:");
sb.AppendLine("- Merge the mappings of root entity in the one is representing the real root of the hierarchy.");
sb.AppendLine("- Inject a IModelInspector with a logic to discover the real root-entity.");
throw new MappingException(sb.ToString());
}
return rootTypes.SingleOrDefault(IsRootEntity);
}
protected IEnumerable<System.Type> GetRootEntitiesOf(System.Type entityType)
{
if (entityType == null)
{
yield break;
}
if(IsRootEntity(entityType))
{
yield return entityType;
}
foreach (var type in entityType.GetBaseTypes().Where(IsRootEntity))
{
yield return type;
}
}
public abstract bool IsRootEntity(System.Type entityType);
protected bool IsMappedForTablePerClassEntities(System.Type type)
{
return IsMappedFor(tablePerClassEntities, type);
}
protected bool IsMappedForTablePerClassHierarchyEntities(System.Type type)
{
return IsMappedFor(tablePerClassHierarchyEntities, type);
}
protected bool IsMappedForTablePerConcreteClassEntities(System.Type type)
{
return IsMappedFor(tablePerConcreteClassEntities, type);
}
private bool IsMappedFor(ICollection<System.Type> explicitMappedEntities, System.Type type)
{
bool isExplicitMapped = explicitMappedEntities.Contains(type);
bool isDerived = false;
if (!isExplicitMapped)
{
isDerived = type.GetBaseTypes().Any(explicitMappedEntities.Contains);
if (isDerived)
{
explicitMappedEntities.Add(type);
}
}
return isExplicitMapped || isDerived;
}
protected void EnlistTypeRegistration(System.Type type, Action<System.Type> registration)
{
List<Action<System.Type>> actions;
if (!delayedEntityRegistrations.TryGetValue(type, out actions))
{
actions = new List<Action<System.Type>>();
delayedEntityRegistrations[type] = actions;
}
actions.Add(registration);
}
protected void ExecuteDelayedTypeRegistration(System.Type type)
{
ExecuteDelayedRootEntitiesRegistrations();
List<Action<System.Type>> actions;
if (delayedEntityRegistrations.TryGetValue(type, out actions))
{
foreach (var action in actions)
{
action(type);
}
}
}
protected void ExecuteDelayedRootEntitiesRegistrations()
{
while (delayedRootEntityRegistrations.Count > 0)
{
delayedRootEntityRegistrations.Dequeue().Invoke();
}
}
protected bool HasDelayedEntityRegistration(System.Type type)
{
return delayedEntityRegistrations.ContainsKey(type);
}
}
}