David M's suggestion and article link were great. Just in case, if someone has problems getting this site (I had to download it from the Google cache), here is my solution:
public abstract class BaseImmutableUserType : IUserType
{
public abstract object NullSafeGet(IDataReader rs, string[] names, object owner);
public abstract void NullSafeSet(IDbCommand cmd, object value, int index);
public abstract SqlType[] SqlTypes { get; }
public new bool Equals(object x, object y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (x == null || y == null)
{
return false;
}
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return DeepCopy(cached);
}
public object Disassemble(object value)
{
return DeepCopy(value);
}
public Type ReturnedType
{
get { return typeof(T); }
}
public bool IsMutable
{
get { return false; }
}
}
public class VersionUserType: BaseImmutableUserType
{
public override SqlType[] SqlTypes
{
get
{
return new SqlType[]{new StringSqlType()};
}
}
public override object NullSafeGet(IDataReader rs, string[] names, object owner)
{
Version version = null;
var value = NHibernateUtil.String.NullSafeGet(rs, names) as string;
if (value != null)
version = new Version(value);
return version;
}
public override void NullSafeSet(IDbCommand cmd, object value, int index)
{
object valueToSet;
Version version = value as Version;
if (version != null)
{
valueToSet = version.ToString();
}
else
{
valueToSet = DBNull.Value;
}
NHibernateUtil.String.NullSafeSet(cmd, valueToSet, index);
}
}
and the mapping file now has this property:
<property name = "MyVersion" column = "MyVersion" not-null = "true" type = "My.Namespace.VersionUserType, My.Assembly" />
source
share