Skip to content

Instantly share code, notes, and snippets.

@chaliy
Created August 26, 2010 18:26
Show Gist options
  • Select an option

  • Save chaliy/551910 to your computer and use it in GitHub Desktop.

Select an option

Save chaliy/551910 to your computer and use it in GitHub Desktop.
IUserType for SQL datetime2
using System;
using System.Data;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;
namespace Infrastructure.Data.NHibernate.UserTypes
{
public class DateTime2UserType : IUserType
{
#region Equals member
bool IUserType.Equals(object x, object y)
{
if (x == null) return false;
return x.Equals(y);
}
#endregion
#region IUserType Members
public object Assemble(object cached, object owner)
{
return cached;
}
public object DeepCopy(object value)
{
return value == null ? (object) null : new DateTime(((DateTime)value).Ticks);
}
public object Disassemble(object value)
{
return value;
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public bool IsMutable
{
get { return false; }
}
public object NullSafeGet(IDataReader reader, string[] names, object owner)
{
var index = reader.GetOrdinal(names[0]);
if (reader.IsDBNull(index))
{
return null;
}
return (DateTime)reader[index];
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null || value == DBNull.Value)
{
NHibernateUtil.DateTime2.NullSafeSet(cmd, null, index);
}
else
{
NHibernateUtil.DateTime2.Set(cmd, value, index);
}
}
public object Replace(object original, object target, object owner)
{
return original;
}
public Type ReturnedType
{
get { return typeof(DateTime); }
}
public SqlType[] SqlTypes
{
get { return new[] { new SqlType(DbType.DateTime2) }; }
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment