Hibernate version: 1.2.1GA
Just noticed this and am wondering (a) if it's a bug or design flaw and (b) what is the best way to handle it.
NH proxies seem to be pretty good about not unnecessarily initializing themselves. You can get an entity's Id and invoke Equals() and GetHashCode(), all without initializing.
UNLESS you override Equals(), as is recommended by the NH docs, along with GetHashCode().
Once you do that, any call to Equals() or GetHashCode() results in the initialization of the object. To me, this suggests that there's no good way to get proxies to play nicely with .NET collections.
It seems like my choices are:
- Override Equals(), allowing me to correctly insert and retrieve entries from, say, an IDictionary(), but automatically initializing everything I put into it
- Don't override anything, meaning I risk the reliability of operations on the IDictionary, but at least I don't unnecesarily initialize anything
- Write my own collection implementations or object wrappers.
If I assume that (1) my session will always give back the same proxy for a given instance, and (2) NHibernate's own collection implementations don't rely on me overriding Equals(), and I also don't use collections that span sessions, maybe the lesser evil is the second option I listed.
For the moment, I'm creating an NHibernate.Engine.EntityKey to put entities into an IDictionary, and that seems to work fine, but I'd rather just put the entity/proxy itself into the IDictionary and rely on normal collection semantics.