0

我有一个自定义集合,它包装了一个 .net HashSet 并实现了 ICollection,然后我将其映射为一个集合。我这样做是希望 NHib 能够像使用 .net HashSet 时那样使用 ICollection 接口,而不是 NHib 在内部使用的 Iesi 接口。

保存到数据库似乎工作正常,但我在补水时遇到的异常让我知道我需要做更多:

Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericSet`1[ContactMechanisms.Domain.Emails.Email]' to type 'ContactMechanisms.Domain.Emails.EmailCollection'.

这些文章经常被引用为处理自定义集合处理的方法,但链接已损坏,我可以看到更多关于使用扩展查询集合的内容。

我必须使用 IUserCollectionType 吗?如果是这样,任何人都有显示示例实现的链接?我在当前的代码/映射中有什么愚蠢的事情吗?

什么是好的解决方案?

CODE(父实体片段)

public class Contact : RoleEntity<Party>, IHaveContactMechanisms
{       
    private readonly ICollection<Email> _emails = new EmailCollection();

    public virtual EmailCollection Emails { get { return (EmailCollection) _emails; } }
}

CODE(自定义集合片段)

 public class EmailCollection : ContactMechanismSet<Email> { ....  }

 public class ContactMechanismSet<T> : ValueObject, ICollection<T> where T : ContactMechanism
{
    private readonly HashSet<T> _set = new HashSet<T>();
}

MAPPING(hbm值类型集合)

<set name ="_emails" table="Emails" access="field">
  <key column="ContactId"></key>
  <composite-element class="ContactMechanisms.Domain.Emails.Email, ContactMechanisms.Domain">
    <property name="IsPrimary" />
    <property name="Address" length="100"/>
    <property name="DisplayName" length="50"/>
  </composite-element>
</set>

* 更新 *

所以在我的对象设置器中执行以下操作,但是 - 我可以做得更好吗?

public virtual EmailCollection Emails {
    get {
        if (!(_emails is EmailCollection )) {
            // NHibernate is giving us an enumerable collection
            // of emails but doesn't know how to turn that into
            // the custom collection we really want
            //
            _emails = new EmailCollection (_emails );
        }
        return (EmailCollection ) _emails ;
    }
}
4

1 回答 1

2

if EmailCollection has a parameterless constructor then the following should be possible with newer NH ootb and older with registering CollectionTypeFactory which handles .NET ISet (can be found in internet)

public class Contact : RoleEntity<Party>, IHaveContactMechanisms
{       
    private EmailCollection _emails = new EmailCollection();

    public virtual EmailCollection Emails { get { return _emails; } }
}

public class ContactMechanismSet<T> : ValueObject, ICollection<T> where T : ContactMechanism
{
    ctor()
    {
        InternalSet = new HashSet<T>();
    }

    private ISet<T> InternalSet { get; set; }
}

<component name="_emails">
  <set name="InternalSet" table="Emails">
    <key column="ContactId"></key>
    <composite-element class="ContactMechanisms.Domain.Emails.Email, ContactMechanisms.Domain">
      <property name="IsPrimary" />
      <property name="Address" length="100"/>
      <property name="DisplayName" length="50"/>
    </composite-element>
  </set>
</component>
于 2013-07-24T11:26:05.297 回答