问题描述
我只是想把头包裹在CQRS(/ ES)上。我没有对CQRS做任何认真的事情。可能我现在只是缺少一些非常基本的东西。目前,我正在阅读探索CQRS和事件源。关于命令,有一句话使我感到困惑:
I´m just trying to wrap my head around CQRS(/ES). I have not done anything serious with CQRS. Probably I´m just missing something very fundamental right now. Currently, I´m reading "Exploring CQRS and Event Sourcing". There is one sentence that somehow puzzles me in regards to commands:
单个接收者处理命令。
"A single recipient processes a command."
我也在Greg Young(),对于任何命令类型,如果注册了一个以上的命令处理程序,则会引发异常。
I´ve seen this also in the CQRS sample application from Greg Young (FakeBus.cs) where an exception is thrown when more then one command handler is registered for any command type.
对我来说,这表明这是CQRS(或命令?)的基本原则。是什么原因?对我来说,这有点违反直觉。
For me, this is an indication that this is a fundamental principle for CQRS (or Commands?). What is the reason? For me, it is somewhat counter-intuitive.
想象一下,我有两个组件需要执行一些操作以响应命令(我有两个相同组件的实例还是两个独立组件无关紧要)。然后,我需要创建一个将命令委托给这些组件的处理程序。
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 these components.
在我看来,这引入了不必要的依赖关系。就CQRS而言,命令不过是发送的消息而已。我不知道为什么此消息应该只有一个处理程序的原因。
In my opinion, this is introducing an unnecessary dependency. In terms of CQRS, a command is nothing more than a message that is sent. I don´t get the reason why there should be only one handler for this message.
有人可以告诉我我在这里想念的吗?可能有一个很好的理由,我现在看不到。
Can someone tell me what I am missing here? There is probably a very good reason for this that I just don´t see right now.
致谢
推荐答案
我绝不是专家CQRS,但也许我可以帮忙阐明一下。
I am by no means an expert myself with CQRS, but perhaps I can help shed some light.
造成这种情况的根本原因之一是交易的一致性。需要在应用程序的一个离散(和隔离)部分中处理命令,以便可以在单个事务中提交该命令。一旦开始拥有多个处理程序,就几乎不可能将应用程序分发到单个进程之外(并保持事务的一致性)。因此,尽管您可以采用这种方式进行设计,但不建议这样做。
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.
希望这会有所帮助。
这篇关于CQRS-单个命令处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!