2010-03-22 3 views
2

내 현재 프로젝트에 대해 C#에서 Castle의 ActiveRecord를 사용하고 있습니다. 내 테이블 중 하나에 대해서는 사용자 정의 유형 클래스를 사용해야합니다 (시간 전환 변환에 어리석은 시간 처리). 내 코드를 깨끗하게 유지하려면 객체 매핑 클래스에서 IUserType에서 파생 된 클래스를 정의하고 싶습니다.성 ActiveRecord :지도에서 IUserType으로 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 클래스 외부에 정의 된 경우 아무 문제가 없습니다 : 여기 Could not determine type for (...)

작은 샘플입니다 :하지만이 서브 클래스를 사용하여이 속성을 매핑 할 수있는 방법을 찾을 수 있습니다, 액티브는 불평이 계속 그리고 [Property(ColumnType = "testForActiveRecord.StupidDBTimeSpan, testForActiveRecord")]을 사용하여 속성을 매핑하고 있습니다.

내가 뭘 잘못하고 있니? 이 subclass-construct를 ActiveRecord와 함께 사용할 수 있습니까?

안부 sc911

답변

1

StupidDBTimeSpan는 CLR 유형 이름이 내부 클래스이므로 : 그것을 해결

testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord 
+0

! 고마워요! 이제 나에게 해당 질문에 답변 해줄 모든 문서를 제공하는 링크를 알려주시겠습니까? 왜냐하면 나는 이것에 대한 완벽한 문서를 찾을 수 없었기 때문입니다 ... – sc911

+0

@ sc911 : 이것은 아주 완성되었습니다 : http://msdn.microsoft.com/en-us/library/yfsftwz6.aspx - 그렇지 않으면 a 'typeof (xxx) .FullName'은 올바른 타입 이름을 알려줍니다. – Lucero

관련 문제