如何将 NHibernate 与 System.Tuple 等不可变类型一起使用?

2024-04-18

我有一个复合映射使用System.Tuple<int,string> http://msdn.microsoft.com/en-us/library/dd268536.aspx看起来如下:

<composite-element
  class="System.Tuple`2[[System.Int32, mscorlib],[System.String, mscorlib]], mscorlib">
    <property name="Item1" column="DBColumn1"/>
    <property name="Item2" column="DBColumn2"/>
</composite-element>

我尝试搞乱BytecodeProvider, IObjectsFactory, ReflectionOptimizer等等,但我无法让 NHibernate 正确加载我的元组(无论我做什么,NHibernate 都坚持先创建对象,然后再填充值)。

NHibernate 是否可以以某种方式强制正确加载和保留不可变类型?


你有没有尝试过ICompositeUserType http://www.nhforge.org/doc/nh/en/index.html#mapping-types-custom?它将允许您像这样定义您的映射Tuple<int, string>财产:

<property name="MyProperty" type="MyNamespace.TupleIntStringType, MyAssembly">
    <column name="Item1"/>
    <column name="Item2"/>
</property>

自定义类型定义为:

public class TupleIntStringType : ICompositeUserType
{
    public object GetPropertyValue(object component, int property)
    {
        var tuple = (Tuple<int, string>)component;
        switch (property)
        {
            case 0:
                return tuple.Item1;
            case 1:
                return tuple.Item2;
            default:
                throw new InvalidOperationException(String.Format("No property number {0} found", property));
        }
    }

    public void SetPropertyValue(object component, int property, object value)
    {
        throw new InvalidOperationException("Immutable, SetPropertyValue is not allowed");
    }

    public new 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 == null ? 0 : x.GetHashCode();
    }

    public object NullSafeGet(IDataReader dr, string[] names, ISessionImplementor session, object owner)
    {
        var item1 = (int)PropertyTypes[0].NullSafeGet(dr, names[0], session, owner);
        var item2 = (String)PropertyTypes[1].NullSafeGet(dr, names[1], session, owner);

        return Tuple.Create(item1, item2);
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index, bool[] settable, ISessionImplementor session)
    {
        if (value == null)
        {
            NHibernateUtil.Timestamp.NullSafeSet(cmd, null, index);
            NHibernateUtil.TimeSpan.NullSafeSet(cmd, null, index + 1);
        }
        else
        {
            var tuple = (Tuple<int, String>)value;

            PropertyTypes[0].NullSafeSet(cmd, tuple.Item1, index, session);
            PropertyTypes[1].NullSafeSet(cmd, tuple.Item2, index + 1, session);
        }
    }

    public object DeepCopy(object value)
    {
        var tuple = (Tuple<int, String>)value;
        return Tuple.Create(tuple.Item1, tuple.Item2);
    }

    public object Disassemble(object value, ISessionImplementor session)
    {
        return DeepCopy(value);
    }

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

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

    public string[] PropertyNames { get { return new[] { "Item1", "Item2" }; } }
    public IType[] PropertyTypes { get { return new IType[] { NHibernateUtil.Int32, NHibernateUtil.String }; } }
    public Type ReturnedClass { get { return typeof(Tuple<int, string>); } }
    public bool IsMutable { get { return false; } }
}

以下是一些例子:
Money 对象和 NHibernate ICompositeUserType http://geekswithblogs.net/opiesblog/archive/2006/08/05/87218.aspx
使用 NHibernate 的 ICompositeUserType 映射时间戳数据 http://lostechies.com/rayhouston/2008/03/24/mapping-timestamp-data-using-nhibernate-s-icompositeusertype/
将 NHibernate ICompositeUserType 与值类型结合使用 https://stackoverflow.com/questions/1189418/using-nhibernate-icompositeusertype-with-a-value-type

您的映射<composite-element>然后更改为(如所示NHIbernate:如何使用 ICompositeUserType 映射包 https://stackoverflow.com/a/8970354/3205 ):

<element type="MyNamespace.TupleIntStringType, MyAssembly">
  <column name="DBColumn1" />
  <column name="DBColumn2" />
</element>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 NHibernate 与 System.Tuple 等不可变类型一起使用? 的相关文章

随机推荐