A Transaction
contains one or more LineItem
. One LineItem
has a relation with an Item
. Every Item
has a number field that represent quantity in stock.
Using this approach, every Transaction
creation will need to decrease quantity in Item
. If user update a Transaction
then quantity in Item
will need to be adjusted. If user delete a Transaction
then quantity in Item
will need to be increased.
What is the best way to implement this rule or behaviour in domain model layer? Transaction
operations (like create, update, or delete) are located in data access layer.
2
You can use publish/subscribe pattern for that.
This means that the Item object subscribes to all changes from sources affecting the stock level, such as a change in quantity in the LineItem object, or the creation or deletion of the LineItem object. The Item object then adjusts the stock accordingly.
This needs to happen within the same database transaction to preserve integrity.
I would recommend such an approach only if you have multiple publishers and subscribers, otherwise it is just overkill. For example the stock can be also influenced by running a physical inventory and create documents with correction postings. Or if you keep the value of the inventory in parallel with the stock, the value object might be a subscriber, too.
That pattern works better if you have a framework which all publishers and subscribers use. Basically the framework should make sure that any change in any object is available for subscription. Also it is good idea to model or configure the subscriptions so the subscribers can be tracked.
2