Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQRS and Domain Events

CQRS has got me into thinking mode.. I am tryinng to start a new project with CQRS ideas. The main things that I like is
1) the separation of Query and Command. Our Domain queries have been a problem.
2) Using Event Storage for Audit - I wont be using it for Replay - AT least not now.

I am good with the query side and I still have some questions on Domain Events

If a Command results in updation of Multiple Aggregates Roots( Ex. Order and OrderDetail) I will have the them scoped under UnitofWork ( transactional). Now each domain is responsible for publishing events when a change takes place to its state.

let us say the command changes 3 orderDetail records. Each OrderDetail will publish 2 Events. In the end we have 6 events .

a) If I publish the events as soon as I have made the changes to the domain object ( but not committed the transaction) how do I reverse the events that have been published (and may have been consumed by subscribers)

  • What I can think of is to hold the events to be published in a list 'under the same unit of work scope' and once the committ on transaction has been called, store it and publish it . Does this sound something one would do.

b) If the changes in OrderDetail requires that some change also take place in Order Aggregate Root then
i) Should I base those changes by handling the events published by OrderDetail Aggregate ? For ex. let us say two Order Detail were removed. This makes Order status from "preferred" to "Not preferred" . ii) What if the Event errors and does not update order state - If order remains preferred then it gets shipped in 2 days.

Adding another question
c) Are "Domain events are the source of all application state changes" or are they "Result of all application state changes"

Thank you in Advance,

The Mar

like image 913
TheMar Avatar asked Oct 01 '10 19:10

TheMar


1 Answers

a) you should not publish events until your transaction is commited, an event reprsent something that has happened, and hence the reason why they are all named in pass tense (eg OrderClearedEvent). Also in the case that you have to "revert" an event, you should take a corrective action, ie you dont erase the event, you must trigger a new event that corrects the effects of the event you want to revert

b) it seems that this is more of a problem on how you are modelling you entities and commands that anything else. I cant think of a reason why OrderDetail would be an AggregateRoot, but I dont know your domain...

c) Commands will result in at least one event being published

Hope this helps :) As Rinat said, the google group is the best place for asking questions, also have a look at cqrsinfo.com and the sample code from github.com/MarkNijhof/Fohjin and github.com/gregoryyoung/m-r

like image 200
roundcrisis Avatar answered Sep 19 '22 15:09

roundcrisis