While trying to solve my problem i have searched for a solution with the help of google. How to create an IUserType has been documented very well and it was very easy to convert a DateTime object to UTC. But the mapping to TimestampType was little difficult.
After little search within the source code of NHibernate I looked over NHibernateUtil class that provides public propertys for the internal NHibernate type constructors (the internal types like TimestampType do not provide public constructor). So I decided to convert my UTC DateTime to TimestampTime.
The following code will show my implementation.
Code:
using System;
using System.Data;
using NHibernate;
using NHibernate.SqlTypes;
namespace MyCompany.Project.Package
{
public class MyCustomUtcTimestamp : IUserType
{
public new bool Equals(object x, object y)
{ ... }
public SqlType[] SqlTypes
{ get { return {new DateTimeSqlType()}; } }
public Type ReturnedType
{ get { return typeof (DateTime); } }
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{ ... }
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (((DateTime) value).Equals(default(DateTime)))
{
((IDataParameter) cmd.Parameters[index]).Value = DBNull.Value;
return;
}
((IDataParameter) cmd.Parameters[index]).Value =
NHibernateUtil.Timestamp.DeepCopy(((DateTime) value).ToUniversalTime());
}
public object DeepCopy(object value)
{ return value; }
public bool IsMutable
{ get { return true; } }
}
}
Have fun
neo