title: NHibernate custom collection type
body: <p>I'm having an entity object called <code>Patient</code> and this entity is having a property called <code>Visits</code> which is of type <code>VisitsCollection</code>.</p>
  
  <p><code>VisitsCollections</code> is a child class of <code>IList&lt;Visit&gt;</code> but it also adds some custom logic to the collection (like auto ordering, some validation, notifications, etc..).</p>
  
  <p>I <strong>need</strong> to use the custom collection type as it adds some data to the entities that are added to the collection and performs some other paperwork transparently.</p>
  
  <p>Now I want to map that in NHibernate, so I've created:</p>
  
  <pre><code>&lt;list name="Visits" lazy="true" fetch="select"&gt;
      &lt;key foreign-key="PatientId" /&gt;
      &lt;index column="Timestamp" /&gt;
      &lt;one-to-many class="Visit" not-found="ignore"/&gt;
  &lt;/list&gt;
  </code></pre>
  
  <p>I'm getting an exception:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'NHibernate.Collection.PersistentList' to type '...VisitsCollection'</p>
  </blockquote>
  
  <p>Whenever I'm accessing the visits property.</p>
  
  <p>I've also tried to map it this way:</p>
  
  <pre><code>&lt;list name="Visits" lazy="true" fetch="select" collection-type="VisitsCollection"&gt;
      &lt;key foreign-key="PatientId" /&gt;
      &lt;index column="Timestamp" /&gt;
      &lt;one-to-many class="Visit" not-found="ignore"/&gt;
  &lt;/list&gt;
  </code></pre>
  
  <p>but still, I'm getting this exception:</p>
  
  <blockquote>
    <p>Custom type does not implement UserCollectionType: .....VisitsCollection</p>
  </blockquote>
  
  <p>I don't want to inherit my <code>VisitsCollection</code> from any NHibernate type as the collection class is part of a framework that I want it to be DAL-agnostic (as it will be used in many scenarios - not only with a database).</p>
  
  <p>Any ideas on how to map this, preserving the structure of my code?</p>
  
  <p>Thanks in advance.</p>