NHibernate 中的 SQL 2008 HierarchyID 支持

发布于 2024-09-12 22:21:38 字数 760 浏览 7 评论 0原文

搜索了各种 NHibernate 列表,但没有得出明确的答案。 SQL2008 方言 不会似乎支持 HierarchyID 数据类型 - 仅新日期和时间类型。

有人有良好的实施或有效的解决方法吗?我真的很想在我的新应用程序中利用 HierarchyID。 MS 自己的工具非常缺乏对这种有趣且强大的数据类型的支持,因此对于 NHibernate 没有支持我并不感到惊讶。

一些 我还没有深入研究的方法。想知道是否有人对什么有效、什么性能更高等有一些经验。

全面披露:我正在使用 Castle ActiveRecord,但这似乎是 NHibernate 问题。

Searched various NHibernate lists and haven't come up with a definitive answer. The SQL2008 dialect doesn't appear to have support for the HierarchyID data type - new date and time types only.

Does anyone have a good implementation or an effective workaround? I'd really like to leverage HierarchyID in a new app of mine. Support for this interesting and powerful data type is sorely lacking in MS's own tools so I'm not shocked that NHibernate doesn't have support.

There are some approaches out there that I haven't delved into yet. Wondering if anyone has some experience in what works, what is more performant, etc.'

Full disclosure: I'm working with Castle ActiveRecord but this seems like an NHibernate issue.

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

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

发布评论

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

评论(2

一花一树开 2024-09-19 22:21:38

我已经对 Needles 的答案进行了测试。这是一个非常好的答案,但需要进行一些更改才能使其发挥作用(至少在 .NET 4 中)。以下是我为我的项目提出的方案:

更新:以下代码可以在 GitHub 上下载,并将在那里进行更新。 NHiberntate.HierarchyId.UserType

SqlHierarchyId IUserType

namespace NHibernate.UserTypes
{
    using SqlTypes;
    using System;
    using System.Data;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Types;

    public class HierarchyId : IUserType
    {
        #region Properties

        public SqlType[] SqlTypes
        {
            get { return new[] { NHibernateUtil.String.SqlType }; }
        }

        public Type ReturnedType
        {
            get { return typeof(SqlHierarchyId); }
        }

        public bool IsMutable
        {
            get { return true; }
        }

        #endregion Properties

        #region Methods

        new public bool Equals(object x, object y)
        {
            if (ReferenceEquals(x, y)) return true;
            if (x == null || y == null) return false;

            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);

            if (prop1 == null) return null;

            return SqlHierarchyId.Parse(new SqlString(prop1.ToString()));
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value == null)
                ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;

            else if (value is SqlHierarchyId)
                ((IDataParameter)cmd.Parameters[index]).Value = ((SqlHierarchyId)value).ToString();
        }

        public object DeepCopy(object value)
        {
            if (value == null) return null;

            return SqlHierarchyId.Parse(((SqlHierarchyId)value).ToString());
        }

        public object Replace(object original, object target, object owner)
        {
            return DeepCopy(original);
        }

        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        #endregion Methods
    }
}

映射

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataLayer" namespace="NHibernate.Map">
    <class name="NHibernate.Map.OrganizationUnit, DataLayer" table="`orgunit`">

        <property name="HierarchyId" column="`ou_hid`" type="NHibernate.UserTypes.HierarchyId, DataLayer" />
        ...

    </class>
</hibernate-mapping>

具有 HierarchyId 的对象

namespace NHibernate.Map
{
    using Microsoft.SqlServer.Types;

    public class OrganizationUnit
    {
        #region Fields

        private SqlHierarchyId _hierarchyId;
        ...

        #endregion Fields

        #region Properties

        public virtual SqlHierarchyId HierarchyId
        {
            get { return _hierarchyId; }
            set { _hierarchyId = value; }
        }
        ...

        #endregion Properties
    }
}

