Skip to content

Commit 0ebb8bd

Browse files
committed
Apply patch of NH-2455 (thanks Michael DELVA)
SVN: trunk@5384
1 parent e96e910 commit 0ebb8bd

19 files changed

+125
-76
lines changed

src/NHibernate.ByteCode.Castle.Tests/ProxyInterface/CustomProxyFixture.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ public bool IsInstrumented(System.Type entityClass)
7575
return false;
7676
}
7777

78-
#endregion
78+
public bool IsProxy(object entity)
79+
{
80+
return entity is INHibernateProxy;
81+
}
82+
83+
#endregion
7984
}
8085

8186
public class DataBindingProxyFactory : ProxyFactory

src/NHibernate.ByteCode.Castle/ProxyFactoryFactory.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public bool IsInstrumented(System.Type entityClass)
2323
return true;
2424
}
2525

26-
#endregion
26+
public bool IsProxy(object entity)
27+
{
28+
return entity is INHibernateProxy;
29+
}
30+
31+
#endregion
2732
}
2833
}

src/NHibernate.ByteCode.LinFu/ProxyFactoryFactory.cs

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public bool IsInstrumented(System.Type entityClass)
2424
return false;
2525
}
2626

27+
public bool IsProxy(object entity)
28+
{
29+
return entity is INHibernateProxy;
30+
}
31+
2732
#endregion
2833
}
2934
}

src/NHibernate.Test/Bytecode/WrongProxyFactoryFactory.cs

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public bool IsInstrumented(System.Type entityClass)
2828
return false;
2929
}
3030

31+
public bool IsProxy(object entity)
32+
{
33+
return entity is INHibernateProxy;
34+
}
35+
3136
#endregion
3237
}
3338
}

src/NHibernate/Bytecode/IProxyFactoryFactory.cs

+2
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@ public interface IProxyFactoryFactory
3838
IProxyValidator ProxyValidator { get; }
3939

4040
bool IsInstrumented(System.Type entityClass);
41+
42+
bool IsProxy(object entity);
4143
}
4244
}

