forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicEntityInstantiator.cs
52 lines (42 loc) · 1.15 KB
/
DynamicEntityInstantiator.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
using System;
using System.Collections.Generic;
using NHibernate.Mapping;
using NHibernate.Util;
namespace NHibernate.Tuple
{
[Serializable]
public class DynamicEntityInstantiator : IInstantiator
{
public const string Key = "$type$";
private readonly string _entityName;
private readonly HashSet<string> _isInstanceEntityNames = new HashSet<string>();
public DynamicEntityInstantiator(PersistentClass mappingInfo)
{
_entityName = mappingInfo.EntityName;
_isInstanceEntityNames.Add(_entityName);
if (mappingInfo.HasSubclasses)
{
foreach (var subclassInfo in mappingInfo.SubclassClosureIterator)
_isInstanceEntityNames.Add(subclassInfo.EntityName);
}
}
#region IInstantiator Members
public object Instantiate(object id)
{
return Instantiate();
}
public object Instantiate()
{
IDictionary<string, object> map = new DynamicComponent();
map[Key] = _entityName;
return map;
}
public bool IsInstance(object obj)
{
if (!(obj is IDictionary<string, object> that))
return false;
return that.TryGetValue(Key, out var type) && _isInstanceEntityNames.Contains(type as string);
}
#endregion
}
}