Handling a custom nhibernate collection

I have a working relationship with many (NOT bbidirectional), where the Resource has a set of many Allocations implanted, as shown below. A domain should do more with a collection of distributions that simply manage it with AddAllocation, RemoveAllocation, etc. Therefore, from the point of view of the object, I would like to add this additional logic, which is not constant, associated with another AllocationCollection class and make this additional class transparent to NHib.

I would also like to describe the capabilities of the AllocationCollection in a TDD manner, but I'm not sure how to refactor an existing class so that NHib still works, displaying the wise. How do you do this?

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 } } 

DISPLAY

 <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> 

+3
collections nhibernate nhibernate-mapping
source share
1 answer

Billy McCafferty has an excellent series of articles about working with custom collections at NHibernate. Personally, I no longer use custom collection types. I control access to the collection from the class containing the assembly (i.e., the Aggregate Root) using the AddMyType, RemoveMyType, etc. methods. I expose the collection as IEnumerable<MyType> . I replaced other custom collector accessors using extension methods with IEnumerable<MyType> .

+6
source share

All Articles