Further to this question:
Composition over Inheritance - where do extra properties go?
The accepted answer and similar ones answer this nicely. But to take this further, what if the Sales department and Production department wanted to record different information about Sickness and Holiday absences? This could be one solution:
public class Holiday : Absence
{
//Extra fields go here.
}
public class Sickness : Absence
{
//Extra fields go here.
}
public class SalesHoliday : Holiday
{
//Extra fields go here.
}
public class SalesSickness : Sickness
{
//Extra fields go here.
}
public class ProductionHoliday : Sickness
{
//Extra fields go here.
}
public class ProductionSickness : Sickness
{
//Extra fields go here.
}
clearly, this is the start of a class explosion which will only get worse, and therefore should be avoided.
One possible solution would be to use the Decorator Pattern (Gang of Four). This would be ideal but in this hypothetical example, the persistence is with NHibernate. I have looked all over the place for an example of how to map the Decorator pattern in NHibernate and not found anything. My experiments have, and one point, utilised various combinations of subclass mappings, joined-subclass mappings, union-subclass mappings, discriminators, implicit polymorphism and many-to-any mappings, but so far with no satisfactory results. Has anyone cracked this one? An Employee entity would have a collection of absences of any type so polymorphic behavior is a requirement.