Thursday, November 28, 2013

NHibernate - How to map interfaces


Problem

If you try to map interface with property that have no setter you will get NHibernate.PropertyNotFoundException - Could not find a setter for property 'property-name' in class 'class-name' on configuration.BuildSessionFactory().

Salvation

Let's look on the following interfaces


To map them I create IIdentified<TId> interface and my implementation classes of IDeliverySchedule and IDeliveryPattern will implement it too. So we have Id setter only in implementations, not in the model interfaces.

Now I map IIdentified<DeliveryScheduleId> but name it IDeliverySchedule.

The same trick for IDeliveryPattern

Then finally we map concrete subclasses

And it magically works.
Take attention that in <one-to-many> and <many-to-one> associations we reference entity-name, not a class.
See full working sample on GitHub

Happy Hibernating