相关疑难解决方法(0)

nhibernate自定义集合处理

我有一个工作的一对多关系(非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)

collections nhibernate nhibernate-mapping

3
推荐指数
1
解决办法
1876
查看次数