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.

  • Not sure if I got your problem correctly ... the usual approach is: change the stock level in the same database transaction as the change in the LineItem. But that is very obvious, so I guess your problem is somewhere else? Commented May 18, 2013 at 9:55
  • Wondering if there is an object-oriented approach, maybe using kind of design pattern or analysis pattern. Think it is easier to maintain if these logic is part of domain model's behavior.
    – user91376
    Commented May 18, 2013 at 12:41

1 Answer 1


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.

  • 1
    Can you elaborate how you envision to use publish/subscribe for this? As it stands, your answer does not really help. Commented May 18, 2013 at 15:14
  • True, done. Hope it is better now. Commented May 18, 2013 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.