CQRS - Single command handler?

删除回忆录丶 提交于 2019-12-04 09:45:15

I am by no means an expert myself with CQRS, but perhaps I can help shed some light.

"A single recipient processes a command.", What is the reason?

One of the fundamental reasons for this is transactional consistency. A command needs to be handled in one discrete (and isolated) part of the application so that it can be committed in a single transaction. As soon as you start to have multiple handlers, distributing the application beyond a single process (and maintaining transactional consistency) is nearly impossible. So, while you could design that way, it is not recommended.

Hope this helps.

Imagine I have two components that need to perform some action in response to a command (it doesn´t matter if I have two instances of the same component or two independent components). Then I would need to create a handler that delegates the command to this components.

That's the responsibility of events.

A command must be handled by one command handler and must change state for a single aggregate root. The aggregate root then raises one or more events indicating that something happened. These events can have multiple listeners that performs desired actions.

For example: you have a PurchaseGift command. Your command handler load the Purchase aggregate root and performs the desired operation raising a GiftPurchased event. You can have one or more listeners to the GiftPurchase event, one for sending an email to the buyer confirming the operation and another to send the gift by mail.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!