I've given Needles' answer a test run. It's a very good answer but there are some changes needed to make it function (at least in .NET 4). Here's what I've come up with for my project:

Update: the following code can be download over at GitHub and will be updated there. NHiberntate.HierarchyId.UserType

SqlHierarchyId IUserType

namespace NHibernate.UserTypes
{
    using SqlTypes;
    using System;
    using System.Data;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Types;

    public class HierarchyId : IUserType
    {
        #region Properties

        public SqlType[] SqlTypes
        {
            get { return new[] { NHibernateUtil.String.SqlType }; }
        }

        public Type ReturnedType
        {
            get { return typeof(SqlHierarchyId); }
        }

        public bool IsMutable
        {
            get { return true; }
        }

        #endregion Properties

        #region Methods

        new public bool Equals(object x, object y)
        {
            if (ReferenceEquals(x, y)) return true;
            if (x == null || y == null) return false;

            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);

            if (prop1 == null) return null;

            return SqlHierarchyId.Parse(new SqlString(prop1.ToString()));
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value == null)
                ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;

            else if (value is SqlHierarchyId)
                ((IDataParameter)cmd.Parameters[index]).Value = ((SqlHierarchyId)value).ToString();
        }

        public object DeepCopy(object value)
        {
            if (value == null) return null;

            return SqlHierarchyId.Parse(((SqlHierarchyId)value).ToString());
        }

        public object Replace(object original, object target, object owner)
        {
            return DeepCopy(original);
        }

        public object Assemble(object cached, object owner)
        {
            return DeepCopy(cached);
        }

        public object Disassemble(object value)
        {
            return DeepCopy(value);
        }

        #endregion Methods
    }
}

Mapping

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataLayer" namespace="NHibernate.Map">
    <class name="NHibernate.Map.OrganizationUnit, DataLayer" table="`orgunit`">

        <property name="HierarchyId" column="`ou_hid`" type="NHibernate.UserTypes.HierarchyId, DataLayer" />
        ...

    </class>
</hibernate-mapping>

Object with HierarchyId

namespace NHibernate.Map
{
    using Microsoft.SqlServer.Types;

    public class OrganizationUnit
    {
        #region Fields

        private SqlHierarchyId _hierarchyId;
        ...

        #endregion Fields

        #region Properties

        public virtual SqlHierarchyId HierarchyId
        {
            get { return _hierarchyId; }
            set { _hierarchyId = value; }
        }
        ...

        #endregion Properties
    }
}
北凤男飞 2024-09-19 22:21:38

免责声明:我不是 NHibernate 专家,但是,我们在即将推出的使用 SQL Server 2008 R2 和层次结构 ID 的项目中将其与 Fluent 一起使用。下面的代码是我们当前在开发环境中使用的代码,尚未经过充分测试/完善。我从其他地方复制了大部分代码(抱歉我丢失了链接!)

您需要创建一个用户定义类型,然后在映射中使用它。下面的映射很流畅,我不知道如何使用 ActiveRecord 来完成它,但我猜它应该是类似的!

用户定义类型

namespace YourNamespace {
    public class SqlHierarchyIdUserType : IUserType {
    public bool Equals(object x, object y) {
        if(ReferenceEquals(x, y))
            return true;

        if(x == null || y == null)
            return false;

        return x.Equals(y);
    }

    public int GetHashCode(object x) {
        return x.GetHashCode();
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner) {
        object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);

        if(prop1 == null)
            return null;

        return SqlHierarchyId.Parse(new SqlString(prop1.ToString()));
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index) {
        if(value == null) {
            ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
        } else {
            if(value is SqlHierarchyId) {
                SqlHierarchyId hId = (SqlHierarchyId)value;
                ((IDataParameter)cmd.Parameters[index]).Value = hId.ToString();
            }
        }
    }

    public object DeepCopy(object value) {
        if(value == null)
            return null;

