public virtual TimeSpan ActualTimeSpan { get; set; }
class TimeSpanUserType : ImmutableUserType
{
public override object NullSafeGet(IDataReader rs, string[] names, object owner)
{
// Need to do some formatting of TimeSpanTest before it can be parsed
return TimeSpan.Parse((string)rs[names[0]]);
}
public override void NullSafeSet(IDbCommand cmd, object value, int index)
{
var timespan = (TimeSpan)value;
var duration = timespan.Duration();
var timeSpanstring = string.Format("{0}{1} {2}:{3}:{4}.{5}",
(timespan.Ticks < 0) ? "-" : "+",
duration.Days.ToString().PadLeft(2, '0'),
duration.Hours.ToString().PadLeft(2, '0'),
duration.Minutes.ToString().PadLeft(2, '0'),
duration.Seconds.ToString().PadLeft(2, '0'),
duration.Milliseconds.ToString().PadLeft(3, '0').PadRight(6, '0'));
NHibernateUtil.String.NullSafeSet(cmd, timeSpanstring, index);
}
public override Type ReturnedType
{
get { return typeof(TimeSpan); }
}
public override SqlType[] SqlTypes
{
get { return new[] { SqlTypeFactory.GetString(8) }; }
}
}
<property name="ActualTimeSpan" column="TIMESPAN_TEST" type="TimeSpanUserType"/>
Редактиране:добавен immutableUserType
public abstract class ImmutableUserType : IUserType
{
public new virtual bool Equals(object x, object y)
{
return object.Equals(x, y);
}
public virtual int GetHashCode(object x)
{
return (x == null) ? 0 : x.GetHashCode();
}
public override bool IsMutable
{
get { return false; }
}
public override object DeepCopy(object value)
{
return value;
}
public override object Replace(object original, object target, object owner)
{
return original;
}
public override object Assemble(object cached, object owner)
{
return cached;
}
public override object Disassemble(object value)
{
return value;
}
public abstract object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner);
public abstract void NullSafeSet(System.Data.IDbCommand cmd, object value, int index);
public abstract Type ReturnedType { get; }
public abstract SqlType[] SqlTypes { get; }
}