NHibernate的:如何映射袋的ICompositeUserTypeNHibernate、ICompositeUserType

2023-09-04 00:22:09 作者:入梦毁心

假设我有一类不可变类型的集合,我想用一个自定义的ICompositeUserType地图

Suppose I have a class with a collection of immutable types, that I would like to map with a custom ICompositeUserType

public class Foo
{
    public long Id { get; protected set; }

    public virtual IList<Bar> Bars { get; set; }
}

//immutable
public class Bar
{
    private _blah;
    private _halb;

    public Bar(string blah, string halb)
    {
        _blah = blah; _halb = halb;
    }

    public Blah { get { return _blah; } }
    public Halb { get { return _halb; } }
}

我如何设立富袋子的映射使用复合用户类型,从而使自定义类型可以调用构造函数时,有必要吗?

How do I set up the mapping of the bag on Foo to use a composite user type, so that the custom type can invoke the constructor when necessary?

我想这一点,但类型不可用复合元素。

I tried this, but type isn't available on composite-element.

<bag name="Bars" table="FooBars" lazy="true">
   <key column="FooId" foreign-key="FK_Bars_FooId" />
   <composite-element class="Doman.Bar, Domain" type="Integration.UserTypes.BarCompositeUserType, Integration">
       <property name="Blah">
          <column name ="BarBlah" sql-type="char(2)"/>
       </property>
   </composite-element>
</bag>

然而,这是行不通的,因为NHibernate的不承认组合元素

However, that won't work because NHibernate does not recognize the "type" attribute on "composite-element"

如何映射了袋的ICompositeUserType? (我使用NHibernate 3.2)

How to I map a bag with an ICompositeUserType? (I am using NHibernate 3.2)

推荐答案

由于属性和转换的ICompositeUserType内指定的元素,而不是组合元素

element instead of composite-element because the properties and conversion is specified inside the ICompositeUserType

<element type="Integration.UserTypes.BarCompositeUserType, Integration">
  <column name="BarBlah" />
  <column name="BarHalb" />
</element>