views:

339

answers:

3

I've recently inherited an application that makes very heavy use of session, including storing a lot of custom data objects in session. One of my first points of business with this application was to at least move the session data away from InProc, and off load it to either a stateserver or SQL Server.

After I made all of the appropriate data objects serializable, and changed the web.config to use a state service, everything appeared to work fine.

However, I found that this application does a lot of object comparisons using GetHashCode(). Methods that worked fine when the session was InProc no longer work because the HashCodes no longer match when they are supposed to. This appears to be the case when trying to find a specific child object from a parent when you know the child object's original hash code

If I simply change the web.config back to using inproc, it works again.

Anyone have any thoughts on where to begin with this?


EDIT:

qbeuek: thanks for the quick reply. In regards to:

The default implementation of GetHashCode in Object class return a hash value based on objects address in memory or something similar. If some other identity comparison is required, you have to override both Equals and GetHashCode.

I should have given more information on how they are using this. Basically, they have one parent data object, and there are several arrays of child objects. They happen to know the hash code for a particular object they need, so they are looping through a specific array of child objects looking for a hash code that matches. Once a match is found, they then use that object for other work.

A: 

Override the GetHashCode method in classes that get called this method and calculate the hash code based on unique object properties (like ID or all object fields).

qbeuek
+1  A: 

When you write

does a lot of object comparisons using GetHashCode()

i sense there is something horribly wrong with this code. The GetHashCode method does not guarantee, that the returned hash values should be in any way unique given two different objects. As far as GetHashCode is concerned, it can return 0 for all objects and still be considered correct.

When two object are the same (the Equals method returns true), they MUST have the same value returned from GetHashCode. When two objects have the same hash value, they can be the same object (Equals returns true) or be different objects (Equals returns false).

There are no other guarantees on the result of GetHashCode.

The default implementation of GetHashCode in Object class return a hash value based on objects address in memory or something similar. If some other identity comparison is required, you have to override both Equals and GetHashCode.

qbeuek
A: 

Solution 1: Create a unique ID for all the child objects and use that instead of hash code.

Solution 2: Replace if (a.GetHashCode() == b.GetHashCode()) with if (a.Equals(b)).

Hallgrim