-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Delete ProductInventory when deleting Product #7
Comments
I would probably make the deletion of the inventory part of the product repository in case this 'cascading delete' is a hard rule; in other words deleting a product will always cause deletion of the inventory. This is probably the most obvious scenario, because referential integrity would prevent you from deleting the product from your RDBMS when its inventory records are still there. On the other hand, if that relationship is not that strict, and deletion of inventory is not necessarily a precondition of removal of a product, implementing the deletion as part of the business logic might make more sense. I could even imagine that both inventory management and product management each become part of a different bounded context, and part of a different service (possibly getting notified by each other through domain events through a durable queue, see section 6.1.3, page 179). When that's the case, both services will have their own code base and database. This will cause the deletion of the product and its inventory to live in completely different places. |
I concur 👍 It's part of the software architect's responsibility to decide where to place business logic. If you decide to model all data in a single fully normalised relational database with referential integrity, you'll implicitly also be putting significant business logic in the database, as @dotnetjunkie implies. There can be good reasons to do that, so that's not a value judgment. If you do that, though, you're going to need a book about relational database design and maintenance. And that's not DIPPP 😉 Our book is about DI, indeed, but also object-oriented programming and design. In that context, we emphasise putting business logic in code rather than in databases. That's a trade-off. A valid criticism is that you end up replicating features already in the RDBMS. That's a fair criticism. In my experience, it makes sense to pull business logic into code when you anticipate that it has a certain degree of complexity. On the other hand, if you're developing a system that's mostly CRUD it may be a better decision to put as many rules as possible into the database itself. All that said, if you decide to put the business logic into the code, and if you decide to follow the Dependency Inversion Principle, you should implement the business rules and try to forget about the underlying storage system. As Steven implies, you might even have a situation where inventory management and product catalogue are two separate bounded contexts, each with their own database. In that situation, I would reach for eventual consistency:
Anything between this kind of architecture, and a SQL transaction with cascading deletes is possible, depending on trade-offs. |
Thank you for sharing your thoughts. It seems that putting business logic into the data access layer is less Evolving the application into having a bounded context for each Good shout to use a messaging queue for this use case! Best wishes, |
Dear @ploeh and @dotnetjunkie,
Suppose there is a new use case stating that when a
Product
is deleted, theProductInventory
with theId
of the deletedProduct
must be deleted as well. What is the best place to tie the delete operation on theProduct
to the delete operation on theProductInventory
?The natural place to consider is DeleteProductService. Below
IInventoryRepository
's new methodDelete
deletes entries in
ProductInventory
before theProduct
is deleted fromProductRepository
.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, butProductInventory
still references the deletedProduct
's id, the Data Access Layer will be in an invalid state. The updated Delete method shows a possible implementation.The number of dependencies for
SqlProductRepository
remains unchanged.Thank you,
Max
The text was updated successfully, but these errors were encountered: