There is a concept that talks about the separation of the persistent layer
from the domain layer
to make the domain layer
more robust - it would not be dependent on the actual implementation of the repository in persistence layer
, but only on the repository interface.
It means that we have:
IPersonRepository {...} // in domain layer
PersonCassandraRepository implements IPersonRepository {...} // in persistence layer
Person (Aggregate Root) {...}
Now, what about Person
?
In anemic-domain-model
we can have:
IPerson {...} // in domain layer
Person implements IPerson {...} // in persistence layer
Why put Person in persistence layer?
Because it contains implementation-specific code.
For example, it may contain JPA-related annotations, and the same as with repository, we don't want data-store specific implementation in our domain layer.
We can do the above with anemic-domain-model, because Person does not contain any domain logic, which means we can put Person in persistene layer.
In anemic-domain-model data is separated from behavior, so Person's behavior is done by separated services, and not written in Person itself.
We cannot do this layers separation with rich-domain-model, because in this case, Person does contain domain-specific logic.
How would you do this layers separation within a rich-domain-model application?
Or maybe you are thinking that it is not needed.
The class(es) that get(s) persisted don't necessarily need to be the classes (or even implement the same interfaces) that are in the domain model.
So you could have a
Person
class in the persistence layer that is designed to just work with your persistence layer and has no real behavior and probably doesn't enforce domain invariants. In your domain layer, you have aPerson
class that does enforce invariants and has no idea about persistence.The repository's responsibility in this approach would then be to translate between the representation of
Person
in the persistence layer and the representation ofPerson
in the domain layer.