Skip to content

Commit a40d738

Browse files
Use a new instantiator instead of changing the old one
And obsolete the old one To be squashed.
1 parent 3b3504c commit a40d738

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using NHibernate.Mapping;
5+
6+
namespace NHibernate.Tuple
7+
{
8+
[Serializable]
9+
public class DynamicEntityInstantiator : IInstantiator
10+
{
11+
public const string Key = "$type$";
12+
13+
private readonly string _entityName;
14+
private readonly HashSet<string> _isInstanceEntityNames = new HashSet<string>();
15+
16+
public DynamicEntityInstantiator(PersistentClass mappingInfo)
17+
{
18+
_entityName = mappingInfo.EntityName;
19+
_isInstanceEntityNames.Add(_entityName);
20+
if (mappingInfo.HasSubclasses)
21+
{
22+
foreach (var subclassInfo in mappingInfo.SubclassClosureIterator)
23+
_isInstanceEntityNames.Add(subclassInfo.EntityName);
24+
}
25+
}
26+
27+
protected virtual IDictionary<string, object> GenerateMap()
28+
{
29+
return new Dictionary<string, object>();
30+
}
31+
32+
#region IInstantiator Members
33+
34+
public object Instantiate(object id)
35+
{
36+
return Instantiate();
37+
}
38+
39+
public object Instantiate()
40+
{
41+
var map = GenerateMap();
42+
if (_entityName != null)
43+
{
44+
map[Key] = _entityName;
45+
}
46+
47+
return map;
48+
}
49+
50+
public bool IsInstance(object obj)
51+
{
52+
if (!(obj is IDictionary<string, object> that))
53+
return false;
54+
if (_entityName == null)
55+
return true;
56+
57+
var type = (string) that[Key];
58+
return type == null || _isInstanceEntityNames.Contains(type);
59+
60+
}
61+
62+
#endregion
63+
}
64+
}

src/NHibernate/Tuple/DynamicMapInstantiator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace NHibernate.Tuple
77
{
8+
//Since v5.2
9+
[Obsolete("This class is not used and will be removed in a future version.")]
810
[Serializable]
911
public class DynamicMapInstantiator : IInstantiator
1012
{
@@ -13,8 +15,6 @@ public class DynamicMapInstantiator : IInstantiator
1315
private readonly string entityName;
1416
private readonly HashSet<string> isInstanceEntityNames = new HashSet<string>();
1517

16-
//Since v5.2
17-
[Obsolete("This constructor is not used and will be removed in a future version.")]
1818
public DynamicMapInstantiator()
1919
{
2020
entityName = null;
@@ -50,7 +50,7 @@ public object Instantiate()
5050

5151
protected virtual IDictionary GenerateMap()
5252
{
53-
return new Dictionary<string, object>();
53+
return new Hashtable();
5454
}
5555

5656
public bool IsInstance(object obj)

src/NHibernate/Tuple/Entity/DynamicMapEntityTuplizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected override ISetter BuildPropertySetter(Mapping.Property mappedProperty,
5757

5858
protected override IInstantiator BuildInstantiator(PersistentClass mappingInfo)
5959
{
60-
return new DynamicMapInstantiator(mappingInfo);
60+
return new DynamicEntityInstantiator(mappingInfo);
6161
}
6262

6363
protected override IProxyFactory BuildProxyFactory(PersistentClass mappingInfo, IGetter idGetter,

0 commit comments

Comments
 (0)