Castle ActiveRecord:映射到 C# 中的类中的 IUserType

发布于 2024-08-26 06:30:40 字数 3396 浏览 2 评论 0原文

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

这是一个小示例:

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

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[] 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
        }
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2024-09-02 06:30:40

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

testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord

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

testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文