Hi
I have just debugged a bit of a problem caused by proxies that I found quite a gotcha. I'm modelling an organisation structure in my application and I have a method on my OrganisationUnit class for searching up the tree for a unit satisfying a min unit type level. The method is recursive:
Code:
public virtual OrganisationUnit GetSatisfying(UnitType level)
{
if (Satisfies(level))
{
return this;
}
else
{
if (parent != null)
return parent.GetSatisfying(level);
else
return this;
}
}
The problem is when nHibernate proxies are involved and I return "this" in the preceding method I'm returning the member object of the proxy, the real object, instead of the proxy if one exists. I read the advice on testing for the type when dealing with proxies but I don't remember anything on this one.
This results in problems in other searches and comparisons because I end up comparing this real object to the proxy version in other code and of course they are not the same object instance.
I suppose I could use Equals in other comparisons and in this circumstance I could probably move the test one level up and check the parent and return that if successful but still it is a little un-nerving.
I'm not sure if this is completely possible but could the nHibernate DynamicProxy interceptor replace any references to the real object with references to the proxy object on return from method calls?
Cheers
Duncan