-
Notifications
You must be signed in to change notification settings - Fork 61
Description
Dear @ploeh and @dotnetjunkie,
Suppose there is a new use case stating that when a Product is deleted, the ProductInventory with the Id of the deleted Product must be deleted as well. What is the best place to tie the delete operation on the Product to the delete operation on the ProductInventory?
The natural place to consider is DeleteProductService. Below IInventoryRepository's new method Delete
deletes entries in ProductInventory before the Product is deleted from ProductRepository.
public DeleteProductService(
IProductRepository productRepository, IInventoryRepository inventoryRepository)
{
// Guards...
this.productRepository = productRepository;
this.inventoryRepository = inventoryRepository;
}
public void Execute(DeleteProduct command)
{
// New method
this.inventoryRepository.Delete(command.ProductId);
this.productRepository.Delete(command.ProductId);
}As a result DeleteProductService gains a new dependency - IInventoryRepository.
But could SqlProductRepository be a more appropriate place for this operation?
If a Product is deleted, but ProductInventory still references the deleted Product's id, the Data Access Layer will be in an invalid state. The updated Delete method shows a possible implementation.
public void Delete(Guid id)
{
this.context.ProductInventories.Remove(this.GetProductInventoryById(id));
this.context.Products.Remove(this.GetById(id));
}The number of dependencies for SqlProductRepository remains unchanged.
Thank you,
Max