Skip to content

Commit 4789e7f

Browse files
committed
Always use PersistentClass as a base class for proxy
Change invocation of IProxyFactory.PostInstantiate to pass correct base class depending on the set interface proxy
1 parent 3ab267c commit 4789e7f

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public void CanLoadDomesticCatUsingBaseClass()
6161
{
6262
var cat = session.Load<Cat>(domesticCatId);
6363
Assert.That(cat, Is.Not.Null);
64+
Assert.That(cat.Name, Is.EqualTo("Tom"));
65+
var domesticCat = (IDomesticCat) cat;
66+
Assert.That(domesticCat.Name, Is.EqualTo("Tom"));
67+
Assert.That(domesticCat.OwnerName, Is.EqualTo("Jerry"));
6468
}
6569
}
6670

@@ -72,6 +76,10 @@ public void CanLoadDomesticCatUsingBaseClassInterface()
7276
{
7377
var cat = session.Load<ICat>(domesticCatId);
7478
Assert.That(cat, Is.Not.Null);
79+
Assert.That(cat.Name, Is.EqualTo("Tom"));
80+
var domesticCat = (IDomesticCat) cat;
81+
Assert.That(domesticCat.Name, Is.EqualTo("Tom"));
82+
Assert.That(domesticCat.OwnerName, Is.EqualTo("Jerry"));
7583
}
7684
}
7785

@@ -83,6 +91,8 @@ public void CanLoadDomesticCatUsingInterface()
8391
{
8492
var cat = session.Load<IDomesticCat>(domesticCatId);
8593
Assert.That(cat, Is.Not.Null);
94+
Assert.That(cat.Name, Is.EqualTo("Tom"));
95+
Assert.That(cat.OwnerName, Is.EqualTo("Jerry"));
8696
}
8797
}
8898

@@ -104,6 +114,8 @@ public void CanLoadDomesticCatUsingSealedClass()
104114
{
105115
var cat = (IDomesticCat) session.Load(typeof(DomesticCat), domesticCatId);
106116
Assert.That(cat, Is.Not.Null);
117+
Assert.That(cat.Name, Is.EqualTo("Tom"));
118+
Assert.That(cat.OwnerName, Is.EqualTo("Jerry"));
107119
}
108120
}
109121

@@ -115,6 +127,7 @@ public void CanLoadDomesticCatUsingSideInterface()
115127
{
116128
var cat = session.Load<IPet>(domesticCatId);
117129
Assert.That(cat, Is.Not.Null);
130+
Assert.That(cat.OwnerName, Is.EqualTo("Jerry"));
118131
}
119132
}
120133

@@ -126,6 +139,7 @@ public void CanLoadCatUsingClass()
126139
{
127140
var cat = session.Load<Cat>(catId);
128141
Assert.That(cat, Is.Not.Null);
142+
Assert.That(cat.Name, Is.EqualTo("Bob"));
129143
}
130144
}
131145

@@ -137,6 +151,7 @@ public void CanLoadCatUsingInterface()
137151
{
138152
var cat = session.Load<ICat>(catId);
139153
Assert.That(cat, Is.Not.Null);
154+
Assert.That(cat.Name, Is.EqualTo("Bob"));
140155
}
141156
}
142157
}

src/NHibernate/Proxy/StaticProxyFactory.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,7 @@ public override void PostInstantiate(
5454
GetIdentifierMethod,
5555
SetIdentifierMethod,
5656
ComponentIdType);
57-
_cacheEntry = new ProxyCacheEntry(GetProxyBaseType(PersistentClass), Interfaces);
58-
}
59-
60-
private static System.Type GetProxyBaseType(System.Type type)
61-
{
62-
if (type.IsClass)
63-
{
64-
for (var t = type; t != null; t = t.BaseType)
65-
{
66-
if (!t.IsSealed)
67-
return t;
68-
}
69-
}
70-
71-
return typeof(object);
57+
_cacheEntry = new ProxyCacheEntry(PersistentClass, Interfaces);
7258
}
7359

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

