Castle ActiveRecord:在C#中映射到IUserType wihtin类 [英] Castle ActiveRecord: Map to IUserType wihtin Class in C#

查看:78
本文介绍了Castle ActiveRecord:在C#中映射到IUserType wihtin类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我当前的项目,我在C#中使用Castle的ActiveRecord.对于我的一张表,我确实需要使用自定义类型类(处理愚蠢的时间到时间跨度转换).为了使我的代码保持整洁,我想在对象映射类中定义从IUserType派生的类.但是我找不到使用此子类映射此属性的方法,ActiveRecord一直抱怨:Could not determine type for (...)

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 (...)

这是一个小样本:

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")]映射属性,则没有问题.

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")].

我做错了什么?可以在ActiveRecord中使用此子类构造吗?

What am I doing wrong? Is it possible to use this subclass-construct with ActiveRecord?

问候 sc911

推荐答案

由于StupidDBTimeSpan是内部类,因此CLR类型名称为:

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

testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord

这篇关于Castle ActiveRecord:在C#中映射到IUserType wihtin类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