These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Proxy's inner reference leaks out, ReferenceEquals fails
PostPosted: Thu Jan 08, 2009 6:25 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Related post: http://forum.hibernate.org/viewtopic.php?t=992593

If an entity class passes a reference to itself through method calls or events (e.g. when implementing INotifyPropertyChanged), for proxied instances, outside callers get a reference to the proxy's inner object. This causes problems when you need to distinguish between instances of the same type and ID (which means you can't use object.Equals), but need to treat the proxy and its inner instance as the same (which means you can't use object.Reference Equals either).

A kludgy workaround is to define a "memory instance ID" property on all your entity classes (or your entity base class) and compare that, because it will be the same for a proxy and its inner reference. However, it would be better to have something like an Unproxy() method on the session, or at least a utility method like AreUnproxiedReferencesEqual().

Does anyone else have this problem? It seems to be a basic consequence of the Decorator design pattern (transparent enclosure) that proxies use (GoF p. 178, "A decorator and its component aren't identical").


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 10:47 am 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
From http://forum.hibernate.org/viewtopic.php?t=982610

This should do the trick:

Code:
public static object Unproxy(object vo)
{
    if (vo is INHibernateProxy)
    {
        LazyInitializer li = NHibernateProxyHelper.GetLazyInitializer((INHibernateProxy)vo);
        return li.GetImplementation();
    }

    return vo;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.