开发者

Castle ActiveRecord: Map to IUserType wihtin Class in C#

开发者 https://www.devze.com 2022-12-23 23:04 出处:网络
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

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[] n开发者_开发问答ames, 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


Since StupidDBTimeSpan is an inner class the CLR type name is:

testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号