Castle ActiveRecord:映射到 C# 中的类中的 IUserType
对于我当前的项目,我在 C# 中使用 Castle 的 ActiveRecord。对于我的一个表,我确实需要使用自定义类型类(处理愚蠢的时间到时间跨度转换)。为了保持代码简洁,我喜欢在对象映射类中定义从 IUserType
派生的类。但我无法找到使用此子类映射此属性的方法,ActiveRecord 不断抱怨: 无法确定 (...) 的类型
这是一个小示例:
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
for my current project I am using Castle's ActiveRecord in C#. For one of my tables I do need to use a custom type class (dealing with an stupid time to timespan conversion). To keep my code clean I like to define the class which is derived from IUserType
within the object mapping class. But I can find no way to map this property using this subclass, ActiveRecord keeps complaining: Could not determine type for (...)
Here is a small sample:
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
}
}
}
There is no problem if the StupidDBTimeSpan
class is defined outside the testForActiveRecord
class and I am mapping the property using [Property(ColumnType = "testForActiveRecord.StupidDBTimeSpan, testForActiveRecord")]
.
What am I doing wrong? Is it possible to use this subclass-construct with ActiveRecord?
Regards
sc911
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 StupidDBTimeSpan 是内部类,因此 CLR 类型名称为:
Since StupidDBTimeSpan is an inner class the CLR type name is: