Expose helper method to get underlying instance from a proxy
Description
Environment
Attachments
Activity

Lee Henson June 8, 2009 at 6:47 AM
Ah I wasn't aware of those methods on PersistenceContext. That's fine, although could we extend the Unproxy method there to allow you specify that you want the proxy initialized? e.g.
Unproxy(object maybeProxy)
{
return Unproxy(maybeProxy, false);
}
Unproxy(object maybeProxy, bool initialize)
{
INHibernateProxy proxy = maybeProxy as INHibernateProxy;
if (proxy != null)
{
ILazyInitializer li = proxy.HibernateLazyInitializer;
if (li.IsUninitialized && !initialize)
throw new PersistentObjectException("object was an uninitialized proxy for " + li.PersistentClass.FullName);
return li.GetImplementation(); // unwrap the object
}
else
{
return maybeProxy;
}
}
(Patch attached for that change)
When you say Unproxy is unsafe, what do you mean? It's kind of a power-user method anyway, something to call when you really have to, not an everyday thing.

Fabio Maulo June 4, 2009 at 10:27 AM
Please attach a test case is you want it for NH2.1.0

Fabio Maulo June 4, 2009 at 10:24 AM
For real you have all you need, and much more, in IPersistenceContext
((ISessionImplementor)session).PersistenceContext.Unproxy
or
((ISessionImplementor)session).PersistenceContext.UnproxyAndReassociate
A simple NHibernateProxyHelper.Unproxy(proxy) is unsafe, btw its usage is under user responsibility.

Lee Henson June 4, 2009 at 9:17 AM
Patch for trunk.
Details
Details
Assignee
Reporter

Patch adds support for:
var underlyingInstance = NHibernateProxyHelper.Unproxy(proxy);
It simply extracts the proxy initialization logic out of NHibernateUtil.GetClass() and puts in a client-usable form.