I am learning DDD and have this basic question:
It seems with factories, rich domain models, repositories the Create,Read,Update (of CRUD) are taken care of, but what about delete? There could be some business logic around delete of an entity where do you handle that? RepositoryImpl(which belongs to the infrastructure) layer should not bother itself checking those invariants, its job is to remove the given entity from the underlying datastore. This seems to be the diametric opposite of the intent for Factories, but DDD doesn't have something like "kill" factories for delete.
Say there is an Order entity that users can delete, but not until it is in "Fulfilled" state, so a client requesting a delete repo.delete(ent)
should get an Exception. Similarly there could be scenarios in which when a client requests a delete it results in an update(may be a status change or setting soft-delete flag).
Where should one handle such scenarios entity.delete()
(does it make sense?) or in a Application or domain service called delete. What I am worried is as long as Repository interface has that method called delete, any client can bypass the service methods and call repo methods directly.
Just to add a context, how I would structure my layers is through Java package, and use package visibility as a tool to forbid corrupting interactions amongst layers.
entity.Delete()
in my Domain Model, and within it I call therepository.Delete(this)
at the end, I truly think this make sense since the domain logic must go in the Domain Model – Nonchalantrepository
is just an interface type that completely hides implementation details from my domain. As in DDD, the repository interface lives in the domain layer, it does make since to use it anywhere within this layer, even if it's inside an entity. – Nonchalant