title: Unit testing NHibernate UserTypes
body: <p>Does anyone have a good approach towards unit testing their UserTypes?</p>
  
  <p>By way of example, I have an object in my model called DateRange, which has a DatePoint start and DatePoint end. In addition to making range type operations available for two DateTimes, these objects let me adjust the precision for the task at hand (i.e., Day, Hour, Minute, etc.). When stored to the db for an application I'm working on, I just need to store the start and end as DateTime, no nulls allowed. I can't think of how to map this without a UserType, so I have:</p>
  
  <pre><code>/// &lt;summary&gt;User type to deal with &lt;see cref="DateRange"/&gt; persistence for time sheet tracking.&lt;/summary&gt;
  public class TimePeriodType : IUserType
  {
  
      public SqlType[] SqlTypes {
          get {
              var types = new SqlType[2];
              types[0] = new SqlType(DbType.DateTime);
              types[1] = new SqlType(DbType.DateTime);
              return types;  
  
          }
      }
  
      public Type ReturnedType
      {
          get { return typeof(DateRange); }
      }
  
      /// &lt;summary&gt;Just return &lt;see cref="DateRange.Equals(object)"/&gt;&lt;/summary&gt;
      public new bool Equals(object x, object y)
      {
          return x != null &amp;&amp; x.Equals(y);
      }
  
      /// &lt;summary&gt;Just return &lt;see cref="DateRange.GetHashCode"/&gt;&lt;/summary&gt;
      public int GetHashCode(object x)
      {
          return x.GetHashCode();
      }
  
      public object NullSafeGet(IDataReader rs, string[] names, object owner)
      {
          var start = (DateTime)NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
          var end = (DateTime)NHibernateUtil.DateTime.NullSafeGet(rs, names[1]);
  
          return new DateRange(start, end, TimeSlice.Minute);
      }
  
      public void NullSafeSet(IDbCommand cmd, object value, int index) {
          Check.RequireNotNull&lt;DateRange&gt;(value);
          Check.RequireArgType&lt;DateRange&gt;(value);
          var dateRange = ((DateRange)value);
  
          NHibernateUtil.DateTime.NullSafeSet(cmd, dateRange.Start, index);
          NHibernateUtil.DateTime.NullSafeSet(cmd, dateRange.End, index);
      }
  
      public object DeepCopy(object value) {
          Check.RequireNotNull&lt;DateRange&gt;(value);
          Check.RequireArgType&lt;DateRange&gt;(value);
          var dateRange = ((DateRange) value);
  
          return new DateRange(dateRange.Start, dateRange.End);
      }
  
      public bool IsMutable
      {
          get { return false; }
      }
  
      public object Replace(object original, object target, object owner) {
          //because it is immutable so we can just return it as is  
          return original;
      }
  
      public object Assemble(object cached, object owner) {
          //Used for caching, as it is immutable we can just return it as is  
          return cached;
      }
  
      public object Disassemble(object value) {
          //Used for caching, as it is immutable we can just return it as is  
          return value;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now I'm looking for a way to prove it works. Thanks in advance!</p>
  
  <p>Cheers,
  Berryl</p>