Reputation: 19356
Supose I have an order entity and I want to can delete it only if this order has no order items.
In this case, when I implement the repository pattern, I need a method that allows to delete entities. So the consumer (for example a controller in ASP), would do it in this way:
var order = unitOfWork.Orthers.GetOrderById(1);
unitOfWork.Orders.Remove(order);
unitOfWork.Complete();
The problem here is that the consumer delete always the order, and don't check if it is possible or not. Anyway, the domain has no control or has no the way to forbid to the consumer to delete an order if it is not allowed.
There is some way to enforce that the consumer wouldn't be the responsable to ensure that it is possible to delete the order?
In the rest of the uses case, for example to create an order, the constructor ensure the information is correct, and in the update, the order only allow to update if it is possible according to the rules. But in the case to delete an order, I don't know how to do it.
Thanks.
Upvotes: 0
Views: 58
Reputation: 57259
There is some way to enforce that the consumer wouldn't be the responsible to ensure that it is possible to delete the order?
As far as I know, no. It is always going to be possible to write code that deletes an item without first checking with the domain model.
The best you can hope for is to have tests/inspections that detect when the programmer has done the wrong thing, so that you can fix it before the expensive accident happens.
That said, you may want to review: Don't Delete -- Just Don't.
It might apply in this way: we never delete items through the interface, but instead use a "soft delete" mechanism -- we use the update protocols to allow the domain model to record the fact that this order is no longer interesting to the business.
Then, when we need to free resources, we purge some subset of the records that have been soft deleted.
In short, you take away from the consumers the capability of deleting records, and instead limit that subset to a well tested process that understands the rules.
Upvotes: 1