Skip to content

Commit b20f638

Browse files
committed
Pre-calculate IsClassProxy by PocoEntityTuplizer
1 parent 4789e7f commit b20f638

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

src/NHibernate.Test/NHSpecificTest/GH2067/Fixture.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using NHibernate.Cfg.MappingSchema;
33
using NHibernate.Mapping.ByCode;
4+
using NHibernate.Proxy;
45
using NUnit.Framework;
56

67
namespace NHibernate.Test.NHSpecificTest.GH2067
@@ -65,9 +66,11 @@ public void CanLoadDomesticCatUsingBaseClass()
6566
var domesticCat = (IDomesticCat) cat;
6667
Assert.That(domesticCat.Name, Is.EqualTo("Tom"));
6768
Assert.That(domesticCat.OwnerName, Is.EqualTo("Jerry"));
69+
var proxy = (INHibernateProxy) cat;
70+
Assert.That(proxy.HibernateLazyInitializer.PersistentClass, Is.EqualTo(typeof(Cat)));
6871
}
6972
}
70-
73+
7174
[Test]
7275
public void CanLoadDomesticCatUsingBaseClassInterface()
7376
{
@@ -80,6 +83,8 @@ public void CanLoadDomesticCatUsingBaseClassInterface()
8083
var domesticCat = (IDomesticCat) cat;
8184
Assert.That(domesticCat.Name, Is.EqualTo("Tom"));
8285
Assert.That(domesticCat.OwnerName, Is.EqualTo("Jerry"));
86+
var proxy = (INHibernateProxy) cat;
87+
Assert.That(proxy.HibernateLazyInitializer.PersistentClass, Is.EqualTo(typeof(Cat)));
8388
}
8489
}
8590

@@ -93,6 +98,8 @@ public void CanLoadDomesticCatUsingInterface()
9398
Assert.That(cat, Is.Not.Null);
9499
Assert.That(cat.Name, Is.EqualTo("Tom"));
95100
Assert.That(cat.OwnerName, Is.EqualTo("Jerry"));
101+
var proxy = (INHibernateProxy) cat;
102+
Assert.That(proxy.HibernateLazyInitializer.PersistentClass, Is.EqualTo(typeof(DomesticCat)));
96103
}
97104
}
98105

@@ -116,6 +123,8 @@ public void CanLoadDomesticCatUsingSealedClass()
116123
Assert.That(cat, Is.Not.Null);
117124
Assert.That(cat.Name, Is.EqualTo("Tom"));
118125
Assert.That(cat.OwnerName, Is.EqualTo("Jerry"));
126+
var proxy = (INHibernateProxy) cat;
127+
Assert.That(proxy.HibernateLazyInitializer.PersistentClass, Is.EqualTo(typeof(DomesticCat)));
119128
}
120129
}
121130

@@ -128,6 +137,8 @@ public void CanLoadDomesticCatUsingSideInterface()
128137
var cat = session.Load<IPet>(domesticCatId);
129138
Assert.That(cat, Is.Not.Null);
130139
Assert.That(cat.OwnerName, Is.EqualTo("Jerry"));
140+
var proxy = (INHibernateProxy) cat;
141+
Assert.That(proxy.HibernateLazyInitializer.PersistentClass, Is.EqualTo(typeof(DomesticCat)));
131142
}
132143
}
133144

@@ -140,6 +151,8 @@ public void CanLoadCatUsingClass()
140151
var cat = session.Load<Cat>(catId);
141152
Assert.That(cat, Is.Not.Null);
142153
Assert.That(cat.Name, Is.EqualTo("Bob"));
154+
var proxy = (INHibernateProxy) cat;
155+
Assert.That(proxy.HibernateLazyInitializer.PersistentClass, Is.EqualTo(typeof(Cat)));
143156
}
144157
}
145158

@@ -152,6 +165,8 @@ public void CanLoadCatUsingInterface()
152165
var cat = session.Load<ICat>(catId);
153166
Assert.That(cat, Is.Not.Null);
154167
Assert.That(cat.Name, Is.EqualTo("Bob"));
168+
var proxy = (INHibernateProxy) cat;
169+
Assert.That(proxy.HibernateLazyInitializer.PersistentClass, Is.EqualTo(typeof(Cat)));
155170
}
156171
}
157172
}

src/NHibernate/Proxy/AbstractProxyFactory.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ public abstract class AbstractProxyFactory: IProxyFactory
1919
protected virtual MethodInfo SetIdentifierMethod { get; private set; }
2020
protected virtual IAbstractComponentType ComponentIdType { get; private set; }
2121
protected virtual bool OverridesEquals { get; set; }
22-
23-
protected bool IsClassProxy
24-
{
25-
get { return Interfaces.Length == 1; }
26-
}
22+
protected internal bool IsClassProxy { get; internal set; }
2723

