title: Lazy-loaded NHibernate properties in Equals and GetHashCode
body: <p>How can the following problem be dealt with?</p>
  
  <p>We're using lazy loaded <a href="http://en.wikipedia.org/wiki/NHibernate" rel="nofollow">NHibernate</a> properties and whenever we're calling <code>Equals()</code> or <code>GetHashCode()</code> any properties used, will be lazy-loaded, potentially causing a cascade of lazy-loading operations. Eager-loading could be used as an alternative, but I think only in specific cases and not as a general solution.</p>
  
  <p>A typical scenario would look like this:</p>
  
  <pre><code>public class AbstractSaveableObject {
      [Id(0, Name = "Id", UnsavedValue = null)]
      [Generator(1, Class = "native")]
      public virtual long? Id { get; set; }
  }
  
  [Class(NameType = typeof(ClassA))]
  public class ClassA : AbstractSavableObject {
      [Bag(0, Inverse = true, Cascade = "none")]
      [Key(1, Column = "ClassA")]
      [OneToMany(2, ClassType = typeof(ClassB))]
      public virtual ICollection&lt;ClassB&gt; ClassBs { get; set; }
  }
  
  [Class(NameType = typeof(ClassB))]
  public class ClassB : AbstractSavableObject {
  
      [ManyToOne(Column = "ClassA")]
      public virtual ClassA ClassA { get; set; }
  
      [ManyToOne]
      public virtual ClassC ClassC { get; set; }
  
      [ManyToOne]
      public virtual ClassD ClassD { get; set; }
  
      public virtual bool Equals(ClassB other)
      {
          if (ReferenceEquals(null, other))
          {
              return false;
          }
          if (ReferenceEquals(this, other))
          {
              return true;
          }
          return Equals(other.ClassC, ClassC) &amp;&amp; Equals(other.ClassD, ClassD);
      }
  }
  </code></pre>
  
  <p>Implementation of <code>GetHashCode</code> and <code>Equals(object)</code> have been omitted for brevity.</p>
  
  <p>What strategies can be been used to tackle this issue?</p>