首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IUserType ActiveRecord:映射到C#中的Castle类

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

Stack Overflow用户
提问于 2010-03-22 21:33:50
回答 1查看 976关注 0票数 2

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

下面是一个小示例:

代码语言:javascript
复制
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
        }
    }
}

如果在testForActiveRecord类外部定义StupidDBTimeSpan类,并且我使用[Property(ColumnType = "testForActiveRecord.StupidDBTimeSpan, testForActiveRecord")]映射该属性,则不会有任何问题。

我做错了什么?可以在ActiveRecord中使用这个子类结构吗?

关于sc911

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-22 21:42:10

因为StupidDBTimeSpan是一个内部类,所以它的类型名是:

代码语言:javascript
复制
testForActiveRecord.firstTable+StupidDBTimeSpan, testForActiveRecord
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2492555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档