Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect hashcode caching in Entity #7

Open
CodesInChaos opened this issue Apr 26, 2011 · 4 comments
Open

Incorrect hashcode caching in Entity #7

CodesInChaos opened this issue Apr 26, 2011 · 4 comments
Labels

Comments

@CodesInChaos
Copy link

Entity caches the hash code on the first call to GetHashCode(). This is a bug because the properties from which the hashcode is calculated can change thus rendering the cached hashcode invalid and violation the combined contract of Equals and GetHashCode.

@alecwhittington
Copy link
Member

feel free to fork, apply the fix, and issue a pull request

@geoffreysmith
Copy link
Member

GetHashCode() uses the DomainSignature properties to calculate the HashCode(), "Since it is recommended that GetHashCode change infrequently, if at all, in an object's lifetime, it's important that properties are carefully selected which truly represent the signature of an object." I'm okay with it calling from a cache rather than calculating it each time, unless you had additional thoughts on the matter?

@geoffreysmith
Copy link
Member

Okay it has been a week with no response, please feel free to send me a note and I'll open this back up for discussion.

@cbp123
Copy link

cbp123 commented Feb 20, 2012

The implemented version of GetHashCode() causes problems when used across sessions. For example, consider the following test:

public void Test()
{
var foo = new Foo();

// Try uncommenting the next line
// foo.GetHashCode()

foo.Bars.Add(Session.Load<Bar>(55));

Session.Save(foo);

Session.Flush();
Session.Clear();

var reloadedFoo = Session.Load<Foo>(foo.Id);

Assert.That(foo.Bars.Contains(reloadedFoo));

}

This test can pass or it can fail.
It will pass if you leave the call to GetHashCode() line commented out. It will fail if you uncomment the line.

I can see why you have decided to cache the hashcode, but I find that it can be confusing and problematic.

@seif seif reopened this Feb 29, 2012
@cd21h cd21h added the wontfix label Jan 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants