问题
In CQRS, how do you make sure a command handler is updating the write store and read store transactionally?
I am not sure if these two steps are suppoed to be a transaction? Or do you rely on eventual consistency here? Meaning the read store will be updated eventually?
What is the (or a) common way to do this using NServiceBus 5 or 6?
In our application, we have IRepository<T>
(Add, Update) for the command side to update Sql Server database.
The query side is simple. A database and a facade on top of Entity Framework DbContext
.
The steps on the command side would be:
- MVC controller receives data from View
- controller action creates a Command/Message object and sends it to the bus.
- The proper command handler creates the appropriate domain object and does validation on it.
- If valid, uses IRepository to insert or update the database.
- Now what?
Does this have to be a Saga in NServiceBus, where Step 1 and 2 update the command store and read store?
Thank you.
回答1:
The flow I see is the following:
- the controller receives an action and emits a command in the command bus
- the proper command handler gets fired and the write models get updated
- in the command handler, an event is fired
- the proper event handler gets fired and the read models get updated
So updating the read models get done in an event handler - hence the eventual consistency
Hope this helps! Best of luck!
来源:https://stackoverflow.com/questions/33133141/in-cqrs-how-to-work-with-nservicebus-to-update-command-and-query-store