Skip to content

Delete ProductInventory when deleting Product #7

@max-ch9i

Description

@max-ch9i

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions