一个人怎么会申请命令查询分离(CQS),当从命令所需的结果数据? [英] How would one apply command query separation (CQS), when result data is needed from a command?

查看:132
本文介绍了一个人怎么会申请命令查询分离(CQS),当从命令所需的结果数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命令查询分离的维基百科的定义,更说明

In wikipedia's definition of command query separation, it is stated that

更正式,方法应该返回一个值只有
如果他们是引用透明
,因此具有无副作用。

More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.

如果我发出命令,我应该如何确定或报告命令是否成功,因为根据这个定义的函数不能返回的数据?

If I am issuing a command, how should I determine or report whether that command was successful, since by this definition the function cannot return data?

例如:

string result = _storeService.PurchaseItem(buyer, item);

这呼叫它既有一个命令和查询,但查询部分是命令的结果。我想我可以使用命令模式,像重构这个如此:

This call has both a command and query in it, but the query portion is result of the command. I guess I could refactor this using the command pattern, like so:

PurchaseOrder order = CreateNewOrder(buyer, item);
_storeService.PerformPurchase(order);
string result = order.Result;



但是,这好像它增加了码的大小和复杂性,这不是一个非常积极的方向重构走向。

But this seems like it's increasing the size and complexity of the code, which is not a very positive direction to refactor towards.

有人可以给我一个更好的方式,当你需要操作的结果,实现命令查询分离?

Can someone give me a better way to achieve command-query separation when you need the result of an operation?

我失去了一些东西在这里。

Am I missing something here?

感谢

注:?!
Martin Fowler的有这样说CQS CommandQuerySeparation 的限制:

迈耶喜欢绝对使用命令查询
分离,但也有
异常。弹出堆栈是改变
状态的改性剂的良好
的例子。迈耶正确地说,你
能避免这种方法,但它
是一个有用的成语。所以,我宁愿
时,我可以遵循这个原则,但
我准备打破它让我的
流行。

Meyer likes to use command-query separation absolutely, but there are exceptions. Popping a stack is a good example of a modifier 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.

这是他的观点,它几乎总是值得向命令/查询分离重构,除了一些小的简单的例外。

From his view, it's almost always worth it to refactor towards command/query separation, except for a few minor simple exceptions.

推荐答案

这个问题是旧的,但还没有收到满意的答案,所以我会从差不多一年前阐述我的意见了一下。

This question is old but has not received a satisfying answer yet, so I'll elaborate a bit on my comment from almost a year ago.

使用驱动架构的事件使得有很大的意义,不仅是实现明确的命令/查询分离,而且还因为它打开了新的架构选择,通常使用异步编程模型拟合(有用的,如果您需要扩展架构) 。很多时候,你会找到解决办法可能存在于不同的造型您的域。

Using an event driven architecture makes a lot of sense, not only for achieving clear command/query separation, but also because it opens new architectural choices and usually fits with an asynchronous programming model (useful if you need to scale your architecture). More often than not, you will find the solution may lie in modelling your domain differently.

因此​​,让我们把你购买的例子。 StoreService.ProcessPurchase 将是处理购买合适的命令。这将产生一个 PurchaseReceipt 。这是一个更好的办法,而不是返回收据 Order.Result 。让事情变得很简单,你可以返回收到命令,并在这里违反CQRS。如果你想要一个更清洁的分离,该命令将提高 ReceiptGenerated 事件,你可以订阅。

So let's take your purchase example. StoreService.ProcessPurchase would be a suitable command for processing a purchase. This would generate a PurchaseReceipt. This is a better way instead of returning the receipt in Order.Result. To keep things very simple, you can return the receipt from the command and violate CQRS here. If you want a cleaner separation, the command would raise a ReceiptGenerated event that you can subscribe to.

如果你认为你的域名,这实际上可能是一个更好的模型。当你在收银台结帐时,你按照这个过程。生成收据前,信用卡支票可能是由于。这很可能需要更长的时间。在同步的情况下,你会等待收银员,无法做任何事情。

If you think about your domain, this may actually be a better model. When you're checking out at a cashier, you follow this process. Before your receipt is generated, a credit card check might be due. This is likely to take longer. In a synchronous scenario, you would wait at the cashier, unable to do anything else.

这篇关于一个人怎么会申请命令查询分离(CQS),当从命令所需的结果数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