Skip to content

Commit 9e61353

Browse files
committed
Add IsClassProxy initialization to PostInstantiate
1 parent 754f740 commit 9e61353

File tree

8 files changed

+107
-46
lines changed

8 files changed

+107
-46
lines changed

src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ protected virtual void GetObjectData(SerializationInfo info, StreamingContext co
176176
public void VerifyProxyForClassWithInternalInterface()
177177
{
178178
var factory = new StaticProxyFactory();
179-
factory.PostInstantiate(typeof(InternalInterfaceTestClass).FullName, typeof(InternalInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
179+
factory.PostInstantiate(typeof(InternalInterfaceTestClass).FullName, typeof(InternalInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
180180

181181
#if NETFX
182182
VerifyGeneratedAssembly(
@@ -202,7 +202,7 @@ public void VerifyProxyForClassWithAdditionalInterface()
202202
// having an additional interface in the interface list, instead of just having INHibernateProxy.
203203
// (Quite a loose semantic...)
204204
new HashSet<System.Type> { typeof(INHibernateProxy), typeof(IPublic) },
205-
null, null, null);
205+
null, null, null, false);
206206

207207
#if NETFX
208208
VerifyGeneratedAssembly(
@@ -226,7 +226,7 @@ public void VerifyProxyForClassWithInterface()
226226
typeof(PublicInterfaceTestClass).FullName,
227227
typeof(PublicInterfaceTestClass),
228228
new HashSet<System.Type> {typeof(INHibernateProxy)},
229-
null, null, null);
229+
null, null, null, true);
230230

231231
#if NETFX
232232
VerifyGeneratedAssembly(
@@ -266,7 +266,7 @@ public void VerifyProxyForClassWithExplicitInterface()
266266
typeof(PublicExplicitInterfaceTestClass).FullName,
267267
typeof(PublicExplicitInterfaceTestClass),
268268
new HashSet<System.Type> {typeof(INHibernateProxy)},
269-
null, null, null);
269+
null, null, null, true);
270270
#if NETFX
271271
VerifyGeneratedAssembly(
272272
() =>
@@ -307,7 +307,8 @@ public void VerifyProxyForRefOutClass()
307307
new HashSet<System.Type> { typeof(INHibernateProxy) },
308308
null,
309309
null,
310-
null);
310+
null,
311+
true);
311312

312313
#if NETFX
313314
VerifyGeneratedAssembly(
@@ -350,7 +351,7 @@ public void VerifyProxyForAbstractClass()
350351
typeof(AbstractTestClass).FullName,
351352
typeof(AbstractTestClass),
352353
new HashSet<System.Type> { typeof(INHibernateProxy) },
353-
null, null, null);
354+
null, null, null, true);
354355

355356
#if NETFX
356357
VerifyGeneratedAssembly(
@@ -370,7 +371,7 @@ public void VerifyProxyForAbstractClass()
370371
public void InitializedProxyStaysInitializedAfterDeserialization()
371372
{
372373
var factory = new StaticProxyFactory();
373-
factory.PostInstantiate(typeof(SimpleTestClass).FullName, typeof(SimpleTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
374+
factory.PostInstantiate(typeof(SimpleTestClass).FullName, typeof(SimpleTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
374375
var proxy = factory.GetProxy(2, null);
375376
Assert.That(proxy, Is.Not.Null, "proxy");
376377
Assert.That(NHibernateUtil.IsInitialized(proxy), Is.False, "proxy already initialized after creation");
@@ -399,7 +400,7 @@ public void InitializedProxyStaysInitializedAfterDeserialization()
399400
public void NonInitializedProxyStaysNonInitializedAfterSerialization()
400401
{
401402
var factory = new StaticProxyFactory();
402-
factory.PostInstantiate(typeof(SimpleTestClass).FullName, typeof(SimpleTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
403+
factory.PostInstantiate(typeof(SimpleTestClass).FullName, typeof(SimpleTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
403404
var proxy = factory.GetProxy(2, null);
404405
Assert.That(proxy, Is.Not.Null, "proxy");
405406
Assert.That(NHibernateUtil.IsInitialized(proxy), Is.False, "proxy already initialized after creation");
@@ -422,7 +423,7 @@ public void NonInitializedProxyStaysNonInitializedAfterSerialization()
422423
public void CanSerializeFieldInterceptorProxy()
423424
{
424425
var factory = new StaticProxyFactory();
425-
factory.PostInstantiate(typeof(PublicInterfaceTestClass).FullName, typeof(PublicInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
426+
factory.PostInstantiate(typeof(PublicInterfaceTestClass).FullName, typeof(PublicInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
426427
var proxy = (PublicInterfaceTestClass) factory.GetFieldInterceptionProxy();
427428
proxy.Id = 1;
428429

@@ -440,7 +441,7 @@ public void CanSerializeFieldInterceptorProxy()
440441
public void CanSerializeFieldInterceptorProxyWithISerializableEntity()
441442
{
442443
var factory = new StaticProxyFactory();
443-
factory.PostInstantiate(typeof(CustomSerializationClass).FullName, typeof(CustomSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
444+
factory.PostInstantiate(typeof(CustomSerializationClass).FullName, typeof(CustomSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
444445
var proxy = (CustomSerializationClass) factory.GetFieldInterceptionProxy();
445446
proxy.Id = 2;
446447

@@ -458,7 +459,7 @@ public void CanSerializeFieldInterceptorProxyWithISerializableEntity()
458459
public void CanSerializeFieldInterceptorProxyWithExplicitISerializableEntity()
459460
{
460461
var factory = new StaticProxyFactory();
461-
factory.PostInstantiate(typeof(CustomExplicitSerializationClass).FullName, typeof(CustomExplicitSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
462+
factory.PostInstantiate(typeof(CustomExplicitSerializationClass).FullName, typeof(CustomExplicitSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
462463
var proxy = (CustomExplicitSerializationClass) factory.GetFieldInterceptionProxy();
463464
proxy.Id = 2;
464465

@@ -476,7 +477,7 @@ public void CanSerializeFieldInterceptorProxyWithExplicitISerializableEntity()
476477
public void VerifyFieldInterceptorProxy()
477478
{
478479
var factory = new StaticProxyFactory();
479-
factory.PostInstantiate(typeof(InternalInterfaceTestClass).FullName, typeof(InternalInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
480+
factory.PostInstantiate(typeof(InternalInterfaceTestClass).FullName, typeof(InternalInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
480481
#if NETFX
481482
VerifyGeneratedAssembly(
482483
() =>
@@ -493,7 +494,7 @@ public void VerifyFieldInterceptorProxy()
493494
public void VerifyFieldInterceptorProxyWithISerializableEntity()
494495
{
495496
var factory = new StaticProxyFactory();
496-
factory.PostInstantiate(typeof(CustomSerializationClass).FullName, typeof(CustomSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
497+
factory.PostInstantiate(typeof(CustomSerializationClass).FullName, typeof(CustomSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
497498
#if NETFX
498499
VerifyGeneratedAssembly(
499500
() =>
@@ -521,7 +522,7 @@ public void VerifyFieldInterceptorProxyWithAdditionalInterface()
521522
// to an instance of the persistentClass, and so cannot implement interface methods if it does not
522523
// inherit the persistentClass.
523524
new HashSet<System.Type> {typeof(INHibernateProxy), typeof(IPublic)},
524-
null, null, null);
525+
null, null, null, false);
525526
#if NETFX
526527
VerifyGeneratedAssembly(
527528
() =>

src/NHibernate/Proxy/AbstractProxyFactory.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ public abstract class AbstractProxyFactory: IProxyFactory
1818
protected virtual MethodInfo GetIdentifierMethod { get; private set; }
1919
protected virtual MethodInfo SetIdentifierMethod { get; private set; }
2020
protected virtual IAbstractComponentType ComponentIdType { get; private set; }
21+
protected virtual bool IsClassProxy { get; private set; }
2122
protected virtual bool OverridesEquals { get; set; }
22-
protected internal bool IsClassProxy { get; internal set; }
2323

24-
public virtual void PostInstantiate(string entityName, System.Type persistentClass, ISet<System.Type> interfaces,
25-
MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod,
26-
IAbstractComponentType componentIdType)
24+
public virtual void PostInstantiate(
25+
string entityName,
26+
System.Type persistentClass,
27+
ISet<System.Type> interfaces,
28+
MethodInfo getIdentifierMethod,
29+
MethodInfo setIdentifierMethod,
30+
IAbstractComponentType componentIdType,
31+
bool isClassProxy)
2732
{
2833
EntityName = entityName;
2934
PersistentClass = persistentClass;
@@ -38,6 +43,26 @@ public virtual void PostInstantiate(string entityName, System.Type persistentCla
3843
SetIdentifierMethod = setIdentifierMethod;
3944
ComponentIdType = componentIdType;
4045
OverridesEquals = ReflectHelper.OverridesEquals(persistentClass);
46+
IsClassProxy = isClassProxy;
47+
}
48+
49+
[Obsolete("Override PostInstantiate method with isClassProxy parameter instead.")]
50+
public virtual void PostInstantiate(
51+
string entityName,
52+
System.Type persistentClass,
53+
ISet<System.Type> interfaces,
54+
MethodInfo getIdentifierMethod,
55+
MethodInfo setIdentifierMethod,
56+
IAbstractComponentType componentIdType)
57+
{
58+
PostInstantiate(
59+
entityName,
60+
persistentClass,
61+
interfaces,
62+
getIdentifierMethod,
63+
setIdentifierMethod,
64+
componentIdType,
65+
interfaces.Count == 1);
4166
}
4267

4368
public abstract INHibernateProxy GetProxy(object id, ISessionImplementor session);

src/NHibernate/Proxy/IProxyFactory.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public interface IProxyFactory
3838
/// Essentially equivalent to constructor injection, but contracted
3939
/// here via interface.
4040
/// </remarks>
41+
[Obsolete("Use ProxyFactoryExtensions.PostInstantiate extension method instead.")]
4142
void PostInstantiate(string entityName, System.Type persistentClass, ISet<System.Type> interfaces,
4243
MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod, IAbstractComponentType componentIdType);
4344

@@ -82,5 +83,41 @@ public static object GetFieldInterceptionProxy(this IProxyFactory proxyFactory)
8283
return proxyFactory.GetFieldInterceptionProxy(null);
8384
#pragma warning restore 618
8485
}
86+
87+
// 6.0 TODO: Move to IProxyFactory
88+
public static void PostInstantiate(
89+
this IProxyFactory pf,
90+
string entityName,
91+
System.Type persistentClass,
92+
HashSet<System.Type> interfaces,
93+
MethodInfo getIdentifierMethod,
94+
MethodInfo setIdentifierMethod,
95+
IAbstractComponentType componentIdType,
96+
bool isClassProxy)
97+
{
98+
if (pf is AbstractProxyFactory apf)
99+
{
100+
apf.PostInstantiate(
101+
entityName,
102+
persistentClass,
103+
interfaces,
104+
getIdentifierMethod,
105+
setIdentifierMethod,
106+
componentIdType,
107+
isClassProxy);
108+
}
109+
else
110+
{
111+
#pragma warning disable 618
112+
pf.PostInstantiate(
113+
entityName,
114+
persistentClass,
115+
interfaces,
116+
getIdentifierMethod,
117+
setIdentifierMethod,
118+
componentIdType);
119+
#pragma warning restore 618
120+
}
121+
}
85122
}
86123
}

src/NHibernate/Proxy/Map/MapProxyFactory.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ public class MapProxyFactory : IProxyFactory
1010
{
1111
private string entityName;
1212

13-
#region IProxyFactory Members
13+
[Obsolete("Please use constructor accepting entityName instead.")]
14+
public MapProxyFactory()
15+
{
16+
}
1417

18+
public MapProxyFactory(string entityName)
19+
{
20+
this.entityName = entityName;
21+
}
22+
23+
[Obsolete("Please use constructor accepting entityName instead.")]
1524
public void PostInstantiate(string entityName, System.Type persistentClass, ISet<System.Type> interfaces,
1625
MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod,
1726
IAbstractComponentType componentIdType)
1827
{
28+
//6.0 TODO: throw NotSupportedException
1929
this.entityName = entityName;
2030
}
2131

@@ -28,7 +38,5 @@ public object GetFieldInterceptionProxy(object getInstance)
2838
{
2939
throw new NotSupportedException();
3040
}
31-
32-
#endregion
3341
}
3442
}

src/NHibernate/Proxy/NHibernateProxyFactoryInfo.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ internal NHibernateProxyFactoryInfo(
4141
public IProxyFactory CreateProxyFactory()
4242
{
4343
var factory = new StaticProxyFactory();
44-
factory.IsClassProxy = _isClassProxy;
45-
factory.PostInstantiate(_entityName, _persistentClass, new HashSet<System.Type>(_interfaces), _getIdentifierMethod, _setIdentifierMethod, _componentIdType);
44+
factory.PostInstantiate(
45+
_entityName,
46+
_persistentClass,
47+
new HashSet<System.Type>(_interfaces),
48+
_getIdentifierMethod,
49+
_setIdentifierMethod,
50+
_componentIdType,
51+
_isClassProxy);
4652
return factory;
4753
}
4854

src/NHibernate/Proxy/StaticProxyFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ public override void PostInstantiate(
4343
ISet<System.Type> interfaces,
4444
MethodInfo getIdentifierMethod,
4545
MethodInfo setIdentifierMethod,
46-
IAbstractComponentType componentIdType)
46+
IAbstractComponentType componentIdType,
47+
bool isClassProxy)
4748
{
48-
base.PostInstantiate(entityName, persistentClass, interfaces, getIdentifierMethod, setIdentifierMethod, componentIdType);
49+
base.PostInstantiate(entityName, persistentClass, interfaces, getIdentifierMethod, setIdentifierMethod, componentIdType, isClassProxy);
4950

5051
_proxyFactoryInfo = new NHibernateProxyFactoryInfo(
5152
EntityName,

src/NHibernate/Tuple/Entity/DynamicMapEntityTuplizer.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,9 @@ protected override IInstantiator BuildInstantiator(PersistentClass mappingInfo)
6060
return new DynamicEntityInstantiator(mappingInfo);
6161
}
6262

63-
protected override IProxyFactory BuildProxyFactory(PersistentClass mappingInfo, IGetter idGetter,
64-
ISetter idSetter)
63+
protected override IProxyFactory BuildProxyFactory(PersistentClass mappingInfo, IGetter idGetter, ISetter idSetter)
6564
{
66-
IProxyFactory pf = new MapProxyFactory();
67-
try
68-
{
69-
//TODO: design new lifecycle for ProxyFactory
70-
pf.PostInstantiate(EntityName, null, null, null, null, null);
71-
}
72-
catch (HibernateException he)
73-
{
74-
log.Warn(he, "could not create proxy factory for:{0}", EntityName);
75-
pf = null;
76-
}
77-
return pf;
65+
return new MapProxyFactory(EntityName);
7866
}
7967
}
8068
}

src/NHibernate/Tuple/Entity/PocoEntityTuplizer.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using NHibernate.Bytecode;
66
using NHibernate.Classic;
77
using NHibernate.Engine;
8-
using NHibernate.Intercept;
98
using NHibernate.Mapping;
109
using NHibernate.Properties;
1110
using NHibernate.Proxy;
@@ -172,11 +171,6 @@ protected override IProxyFactory BuildProxyFactory(PersistentClass persistentCla
172171
IProxyFactory pf = BuildProxyFactoryInternal(persistentClass, idGetter, idSetter);
173172
try
174173
{
175-
if (pf is AbstractProxyFactory apf)
176-
{
177-
apf.IsClassProxy = !isInterface;
178-
}
179-
180174
pf.PostInstantiate(
181175
EntityName,
182176
_mappedClass,
@@ -185,7 +179,8 @@ protected override IProxyFactory BuildProxyFactory(PersistentClass persistentCla
185179
proxySetIdentifierMethod,
186180
persistentClass.HasEmbeddedIdentifier
187181
? (IAbstractComponentType) persistentClass.Identifier.Type
188-
: null);
182+
: null,
183+
!isInterface);
189184
}
190185
catch (HibernateException he)
191186
{

0 commit comments

Comments
 (0)