Definitely, this is a data model, not a domain model.
To get started to DDD you should read the blue book a couple of times.
Here a few problems of the code you posted:
- Entities should be identified by shared identifiers, not Int32.
- The identity of an entity can't change: it can expose a readonly property, but not a mutable one
- Entities should be properly encapsulated: their state should change only as an effect of commands, but you are exposing it through properties
- Bounded contexts are key concepts in DDD: you should ponder with the domain expert if
Employees
and Retirements
belong to the same one.
- If
Employees
and Retirements
belong to the same bounded context, you should relate them via identifiers, not direct object reference.
- Each entity should hold only that state strictly required to enforce its invariants (those relevant within the context boundaries).
So, to answer to your questions:
- Does Employee entity really need a list of Retirement ??
No. It could need to expose an immutable set of Retirement (eg and IEnumerable<Retirement>
), if it has commands like void Retire(DateTime from, DateTime to)
(obviously with proper exceptions). However, if this case Retirement would be a value object, not an entity; something like:
public sealed Retirement {
public Retirement (DateTime begining, DateTime end)
{
Begining = begining;
End = end;
}
public DateTime Begining {get; private set;}
public DateTime End {get; private set;}
}
- If I have to get all employees that is retired today, is it more "logical" to search into Employee or into Retirement?
Probably, you should have only one repository for Employees, even for those that have retired.
Generally, in OO languages, one repository for each type of entity is the best choice.
- When I hire an employee I do not expect him to be retired. It is not a common feature. Does it change Employee behavior?
You should model invariants' violations with exceptions. If an employee cannot be retired in the same day it was hired, you should throw a specific exception documented in the Retire
command.