对于我当前的项目,我在 C# 中使用 Castle 的 ActiveRecord。对于我的一个表,我确实需要使用自定义类型类(处理愚蠢的时间到时间跨度转换)。为了保持我的代码干净,我喜欢定义派生自的类IUserType
在对象映射类内。但我找不到使用此子类映射此属性的方法,ActiveRecord 不断抱怨:Could not determine type for (...)
这是一个小样本:
namespace testForActiveRecord
{
[ActiveRecord("[firstTable]")]
public class firstTable:ActiveRecordBase<firstTable>
{
private TimeSpan _TStest;
// more private fields and properties here
[Property(ColumnType = "testForActiveRecord.firstTable.StupidDBTimeSpan, testForActiveRecord")]
public TimeSpan TStest
{
get { return _TStest; }
set { _TStest = value; }
}
// Usertype doing the conversion from a date saved in the DB to the timespan it is representing
// The TimeSpan is saved by an offset to the date 30.12.1899...
public class StupidDBTimeSpan : IUserType
{
#region IUserType Member
DateTime Basis = new DateTime(1899,12,30,00,00,00);
object IUserType.Assemble(object cached, object owner)
{
return cached;
}
object IUserType.DeepCopy(object value)
{
return value;
}
object IUserType.Disassemble(object value)
{
return value;
}
bool IUserType.Equals(object x, object y)
{
if (x == y) return true;
if (x == null || y == null) return false;
return x.Equals(y);
}
int IUserType.GetHashCode(object x)
{
return x.GetHashCode();
}
bool IUserType.IsMutable
{
get { return false; }
}
public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
{
object obj = NHibernateUtil.DateTime.NullSafeGet(rs, names[0]);
TimeSpan Differenz = new TimeSpan();
if (obj != null)
{
Differenz = (DateTime)obj - Basis;
}
return Differenz;
}
public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
{
if (value == null)
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
NHibernateUtil.DateTime.NullSafeSet(cmd, Basis + (TimeSpan)value, index);
}
}
object IUserType.Replace(object original, object target, object owner)
{
return original;
}
Type IUserType.ReturnedType
{
get { return typeof(TimeSpan); }
}
NHibernate.SqlTypes.SqlType[] IUserType.SqlTypes
{
get { return new SqlType[] { new SqlType(DbType.DateTime) }; }
}
#endregion
}
}
}
如果StupidDBTimeSpan
类是在外部定义的testForActiveRecord
类,我正在使用映射属性[Property(ColumnType = "testForActiveRecord.StupidDBTimeSpan, testForActiveRecord")]
.
我究竟做错了什么?是否可以将此子类构造与 ActiveRecord 一起使用?
问候
SC911