0

I have a situation where I have a Common.Domain.Person and Specific.Domain.Person.

First one should be provided as a part of a common package.
Second one appears when common package has to be customized to fit the needs of specific project.

In the object model, it can be easily implemented with inheritance.
In the NH mapping, however, I have encountered a small problem.

I can create an NHibernate <subclass> mapping, but that would require me to use an discriminator. However, I know that if specific person class was inherited, then common class instances will never be used within this specific project.

What is the best way to implement this without adding discriminator column to the base class (since there are no different cases to discriminate)?

Andrey Shchekin
  • 21,101
  • 19
  • 94
  • 162
  • You can map inheritance on 3 different ways in NHibernate. You will need some way to tell to which class you want to map from the database. How do you like to make the difference between the 2 classes in the database? – Paco May 25 '10 at 18:44
  • In the described case, there never will be more than one class in this table, so the difference is not present. – Andrey Shchekin May 25 '10 at 18:50
  • Than I don't understand the question. I don't get why you want inheritance when the base class will never be used. With this description I would create only one of the 2 classes. – Paco May 25 '10 at 19:12
  • One class is in the common assembly provided by one vendor, other class is created only if customizations are needed by the people using this assembly (who can't change it). – Andrey Shchekin May 26 '10 at 06:19

2 Answers2

1

this is what i wanted and nhibernate supports it using xml entities. Unfortunately this feature has been borked since (at least) NH v2++.

see also Using Doctype in Nhibernate

A work-around could be to inject these properies programmaticaly when you create the SessionFactory (Dynamic Mapping)

see also http://ayende.com/Blog/archive/2008/05/01/Dynamic-Mapping-with-NHibernate.aspx

Community
  • 1
  • 1
Jaguar
  • 5,929
  • 34
  • 48
0

Just map the Specific.Domain.Person and leave Common.Domain.Person unmapped.

If you are not saving instances of it, NHibernate does not need to know about it.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • This will require users of the common assembly to repeat the mapping even if they do not override the class. And if they override it, if the base class has 8 properties and overridden has 1, it seems redundant and error-prone to make them map 8 original properties again. – Andrey Shchekin May 26 '10 at 06:22
  • Error prone?? Just use the SAME hbm and change the file name. – Diego Mijelshon May 26 '10 at 11:47
  • I see you point, but there are some problems with that. What about links from all other entities that reference base class (but should be loaded as a new implementation)? So you basically suggest that common library should be shipped with a set of separate hbm files that users have to modify y hand? In this case, adding any new properties to base entities will require more complex migration process. – Andrey Shchekin May 26 '10 at 12:44