src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,9 @@ protected override IInstantiator BuildInstantiator(PersistentClass persistentCla
108108
}
109109
}
110110

111-
protected override IProxyFactory BuildProxyFactory(PersistentClass persistentClass, IGetter idGetter,
112-
ISetter idSetter)
111+
protected override IProxyFactory BuildProxyFactory(PersistentClass persistentClass, IGetter idGetter, ISetter idSetter)
113112
{
114-
bool needAccesorCheck = true; // NH specific (look the comment below)
113+
bool isInterface = false; // NH specific (look the comment below)
115114

116115
// determine the id getter and setter methods from the proxy interface (if any)
117116
// determine all interfaces needed by the resulting proxy
@@ -120,27 +119,27 @@ protected override IProxyFactory BuildProxyFactory(PersistentClass persistentCla
120119
System.Type _mappedClass = persistentClass.MappedClass;
121120
System.Type _proxyInterface = persistentClass.ProxyInterface;
122121

123-
if (_proxyInterface != null && !_mappedClass.Equals(_proxyInterface))
122+
if (_proxyInterface != null && _mappedClass != _proxyInterface)
124123
{
125124
if (!_proxyInterface.IsInterface)
126125
{
127126
throw new MappingException("proxy must be either an interface, or the class itself: " + EntityName);
128127
}
129-
needAccesorCheck = false; // NH (the proxy is an interface all properties can be overridden)
128+
isInterface = true; // NH (the proxy is an interface all properties can be overridden)
130129
proxyInterfaces.Add(_proxyInterface);
131130
}
132131

133132
if (_mappedClass.IsInterface)
134133
{
135-
needAccesorCheck = false; // NH (the mapped class is an interface all properties can be overridden)
134+
isInterface = true; // NH (the mapped class is an interface all properties can be overridden)
136135
proxyInterfaces.Add(_mappedClass);
137136
}
138137

139138
foreach (Subclass subclass in persistentClass.SubclassIterator)
140139
{
141140
System.Type subclassProxy = subclass.ProxyInterface;
142141
System.Type subclassClass = subclass.MappedClass;
143-
if (subclassProxy != null && !subclassClass.Equals(subclassProxy))
142+
if (subclassProxy != null && subclassClass != subclassProxy)
144143
{
145144
if (!subclassProxy.IsInterface)
146145
{
@@ -155,7 +154,7 @@ protected override IProxyFactory BuildProxyFactory(PersistentClass persistentCla
155154
* - Check if the logger is enabled
156155
* - Don't need nothing to check if the mapped-class or proxy is an interface
157156
*/
158-
if (log.IsErrorEnabled() && needAccesorCheck)
157+
if (!isInterface && log.IsErrorEnabled())
159158
{
160159
LogPropertyAccessorsErrors(persistentClass);
161160
}
@@ -173,8 +172,15 @@ protected override IProxyFactory BuildProxyFactory(PersistentClass persistentCla
173172
IProxyFactory pf = BuildProxyFactoryInternal(persistentClass, idGetter, idSetter);
174173
try
175174
{
176-
pf.PostInstantiate(EntityName, _mappedClass, proxyInterfaces, proxyGetIdentifierMethod, proxySetIdentifierMethod,
177-
persistentClass.HasEmbeddedIdentifier ? (IAbstractComponentType) persistentClass.Identifier.Type: null);
175+
pf.PostInstantiate(
176+
EntityName,
177+
isInterface ? typeof(object) : _mappedClass,
178+
proxyInterfaces,
179+
proxyGetIdentifierMethod,
180+
proxySetIdentifierMethod,
181+
persistentClass.HasEmbeddedIdentifier
182+
? (IAbstractComponentType) persistentClass.Identifier.Type
183+
: null);
178184
}
179185
catch (HibernateException he)
180186
{

0 commit comments

Comments
 (0)