2010-03-22 63 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