Mapping abstract class, should common properties be mapped inside abstract or child class

StackOverflow https://stackoverflow.com/questions/23366856

  •  11-07-2023
  •  | 
  •  

Question

I have ArticleBase which is inherited by ArticleComputer and ArticleCar. I'm using nhibernate inheritance mapping with discriminator value. Everything is up and running but I have dilema, since ArticleBase has property Tags should I map this property inside ArticleBaseMap class or I should leave to child classes to map this common properties.

I guess it should be mapped inside abstract base class but I would like to hear from experienced user.

Thanks

Was it helpful?

Solution

The answer here is very straightforward:

  • in case, that the property is available on the ArticleBase Table (where the Discriminator column is) map it there.
  • if the column belongs to child table (e.g. ArticleComputer) then it belongs to the child maping.

So, where the column is there .. should be the mapping

It really does not mean: where it is declared (base, interface). The reason could be, that in C# we do some common stuff in base implementation, while some children (for any reason) could not have this as a part of mapping (it is just virtual)

I.e.: We should map it where the column exists. Nothing else. And then, if we are about to repeatedly mapping it everywhere on all children (because column is declared there):

.. change the design of DB. Move that from children back to parent table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top