src/NHibernate/Engine/CascadingAction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public override void NoCascade(IEventSource session, object child, object parent
371371
{
372372
string childEntityName = ((EntityType)type).GetAssociatedEntityName(session.Factory);
373373

374-
if (!IsInManagedState(child, session) && !(child is INHibernateProxy) && ForeignKeys.IsTransient(childEntityName, child, null, session))
374+
if (!IsInManagedState(child, session) && !(child.IsProxy()) && ForeignKeys.IsTransient(childEntityName, child, null, session))
375375
{
376376
string parentEntiytName = persister.EntityName;
377377
string propertyName = persister.PropertyNames[propertyIndex];

src/NHibernate/Engine/ForeignKeys.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ private bool IsNullifiable(string entityName, object obj)
102102
//if (obj == org.hibernate.intercept.LazyPropertyInitializer_Fields.UNFETCHED_PROPERTY)
103103
// return false; //this is kinda the best we can do...
104104

105-
INHibernateProxy proxy = obj as INHibernateProxy;
106-
if (proxy != null)
105+
if (obj.IsProxy())
107106
{
108-
// if its an uninitialized proxy it can't be transient
107+
INHibernateProxy proxy = obj as INHibernateProxy;
108+
109+
// if its an uninitialized proxy it can't be transient
109110
ILazyInitializer li = proxy.HibernateLazyInitializer;
110111
if (li.GetImplementation(session) == null)
111112
{
@@ -156,7 +157,7 @@ private bool IsNullifiable(string entityName, object obj)
156157
/// </remarks>
157158
public static bool IsNotTransient(string entityName, System.Object entity, bool? assumed, ISessionImplementor session)
158159
{
159-
if (entity is INHibernateProxy)
160+
if (entity.IsProxy())
160161
return true;
161162
if (session.PersistenceContext.IsEntryFor(entity))
162163
return true;

src/NHibernate/Engine/StatefulPersistenceContext.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,11 @@ public void ReassociateProxy(object value, object id)
572572
//{
573573
// value = wrapper.Element;
574574
//}
575-
var proxy = value as INHibernateProxy;
576-
if (proxy != null)
575+
if (value.IsProxy())
577576
{
578-
if (log.IsDebugEnabled)
577+
var proxy = value as INHibernateProxy;
578+
579+
if (log.IsDebugEnabled)
579580
{
580581
log.Debug("setting proxy identifier: " + id);
581582
}
@@ -619,10 +620,11 @@ public object Unproxy(object maybeProxy)
619620
// maybeProxy = wrapper.Element;
620621
//}
621622

622-
INHibernateProxy proxy = maybeProxy as INHibernateProxy;
623-
if (proxy != null)
623+
if (maybeProxy.IsProxy())
624624
{
625-
ILazyInitializer li = proxy.HibernateLazyInitializer;
625+
INHibernateProxy proxy = maybeProxy as INHibernateProxy;
626+
627+
ILazyInitializer li = proxy.HibernateLazyInitializer;
626628
if (li.IsUninitialized)
627629
throw new PersistentObjectException("object was an uninitialized proxy for " + li.PersistentClass.FullName);
628630

@@ -647,10 +649,11 @@ public object UnproxyAndReassociate(object maybeProxy)
647649
//{
648650
// maybeProxy = wrapper.Element;
649651
//}
650-
var proxy = maybeProxy as INHibernateProxy;
651-
if (proxy != null)
652+
if (maybeProxy.IsProxy())
652653
{
653-
ILazyInitializer li = proxy.HibernateLazyInitializer;
654+
var proxy = maybeProxy as INHibernateProxy;
655+
656+
ILazyInitializer li = proxy.HibernateLazyInitializer;
654657
ReassociateProxy(li, proxy);
655658
return li.GetImplementation(); //initialize + unwrap the object
656659
}

src/NHibernate/Event/Default/DefaultEvictEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public virtual void OnEvict(EvictEvent @event)
2424
object obj = @event.Entity;
2525
IPersistenceContext persistenceContext = source.PersistenceContext;
2626

27-
if (obj is INHibernateProxy)
27+
if (obj.IsProxy())
2828
{
2929
ILazyInitializer li = ((INHibernateProxy)obj).HibernateLazyInitializer;
3030
object id = li.Identifier;

src/NHibernate/Event/Default/DefaultMergeEventListener.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public virtual void OnMerge(MergeEvent @event, IDictionary copiedAlready)
9090
if (original != null)
9191
{
9292
object entity;
93-
if (original is INHibernateProxy)
93+
if (original.IsProxy())
9494
{
9595
ILazyInitializer li = ((INHibernateProxy)original).HibernateLazyInitializer;
9696
if (li.IsUninitialized)
@@ -521,7 +521,7 @@ protected EventCache GetTransientCopyCache(MergeEvent @event, EventCache copyCac
521521
{
522522
object entityCopy = copyCache[entity];
523523

524-
if (entityCopy is INHibernateProxy)
524+
if (entityCopy.IsProxy())
525525
entityCopy = ((INHibernateProxy)entityCopy).HibernateLazyInitializer.GetImplementation();
526526

527527
// NH-specific: Disregard entities that implement ILifecycle and manage their own state - they

src/NHibernate/Event/Default/DefaultPersistEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public virtual void OnPersist(PersistEvent @event, IDictionary createdAlready)
3838
object obj = @event.Entity;
3939

4040
object entity;
41-
if (obj is INHibernateProxy)
41+
if (obj.IsProxy())
4242
{
4343
ILazyInitializer li = ((INHibernateProxy)obj).HibernateLazyInitializer;
4444
if (li.IsUninitialized)

src/NHibernate/Event/Default/DefaultSaveOrUpdateEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public virtual void OnSaveOrUpdate(SaveOrUpdateEvent @event)
3131
{
3232
//assign the requested id to the proxy, *before*
3333
//reassociating the proxy
34-
if (obj is INHibernateProxy)
34+
if (obj.IsProxy())
3535
{
3636
((INHibernateProxy)obj).HibernateLazyInitializer.Identifier = requestedId;
3737
}

src/NHibernate/Impl/SessionImpl.cs

+26-18
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,11 @@ public LockMode GetCurrentLockMode(object obj)
432432
{
433433
throw new ArgumentNullException("obj", "null object passed to GetCurrentLockMode");
434434
}
435-
var proxy = obj as INHibernateProxy;
436-
if (proxy != null)
435+
436+
if (obj.IsProxy())
437437
{
438-
obj = proxy.HibernateLazyInitializer.GetImplementation(this);
438+
var proxy = obj as INHibernateProxy;
439+
obj = proxy.HibernateLazyInitializer.GetImplementation(this);
439440
if (obj == null)
440441
{
441442
return LockMode.None;
@@ -1040,10 +1041,10 @@ public override string BestGuessEntityName(object entity)
10401041
{
10411042
using (new SessionIdLoggingContext(SessionId))
10421043
{
1043-
INHibernateProxy proxy = entity as INHibernateProxy;
1044-
if (proxy != null)
1044+
if (entity.IsProxy())
10451045
{
1046-
ILazyInitializer initializer = proxy.HibernateLazyInitializer;
1046+
INHibernateProxy proxy = entity as INHibernateProxy;
1047+
ILazyInitializer initializer = proxy.HibernateLazyInitializer;
10471048

10481049
// it is possible for this method to be called during flush processing,
10491050
// so make certain that we do not accidently initialize an uninitialized proxy
@@ -1292,10 +1293,12 @@ public string GetEntityName(object obj)
12921293
using (new SessionIdLoggingContext(SessionId))
12931294
{
12941295
CheckAndUpdateSessionStatus();
1295-
var proxy = obj as INHibernateProxy;
1296-
if (proxy != null)
1296+
1297+
if (obj.IsProxy())
12971298
{
1298-
if (!persistenceContext.ContainsProxy(proxy))
1299+
var proxy = obj as INHibernateProxy;
1300+
1301+
if (!persistenceContext.ContainsProxy(proxy))
12991302
{
13001303
throw new TransientObjectException("proxy was not associated with the session");
13011304
}
@@ -1517,10 +1520,12 @@ public object GetIdentifier(object obj)
15171520
// Actually the case for proxies will probably work even with
15181521
// the session closed, but do the check here anyway, so that
15191522
// the behavior is uniform.
1520-
var proxy = obj as INHibernateProxy;
1521-
if (proxy != null)
1523+
1524+
if (obj.IsProxy())
15221525
{
1523-
ILazyInitializer li = proxy.HibernateLazyInitializer;
1526+
var proxy = obj as INHibernateProxy;
1527+
1528+
ILazyInitializer li = proxy.HibernateLazyInitializer;
15241529
if (li.Session != this)
15251530
{
15261531
throw new TransientObjectException("The proxy was not associated with this session");
@@ -1547,10 +1552,11 @@ public override object GetContextEntityIdentifier(object obj)
15471552
{
15481553
using (new SessionIdLoggingContext(SessionId))
15491554
{
1550-
INHibernateProxy proxy = obj as INHibernateProxy;
1551-
if (proxy != null)
1555+
if (obj.IsProxy())
15521556
{
1553-
return proxy.HibernateLazyInitializer.Identifier;
1557+
INHibernateProxy proxy = obj as INHibernateProxy;
1558+
1559+
return proxy.HibernateLazyInitializer.Identifier;
15541560
}
15551561
else
15561562
{
@@ -1942,10 +1948,12 @@ public bool Contains(object obj)
19421948
using (new SessionIdLoggingContext(SessionId))
19431949
{
19441950
CheckAndUpdateSessionStatus();
1945-
var proxy = obj as INHibernateProxy;
1946-
if (proxy != null)
1951+
1952+
if (obj.IsProxy())
19471953
{
1948-
//do not use proxiesByKey, since not all
1954+
var proxy = obj as INHibernateProxy;
1955+
1956+
//do not use proxiesByKey, since not all
19491957
//proxies that point to this session's
19501958
//instances are in that collection!
19511959
ILazyInitializer li = proxy.HibernateLazyInitializer;

src/NHibernate/Impl/StatelessSessionImpl.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ public override string BestGuessEntityName(object entity)
470470
{
471471
using (new SessionIdLoggingContext(SessionId))
472472
{
473-
INHibernateProxy proxy = entity as INHibernateProxy;
474-
if (proxy != null)
473+
if (entity.IsProxy())
475474
{
476-
entity = proxy.HibernateLazyInitializer.GetImplementation();
475+
INHibernateProxy proxy = entity as INHibernateProxy;
476+
entity = proxy.HibernateLazyInitializer.GetImplementation();
477477
}
478478
return GuessEntityName(entity);
479479
}

src/NHibernate/Intercept/AbstractFieldInterceptor.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ public object Intercept(object target, string fieldName, object value)
114114
{
115115
return InitializeField(fieldName, target);
116116
}
117-
var nhproxy = value as INHibernateProxy;
118-
if (nhproxy != null && unwrapProxyFieldNames != null && unwrapProxyFieldNames.Contains(fieldName))
117+
118+
if (value.IsProxy() && unwrapProxyFieldNames != null && unwrapProxyFieldNames.Contains(fieldName))
119119
{
120-
return InitializeOrGetAssociation(nhproxy, fieldName);
120+
var nhproxy = value as INHibernateProxy;
121+
122+
return InitializeOrGetAssociation(nhproxy, fieldName);
121123
}
122124
return InvokeImplementation;
123125
}

src/NHibernate/NHibernateUtil.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ public static void Initialize(object proxy)
365365
{
366366
return;
367367
}
368-
else if (proxy is INHibernateProxy)
368+
else if (proxy.IsProxy())
369369
{
370370
((INHibernateProxy)proxy).HibernateLazyInitializer.Initialize();
371371
}
@@ -382,7 +382,7 @@ public static void Initialize(object proxy)
382382
/// <returns>true if the argument is already initialized, or is not a proxy or collection</returns>
383383
public static bool IsInitialized(object proxy)
384384
{
385-
if (proxy is INHibernateProxy)
385+
if (proxy.IsProxy())
386386
{
387387
return !((INHibernateProxy)proxy).HibernateLazyInitializer.IsUninitialized;
388388
}
@@ -404,7 +404,7 @@ public static bool IsInitialized(object proxy)
404404
/// <returns>the true class of the instance</returns>
405405
public static System.Type GetClass(object proxy)
406406
{
407-
if (proxy is INHibernateProxy)
407+
if (proxy.IsProxy())
408408
{
409409
return ((INHibernateProxy)proxy).HibernateLazyInitializer.GetImplementation().GetType();
410410
}
@@ -530,7 +530,7 @@ public static void Close(IEnumerable enumerable)
530530
public static bool IsPropertyInitialized(object proxy, string propertyName)
531531
{
532532
object entity;
533-
if (proxy is INHibernateProxy)
533+
if (proxy.IsProxy())
534534
{
535535
ILazyInitializer li = ((INHibernateProxy)proxy).HibernateLazyInitializer;
536536
if (li.IsUninitialized)

0 commit comments

Comments
 (0)