Hello all,
I've run into a bit of an issue with the implementation of Equals in
EntityWithTypedId<IdT> and the fact that NHibernate can sometimes
return dynamic proxies when using lazy loading.
I'm just getting started with NHibernate so my apologies if I am
butchering any of the details here. I ran into the problem in a test
where I was comparing for equivalence a list of entities persisted and
evicted in setup to a corresponding list loaded in the method under
test. One of the entities appeared to be coming back as the proxy
type and looks to be the result of the fact that the same entity was
loaded as a lazy proxy through an association on another entity loaded
within the target method.
I had figured that the type check in the Equals method might be the
issue and once I stepped through with the debugger it appears my
comparison was failing here:
if (compareTo == null || !GetType().Equals(compareTo.GetType()))
return false;
I don't know that I have an answer as I realize Billy has very
carefully considered ithe implications of Equals and HashCode here but
I'm wondering if it may make sense to either ditch the GetType check
or consider some alternatives within Nhibernate itself. I know there
is some way to specify an interface to be used for dynamic proxies if
necessary. However, if we are casting using as, is it sufficient to
just satisfy the equality condition if compareTo is not null?
A few interesting, related links I came across:
http://cwmaier.blogspot.com/2007/07/liskov-substitution-principle-equals.html
(Hibernate Specific)
http://blogs.chayachronicles.com/sonofnun/archive/2007/03/30/230.aspx
Thanks in advance for any help!