问题
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
回答1:
Since StupidDBTimeSpan is an inner class the CLR type name is:
testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord
来源:https://stackoverflow.com/questions/2492555/castle-activerecord-map-to-iusertype-wihtin-class-in-c-sharp