command-query-separation - 命令查询分离 : commands must return void?

标签 command-query-separation

如果 CQS 阻止命令返回状态变量,那么如何为可能不成功的命令编码?假设您不能依赖异常。

似乎任何请求/响应都违反了 CQS。

所以看起来你会有一组“妈妈可以我”方法给出命令返回的状态。在多线程/多计算机应用程序中会发生什么?

如果我有三个客户端希望请求服务器的对象增加一个(并且对象的限制为 0-100)。所有人都检查他们是否可以,但一个人得到它 - 而另外两个则不能,因为它只是达到了限制。似乎返回状态可以解决这里的问题。

最佳答案

It seems like anything that is request/response is a violation of CQS.


几乎是的,因此命令查询- 分离 .作为马丁福勒nicely puts it :

The fundamental idea is that we should divide an object's methods into two sharply separated categories:

Queries: Return a result and do not change the observable state of the system (are free of side effects).

Commands: Change the state of a system but do not return a value [my emphasis].


请求服务器的对象增加 1 是一个命令,因此它不应返回值 - 处理对该请求的响应意味着您正在同时执行命令和查询操作,这违反了 CQS 的基本原则。
所以如果你想知道服务器的值是什么,你可以发出一个单独的查询。
如果你真的需要一个请求-响应模式,你要么需要一个复杂的回调事件过程来发出特定请求状态的查询,要么纯 CQS 不适合你系统的这部分——注意这个词纯的。

多线程是 CQS 的一个主要缺点,并且可能使其难以实现。 Wikipedia有一个基本的例子和对此的讨论,还链接到同一篇 Martin Fowler 的文章,他建议打破模式来完成一些事情而不会让自己发疯:

[Bertrand] Meyer [the inventor of CQS] likes to use command-query separation absolutely, but there are exceptions. Popping a stack is a good example of a query that modifies state. Meyer correctly says that you can avoid having this method, but it is a useful idiom. So I prefer to follow this principle when I can, but I'm prepared to break it to get my pop.



TL;博士 - 我可能只是看看返回一个响应,即使它不是正确的 CQS。

关于command-query-separation - 命令查询分离 : commands must return void?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28382151/

相关文章:

c# - 当命令需要结果数据时,如何应用命令查询分离 (CQS)?

ruby - "Command-Query Separation"规则的异常(exception)情况?

domain-driven-design - 在领域驱动设计的 CQRS 实现中为命令/查询单独应用服务?

asp.net-mvc - 从命令运行查询是否违反了命令-查询分隔?

java - Spring - 将自定义实例传递给构造函数

c# - 事件溯源基础设施实现

oop - 在对域建模时是否应该考虑规则 "one transaction per aggregate"?

design-patterns - 如何从现有的写入数据库生成 CQRS 中的读取数据?