NHibernate Custom Collection Type

I have an entity object called Patient , and this object has a property called Visits , which is of type VisitsCollection .

VisitsCollections is a child of IList<Visit> , but it also adds some user logic to the collection (for example, automatic ordering, some checks, notifications, etc.).

I need to use a custom collection type as it adds some data to objects that are added to the collection and transparently executes some other documents.

Now I want to display this in NHibernate, so I created:

 <list name="Visits" lazy="true" fetch="select"> <key foreign-key="PatientId" /> <index column="Timestamp" /> <one-to-many class="Visit" not-found="ignore"/> </list> 

I get an exception:

Cannot apply an object of type "NHibernate.Collection.PersistentList" to type "... VisitsCollection"

Whenever I access the visits property.

I also tried to display it like this:

 <list name="Visits" lazy="true" fetch="select" collection-type="VisitsCollection"> <key foreign-key="PatientId" /> <index column="Timestamp" /> <one-to-many class="Visit" not-found="ignore"/> </list> 

but still I get this exception:

User type does not implement UserCollectionType: ..... VisitsCollection

I don’t want to inherit my VisitsCollection from any type of NHibernate, because the collection class is part of the structure in which I want it to be a DAL agnostic (since it will be used in many scenarios - not just using the database).

Any ideas on how to map this while maintaining the structure of my code?

Thanks in advance.

+7
collections nhibernate nhibernate-mapping
source share
3 answers

I never use custom collection types, mainly because I'm lazy. NHibernate wants you to use IUserCollectionType, which I think requires a bit of plumbing.

Instead, my first stop will be to use extension methods like those discussed by Billy McCafferty . But you have code written like this ...

Alternatively, you can display your collection as a component as the Colin Jack discussed here . Could this be easier for your scenario?

Also check out this SO stream .

+6
source share

I will also vote not to use custom collections. In any case, you can do this through the component.

 <component name="Warehouses" class="Core.Domain.Collections.EntitySet`1[Core.Domain.OrgStructure.IWarehouseEntity,Core],Core"> <set name="_internalCollection" table="`WAREHOUSE`" cascade="save-update" access="field" generic="true" lazy="true" > <key column="`WarehouseOrgId`" foreign-key="FK_OrgWarehouse" /> <!--This is used to set the type of the collection items--> <one-to-many class="Domain.Model.OrgStructure.WarehouseEntity,Domain"/> </set> 

How to map a custom NHibernate collection to fluentNHibernate?

+1
source share

Just for reference, here is how you could do it using FluentNHibernate

Whether we need to create or create a custom collection type, this is a separate section of IMHO

 public class PatientOverride : IAutoMappingOverride<Patient> { public void Override(AutoMapping<Patient> mapping) { mapping.Component( x => x.Visits, part => { part.HasMany(Reveal.Member<VisitsCollection, IEnumerable<Visit>>("backingFieldName")) // this is the backing field name for collection inside the VisitsCollection class .KeyColumn("PatientId") .Inverse(); // depends on your use case whether you need it or not }); } } 
0
source share

All Articles