对于我的当前项目,我在C#中使用Castle的ActiveRecord。对于我的一个表,我确实需要使用一个自定义类型类(处理一个愚蠢的time to timespan转换)。为了保持代码的整洁,我喜欢在对象映射类中定义从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
}
}
}如果在testForActiveRecord类外部定义StupidDBTimeSpan类,并且我使用[Property(ColumnType = "testForActiveRecord.StupidDBTimeSpan, testForActiveRecord")]映射该属性,则不会有任何问题。
我做错了什么?可以在ActiveRecord中使用这个子类结构吗?
关于sc911
发布于 2010-03-22 21:42:10
因为StupidDBTimeSpan是一个内部类,所以它的类型名是:
testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecordhttps://stackoverflow.com/questions/2492555
复制相似问题