2010-03-22 64 views
2

我当前的项目我在C#中使用Castle的ActiveRecord。对于我的一个表,我确实需要使用自定义类型类(处理时间间隔转换的愚蠢时间)。为了保持我的代码清洁,我喜欢在对象映射类中定义派生自IUserType的类。但是,我找不到任何方式使用此子类此属性映射,ActiveRecord的不断抱怨:Could not determine type for (...)Castle ActiveRecord:映射到IUserType wihtin类C#

这里是一个小例子:

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

回答

1

由于StupidDBTimeSpan是一个内部类的CLR类型名称是:

testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord 
+0

就解决了!非常感谢! 现在你能告诉我一个链接提供所有的文件,将回答这些问题对我来说?因为我找不到任何完整的文档... – sc911 2010-03-22 13:55:35

+0

@ sc911:这是相当完整的...:http://msdn.microsoft.com/en-us/library/yfsftwz6.aspx - 否则,一个'typeof(xxx).FullName'应该给你正确的类型名称。 – Lucero 2010-03-22 14:06:29