nhibernate自定义集合处理

Ber*_*ryl 3 collections nhibernate nhibernate-mapping

我有一个工作的一对多关系(非bbidirectional),其中Resource有一组实现的许多Allocations,如下所示.域需要通过AddAllocation,RemoveAllocation等管理它的分配集合做更多的事情.所以从对象的角度来看,我想把那些非持久性的额外逻辑放到另一个类,AllocationCollection和使这个额外的类对NHib透明.

我还想以TDD的方式充实AllocationCollection的响应能力,但我不确定如何重构现有的类,因此NHib仍然有效,映射明智.你会怎么做?

干杯,Berryl

模型

public class Resource {

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

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>
Run Code Online (Sandbox Code Playgroud)

Jam*_*Ide 6

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