var blog = new ThoughtStream(me); RSS 2.0
 Monday, March 24, 2008

If you are considering the use of NHibernate, or are already using NHibernate, be sure that you always override the .Equals and .GetHashCode methods of your entities. NHibernate makes use of these methods extensively and you are likely to have strange issues if you don't override both of these correctly. Unfortunately, GetHashCode is one of those areas that is difficult to get right; but it needs to be done anyways.

For more information, see:

And one specific note on .Equals: be sure to check ReferencEquals as the last resort, if no other comparison is possible.

I've learned these lessons the hard way; hopefully you won't have to.

Monday, March 24, 2008 2:29:02 PM (Central Standard Time, UTC-06:00)  #    Comments [2]. Trackback 
Tags: .NET | NHibernate

Tuesday, March 25, 2008 12:06:17 AM (Central Standard Time, UTC-06:00)
It would be very handy if you dove into the details a little bit on exactly why you need to override GetHashCode and Equals... Do you need to override these for every entity that NHibernate persists?
Tuesday, March 25, 2008 7:27:15 AM (Central Standard Time, UTC-06:00)
Dan,

I left those details out, because I would be re-stating what the above links already talk about (specifically, the last 4 - the first two give good detail on how to properly override these methods).

In a nutshell, though, I recommend overriding equals and gethashcode on ever class that NHibernate will be persisting. My reason for this is that I very rarely, if ever, see a class that is not used in a collection (ISet) or across multiple NHibernate sessions.

The pragmatist in me says that you don't have to override it for all objects. I've done plenty of small sample apps using NHibernate, and not had problems when I didn't override these. However, in any medium to large scale system, I would rather avoid the issue by having the correct code in place immediately vs. wait until I see the issue and then pull my hair out trying to find out why it doesn't work (which is what I've been doing for the last 4 days, hence this post. :) )
Comments are closed.
Navigation
About Me
View Derick Bailey's profile on LinkedIn

Send mail to the author(s) Contact Me
Archive
<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Derick Bailey
Sign In
Statistics
Total Posts: 115
This Year: 0
This Month: 0
This Week: 0
Comments: 44
Themes
Pick a theme:
All Content © 2010, Derick Bailey
DasBlog theme 'Business' created by Christoph De Baene (delarou)