        var sourceTarget = (SqlHierarchyId)value;
        SqlHierarchyId copy = SqlHierarchyId.Parse(sourceTarget.ToString());

        return copy;

    }

    public object Replace(object original, object target, object owner) {
        return DeepCopy(original);
    }

    public object Assemble(object cached, object owner) {
        return DeepCopy(cached);
    }

    public object Disassemble(object value) {
        return DeepCopy(value);
    }

    public SqlType[] SqlTypes {
        get { return new[] { NHibernateUtil.String.SqlType }; }
    }

    public Type ReturnedType {
        get { return typeof(SqlHierarchyId); }
    }

    public bool IsMutable {
        get { return true; }
    }
}
}

流畅映射

Map(e => e.YourSqlHierarchyIdProperty)
    .Column("YourSqlHierarchyIdFieldName")
    .CustomType<SqlHierarchyIdUserType>();

阅读这篇文章:

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

ActiveRecord 使用 [Property] 属性来映射用户定义类型。所以对你来说它看起来像这样:

public class YourDataObject {
    [Property(ColumnType="YourNamespace.SqlHierarchyIdUserType, YourNamespace")
    public virtual SqlHierarchyId YourSqlHierarchyIdProperty;
}

希望它有帮助!

Disclaimer: Im not an NHibernate expert, however, we are using it with Fluent in an upcoming project which uses SQL Server 2008 R2 and Hierarchy IDs. The code below is what we are using currently on our dev environment and is not fully tested/refined. I copied the majority of the code from elsewhere (sorry I lost the link!)

You need to create a User Defined Type and then use it in your mappings. The mapping below is Fluent, Im not aware how to do it using ActiveRecord but Im guessing it should be similar!

User Defined Type

namespace YourNamespace {
    public class SqlHierarchyIdUserType : IUserType {
    public bool Equals(object x, object y) {
        if(ReferenceEquals(x, y))
            return true;

        if(x == null || y == null)
            return false;

        return x.Equals(y);
    }

    public int GetHashCode(object x) {
        return x.GetHashCode();
    }

    public object NullSafeGet(IDataReader rs, string[] names, object owner) {
        object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);

        if(prop1 == null)
            return null;

        return SqlHierarchyId.Parse(new SqlString(prop1.ToString()));
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index) {
        if(value == null) {
            ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
        } else {
            if(value is SqlHierarchyId) {
                SqlHierarchyId hId = (SqlHierarchyId)value;
                ((IDataParameter)cmd.Parameters[index]).Value = hId.ToString();
            }
        }
    }

    public object DeepCopy(object value) {
        if(value == null)
            return null;

        var sourceTarget = (SqlHierarchyId)value;
        SqlHierarchyId copy = SqlHierarchyId.Parse(sourceTarget.ToString());

        return copy;

    }

    public object Replace(object original, object target, object owner) {
        return DeepCopy(original);
    }

    public object Assemble(object cached, object owner) {
        return DeepCopy(cached);
    }

    public object Disassemble(object value) {
        return DeepCopy(value);
    }

    public SqlType[] SqlTypes {
        get { return new[] { NHibernateUtil.String.SqlType }; }
    }

    public Type ReturnedType {
        get { return typeof(SqlHierarchyId); }
    }

    public bool IsMutable {
        get { return true; }
    }
}
}

Fluent Mapping

Map(e => e.YourSqlHierarchyIdProperty)
    .Column("YourSqlHierarchyIdFieldName")
    .CustomType<SqlHierarchyIdUserType>();

Reading this post:

Castle ActiveRecord: Map to IUserType wihtin Class in C#

ActiveRecord uses a [Property] attribute to map User Defined Types. So for you it would look something like this:

public class YourDataObject {
    [Property(ColumnType="YourNamespace.SqlHierarchyIdUserType, YourNamespace")
    public virtual SqlHierarchyId YourSqlHierarchyIdProperty;
}

Hope it helps!

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