<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://nhforge.org/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Identity Field, Equality and Hash Code</title><link>http://nhforge.org/wikis/patternsandpractices/identity-field-equality-and-hash-code.aspx</link><description>Find articles regarding best practices with NHibernate.</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Identity Field, Equality and Hash Code</title><link>http://nhforge.org/wikis/patternsandpractices/identity-field-equality-and-hash-code.aspx</link><pubDate>Tue, 23 Sep 2008 20:28:33 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:36</guid><dc:creator>gabriel.schenker</dc:creator><comments>http://nhforge.org/wikis/patternsandpractices/identity-field-equality-and-hash-code/comments.aspx</comments><description>Current revision posted to Patterns &amp;amp; Practices by gabriel.schenker on 23/09/2008 05:28:33 p.m.&lt;br /&gt;
&lt;p&gt;In this article I&amp;#39;ll describe a possible base class for domain entities which 
implements a surrogate key as identity field and provides equality and hash 
code.&lt;/p&gt;
&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Martin Fowler writes in his &lt;a href="http://martinfowler.com/books.html#eaa"&gt;PoEAA&lt;/a&gt; book: &amp;quot;&lt;em&gt;The identity 
field saves a database ID field in an object to maintain identity between an 
in-memory object and a database row&lt;/em&gt;.&amp;quot;&lt;/p&gt;
&lt;p&gt;And further he states: &amp;quot;&lt;em&gt;The first concern is whether to use meaningful or 
meaningless keys. A meaningful key is something like the U.S. Social Security 
Number... A meaningless key is essentially a random number the database dreams 
up that&amp;#39;s never intended for human use.&lt;/em&gt;&amp;quot;&lt;/p&gt;
&lt;p&gt;There are many reasons why meaningful keys often are NOT good candidates for 
an identity field. Primarily they often are not immutable (due to possible human 
errors) and not unique. Thus Martin Fowler states: &amp;quot;... &lt;em&gt;As a result, 
meaningful keys should be distrusted.&lt;/em&gt; ...&amp;quot;&lt;/p&gt;
&lt;p&gt;Having you provided some background about the ongoing dispute about what is a 
good candidate for an identity field I&amp;#39;ll now make my choice. I always choose 
meaningless keys as identity fields. Such fields are often called &lt;a href="http://en.wikipedia.org/wiki/Surrogate_key"&gt;surrogate key&lt;/a&gt;. Important: 
&amp;quot;&lt;em&gt;The surrogate key is &lt;strong&gt;not&lt;/strong&gt; derived from application 
data.&lt;/em&gt;&amp;quot;&lt;/p&gt;
&lt;p&gt;My favorite type of surrogate key is a &lt;strong&gt;GUID&lt;/strong&gt; (global unique 
identifier). The mathematical algorithm used to generate a new GUID is such as 
that it is (nearly) impossible to generate the same ID twice (the probability 
tends to zero).&lt;/p&gt;
&lt;p&gt;NHibernate supports GUID as one possible type for the identity field.&lt;/p&gt;
&lt;h2&gt;Problem Description&lt;/h2&gt;
&lt;p&gt;When dealing with NHibernate one often uses a special type of collection 
known as &lt;strong&gt;Set&lt;/strong&gt;. A set is a collection that contains no duplicate 
elements. More formally, &lt;em&gt;sets&lt;/em&gt; contain no pair of elements &lt;em&gt;e1&lt;/em&gt; 
and &lt;em&gt;e2 &lt;/em&gt;such that &lt;em&gt;e1.Equals(e2)&lt;/em&gt;, and at most one &lt;em&gt;null&lt;/em&gt; 
element. As the &lt;b&gt;Set &lt;/b&gt;is not provided by the .NET framework NHibernate uses the 
IESI collections library which contains an implementation of a set.&lt;/p&gt;
&lt;p&gt;In the definition above you find which is the important predicate to decide 
whether two elements are the same or not. It is the 
&lt;em&gt;&lt;strong&gt;Equals&lt;/strong&gt;&lt;/em&gt; function. By default the Equals function takes 
the hash code of two objects and compares it. So if two variables &lt;em&gt;e1&lt;/em&gt; 
and &lt;em&gt;e2&lt;/em&gt; refer to 2 different instances of a class &lt;em&gt;Equals&lt;/em&gt; will 
always return false. But we want to use the identity field as the relevant part 
in the comparison of two instances. If two different instances have the 
&lt;strong&gt;same&lt;/strong&gt; identity field then they are equal (that is they refer to 
the same database record).&lt;/p&gt;
&lt;h2&gt;Implementation&lt;/h2&gt;
&lt;p&gt;The default implementation of the &lt;b&gt;Equals &lt;/b&gt;function is to be found in the 
&lt;b&gt;System.Object&lt;/b&gt; class. From this class all other classes in .NET implicitly or 
explicitly inherit. Fortunately the &lt;b&gt;Equals &lt;/b&gt;function is virtual and we are able 
to override it. But when overriding the &lt;em&gt;Equals&lt;/em&gt; function we have to also 
override the &lt;em&gt;GetHashCode&lt;/em&gt; function.&lt;/p&gt;
&lt;p&gt;Assuming that we take a &lt;strong&gt;GUID&lt;/strong&gt; called &lt;strong&gt;Id&lt;/strong&gt; as 
identity field we can define the following base class from which all our domain 
classes directly or indirectly will inherit&lt;/p&gt;
&lt;div&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; IdentityFieldProvider&amp;lt;T&amp;gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;where&lt;/span&gt; T : IdentityFieldProvider&amp;lt;T&amp;gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; Guid _id;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;virtual&lt;/span&gt; Guid Id&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        get { &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _id; }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        set { _id = &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;; }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now lets override the Equals method. A possible solution is&lt;/p&gt;
&lt;div&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; Equals(&lt;span style="color:#0000ff;"&gt;object&lt;/span&gt; obj)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    T other = obj &lt;span style="color:#0000ff;"&gt;as&lt;/span&gt; T;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (other == &lt;span style="color:#0000ff;"&gt;null&lt;/span&gt;)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;false&lt;/span&gt;;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#008000;"&gt;// handle the case of comparing two NEW objects&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; otherIsTransient = Equals(other.Id, Guid.Empty);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; thisIsTransient = Equals(Id, Guid.Empty);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (otherIsTransient &amp;amp;&amp;amp; thisIsTransient)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; ReferenceEquals(other, &lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; other.Id.Equals(Id);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We have to distinguish 3 possible cases. The first one is that the 
user/developer wants to compare two objects of different type. This case is 
trivial; the answer is ALWAYS &amp;quot;not equal&amp;quot;. The second case is when the two 
objects are both new (also called &lt;em&gt;transient&lt;/em&gt;) then the two references 
point to the same instance. And the third case just takes the implementation of 
the Equals method of the GUID type to check for equality.&lt;/p&gt;
&lt;p&gt;Now we have to also override the &lt;em&gt;GetHashCode&lt;/em&gt; method also inherited 
from &lt;b&gt;System.Object&lt;/b&gt;.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;? _oldHashCode;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt; GetHashCode()&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#008000;"&gt;// Once we have a hash code we&amp;#39;ll never change it&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (_oldHashCode.HasValue)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _oldHashCode.Value;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; thisIsTransient = Equals(Id, Guid.Empty);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#008000;"&gt;// When this instance is transient, we use the base GetHashCode()&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#008000;"&gt;// and remember it, so an instance can NEVER change its hash code.&lt;/span&gt;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt; (thisIsTransient)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    {&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;        _oldHashCode = &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;.GetHashCode();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; _oldHashCode.Value;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    }&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; Id.GetHashCode();&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now, why this kind of code you might ask yourself? Well, a object should 
never ever change it&amp;#39;s hash code during its life, that is from the moment the 
object is instantiated until it is disposed. If a object is restored from 
database there is no problem since any existing database record has always a 
well defined and unique identity field. Thus we can derive the hash code from 
this &lt;b&gt;Id&lt;/b&gt; field. This is done in the last line of code in the code snippet 
above.&lt;/p&gt;
&lt;p&gt;A little bit more problematic is the case when a new object is created in 
memory, then it&amp;#39;s identity field is undefined (the object has not been saved to 
the database so far and is thus considered as being &lt;em&gt;transient&lt;/em&gt;). In our 
case undefined means that the &lt;b&gt;Id &lt;/b&gt;field has a value of &lt;em&gt;Guid.Empty&lt;/em&gt;. In 
this case we take the default implementation (of System.Object) of the 
&lt;em&gt;GetHashCode&lt;/em&gt; method to generate a hash code. But we store is in an 
instance variable for further reference.&lt;/p&gt;
&lt;p&gt;Later in the life cycle of the instance it may be persisted to the database 
(but still continues to sit around in the memory). At this moment NHibernate 
assigns a new unique value to the Id field of the instance. Now the object isn&amp;#39;t 
transient any more but the 2 first lines in the method avoid that the hash code 
of the object changes. It is still the same object as before. It has just been 
made persistent.&lt;/p&gt;
&lt;p&gt;Finally we can also override the two operators &amp;#39;==&amp;#39; and &amp;#39;!=&amp;#39; to make it 
possible to compare two instances with those operators instead of only the 
&lt;em&gt;Equals&lt;/em&gt; method.&lt;/p&gt;
&lt;div&gt;
&lt;div style="border-style:none;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;operator&lt;/span&gt; ==(IdentityFieldProvider&amp;lt;T&amp;gt; x, IdentityFieldProvider&amp;lt;T&amp;gt; y)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; Equals(x, y);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;}&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;bool&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;operator&lt;/span&gt; !=(IdentityFieldProvider&amp;lt;T&amp;gt; x, IdentityFieldProvider&amp;lt;T&amp;gt; y)&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;{&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt; !(x == y);&lt;/pre&gt;
&lt;pre style="border-style:none;margin:0em;padding:0px;overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;background-color:white;"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#39;s it. You can now use this class as the base for every entity class in 
your domain and never ever have to think about the identity field and the 
equality of objects. It just happens...&lt;/p&gt;</description></item></channel></rss>