2824
public virtual void PostInstantiate(string entityName, System.Type persistentClass, ISet<System.Type> interfaces,
2925
MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod,
@@ -44,7 +40,6 @@ public virtual void PostInstantiate(string entityName, System.Type persistentCla
4440
OverridesEquals = ReflectHelper.OverridesEquals(persistentClass);
4541
}
4642

47-
4843
public abstract INHibernateProxy GetProxy(object id, ISessionImplementor session);
4944

5045
// Since 5.3

src/NHibernate/Proxy/NHibernateProxyFactoryInfo.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,32 @@ public sealed class NHibernateProxyFactoryInfo : ISerializable
1616
private readonly MethodInfo _getIdentifierMethod;
1717
private readonly MethodInfo _setIdentifierMethod;
1818
private readonly IAbstractComponentType _componentIdType;
19+
private readonly bool _isClassProxy;
1920

20-
internal NHibernateProxyFactoryInfo(string entityName, System.Type persistentClass, System.Type[] interfaces, MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod, IAbstractComponentType componentIdType)
21+
internal NHibernateProxyFactoryInfo(
22+
string entityName,
23+
System.Type persistentClass,
24+
System.Type[] interfaces,
25+
MethodInfo getIdentifierMethod,
26+
MethodInfo setIdentifierMethod,
27+
IAbstractComponentType componentIdType,
28+
bool isClassProxy)
2129
{
2230
_entityName = entityName;
2331
_persistentClass = persistentClass;
2432
_interfaces = interfaces;
2533
_getIdentifierMethod = getIdentifierMethod;
2634
_setIdentifierMethod = setIdentifierMethod;
2735
_componentIdType = componentIdType;
36+
_isClassProxy = isClassProxy;
2837
}
2938

3039
internal System.Type PersistentClass => _persistentClass;
3140

3241
public IProxyFactory CreateProxyFactory()
3342
{
3443
var factory = new StaticProxyFactory();
44+
factory.IsClassProxy = _isClassProxy;
3545
factory.PostInstantiate(_entityName, _persistentClass, new HashSet<System.Type>(_interfaces), _getIdentifierMethod, _setIdentifierMethod, _componentIdType);
3646
return factory;
3747
}
@@ -44,6 +54,7 @@ private NHibernateProxyFactoryInfo(SerializationInfo info, StreamingContext cont
4454
_getIdentifierMethod = (MethodInfo) info.GetValue(nameof(_getIdentifierMethod), typeof(MethodInfo));
4555
_setIdentifierMethod = (MethodInfo) info.GetValue(nameof(_setIdentifierMethod), typeof(MethodInfo));
4656
_componentIdType = (IAbstractComponentType) info.GetValue(nameof(_componentIdType), typeof(IAbstractComponentType));
57+
_isClassProxy = (bool) info.GetValue(nameof(_isClassProxy), typeof(bool));
4758
}
4859

4960
[SecurityCritical]
@@ -55,6 +66,7 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
5566
info.AddValue(nameof(_getIdentifierMethod), _getIdentifierMethod);
5667
info.AddValue(nameof(_setIdentifierMethod), _setIdentifierMethod);
5768
info.AddValue(nameof(_componentIdType), _componentIdType);
69+
info.AddValue(nameof(_isClassProxy), _isClassProxy);
5870
}
5971
}
6072
}

src/NHibernate/Proxy/StaticProxyFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ public override void PostInstantiate(
5353
Interfaces,
5454
GetIdentifierMethod,
5555
SetIdentifierMethod,
56-
ComponentIdType);
57-
_cacheEntry = new ProxyCacheEntry(PersistentClass, Interfaces);
56+
ComponentIdType,
57+
IsClassProxy);
58+
_cacheEntry = new ProxyCacheEntry(IsClassProxy ? PersistentClass : typeof(object), Interfaces);
5859
}
5960

6061
private Func<ILazyInitializer, NHibernateProxyFactoryInfo, INHibernateProxy> CreateProxyActivator(ProxyCacheEntry pke)

src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,14 @@ protected override IProxyFactory BuildProxyFactory(PersistentClass persistentCla
172172
IProxyFactory pf = BuildProxyFactoryInternal(persistentClass, idGetter, idSetter);
173173
try
174174
{
175+
if (pf is AbstractProxyFactory apf)
176+
{
177+
apf.IsClassProxy = !isInterface;
178+
}
179+
175180
pf.PostInstantiate(
176181
EntityName,
177-
isInterface ? typeof(object) : _mappedClass,
182+
_mappedClass,
178183
proxyInterfaces,
179184
proxyGetIdentifierMethod,
180185
proxySetIdentifierMethod,

0 commit comments

Comments
 (0)