nhibernate自定义集合处理 [英] nhibernate custom collection handling

查看:169
本文介绍了nhibernate自定义集合处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的一对多的关系(不是双向的)其中Resource有一组许多分配,如下所示。域需要做更多的分配的集合,只是管理它与添加分配,RemoveAllocation等等。所以从对象的角度来看,我想把那些额外的逻辑,不持久性相关到一个不同的类,AllocationCollection,和让这个额外的类对NHib透明。

I have a working one to many relationship (NOT bbidirectional) where Resource has a set of many Allocations implented as shown below. The domain needs to do more with the collection of allocations that just manage it with AddAllocation, RemoveAllocation, etc. So from an object perspective, I'd like to put that extra logic that is not persistent related into a different class, AllocationCollection, and make that extra class transparent to NHib.

我也想以TDD的方式显示AllocationCollection的responibility,但是我不知道如何重构现有的类,所以NHib仍然工作,映射明智。您将如何做?

I'd also like to flesh out the responibilities of the AllocationCollection in a TDD manner, but I'm not sure how to refactor the existing class so NHib still works, mapping wise. How would you do that?

干杯,
Berryl

Cheers, Berryl

MODEL

public class Resource {

    public virtual ICollection<Allocation> Allocations
    {
        get { return _allocations ?? (_allocations = new HashSet<Allocation>()); }
        private set { _allocations = value; } // Nhib will use this
    }
}

MAPPING

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" ...
<class xmlns="urn:nhibernate-mapping-2.2" name="Domain.Model.Resources.Resource, ... table="Resources">
....
<set cascade="all-delete-orphan" name="Allocations">
  <key foreign-key="Allocations_Resource_FK">
    <column name="ResourceId" />
  </key>
  <one-to-many class="Model.Allocations.Allocation, ... />
</set>


推荐答案

Billy McCafferty有关于在NHibernate中使用自定义集合的优秀系列文章。我个人不再使用自定义集合类型。我使用AddMyType,RemoveMyType等方法控制包含集合(即聚合根)的类的集合访问。我公开集合为 IEnumerable< MyType> 。我用 IEnumerable< MyType> 替换了其他自定义集合访问器的扩展方法。

Billy McCafferty has an excellent series of articles on working with custom collections in NHibernate. Personally I no longer use custom collection types. I control collection access from the class containing the collection (i.e. aggregate root) with AddMyType, RemoveMyType, etc., methods. I expose the collection as IEnumerable<MyType>. I've replaced other custom collection accessors with extension methods on IEnumerable<MyType>.

这篇关于nhibernate自定义集合处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