【发布时间】:2010-03-22 13:33:49
【问题描述】:
对于我当前的项目,我在 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
【问题讨论】:
标签: c# mapping castle-activerecord