NHibernate 2 Proxies and Equals Method Gotcha

NHibernate Forum

NHibernate 2 Proxies and Equals Method Gotcha

Postby flahan on Tue Nov 07, 2006 6:55 pm

I thought I would share a little issue that I came across while porting to Nhibernate 2.0. I have used a few of the templates and they all (of the ones I have used) seem to generate an Equals method similar to this:

Code: Select all
public override bool Equals( object obj )
{
   if( this == obj )
      return true;

   if( ( obj == null ) || ( obj.GetType() != this.GetType() ) )
      return false;

   CreditAppStipulation castObj = (CreditAppStipulation)obj;

   return ( castObj != null ) &&
       ( this.m_castipid == castObj.CaStipId );
}


The problem is that with NHibernate 2.0 you have the introduction of Proxy objects and the second if test will not always work. In my case, I had a situation where I had a regular POCO and a proxy for a CreditAppStipulation object. It took me a while to track down that my objects weren't testing as equal because the GetType() methods were returning two different type values :? .

So, what I did was change my equals tests to use the following format:
Code: Select all
        public override bool Equals(object obj)
        {
            if (this == obj) return true;
            CreditAppStipulation castObj = obj as CreditAppStipulation;
            return (castObj != null) &&
                (this.m_castipid == castObj.CaStipId);
        }


This seems to take care of the problem I was having. I thought I would share it in case any of the template owners or users might find it helpful.

Pat
flahan
Lurker
 
Posts: 2
Joined: Tue Nov 07, 2006 6:39 pm

Postby flahan on Mon Jan 22, 2007 6:34 pm

Another issue that you may have to deal with is comparing default values. I ran into a situation where I was adding objects to a collection and then needed to go back and delete one of the objects before they had been saved to the db. In this case they both had a default value in the id field of zero :roll: . So what happened is the wrong object was being deleted from the collection because two different objects would test as equal since they both had an id of 0.

So changed the Equals method from:
Code: Select all
        public override bool Equals(object obj)
        {
            if (this == obj) return true;
            CreditAppStipulation castObj = obj as CreditAppStipulation;
            return (castObj != null) &&
                (this.m_castipid == castObj.CaStipId);
        }


To:
Code: Select all

        public override bool Equals(object obj)
        {
            if (this == obj) return true;
            CreditAppStipulation castObj = obj as CreditAppStipulation;
            return (castObj != null) &&
                (this.m_castipid == castObj.CaStipId
                 && this.m_castipid != 0);
        }


I added the check for the zero value in the id field. I made the assumption that if the object I was comparing had a zero value in the id field, it should be equal to any other object. If you are comparing the object to itself, then the first if test should catch it.
flahan
Lurker
 
Posts: 2
Joined: Tue Nov 07, 2006 6:39 pm


Return to NHibernate

Who is online

Users browsing this forum: No registered users and 2 guests