(NHibernate v1.2)
Hi and thanks in advance for the help this problem is really puzzling me.
in the NHibernate docs, there is a note about implementing Implementing Equals() and GetHashCode()... it is written here:
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode
This is code from the example:
Code:
public override bool Equals(object other)
{
if (this == other) return true;
Cat cat = other as Cat;
if (cat == null) return false; // null or not a cat
if (Name != cat.Name) return false;
if (!Birthday.Equals(cat.Birthday)) return false;
return true;
}
Now imagine that the class Cat is a "lazy" class (in this version of NHibernate every class is lazy by default.)
Then, all of Cat's methods and properties are virtual, and dynamic prxies of Cat are created at runtime.
Do you know what happens if we pass a proxy object into the equality method above?
Code:
Cat cat = other as Cat;
if (cat == null) return false; // null or not a cat
If the object "other" is a dynamic proxy of cat, this will always return false on the equality, because "cat" is null, because I cannot cast between the proxied object and the real object.
This has some very annoying implications... how can I write this code better?
Is there any method inside the NHibernate library that will return a Cat object from a dynamic proxied object? Do I need to extract an interface out of the "Cat" class? Do I need to get rid of my lazy classes?
any suggestions?