应该从API返回CQRS命令的内容? [英] What should be returned from the API for CQRS commands?

查看:246
本文介绍了应该从API返回CQRS命令的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,在通过RESTful HTTP API公开的面向CQRS的API中,命令和查询通过HTTP谓词表示,命令是异步的,通常返回 202 Accepted ,而查询获取您需要的信息。有人问我以下内容:假设他们想要更改某些信息,他们必须发送命令然后查询以获得结果状态,为什么要强制客户端发出两个HTTP请求,只需返回他们想要的内容即可单个HTTP请求中命令的HTTP响应?

As far as I understand, in a CQRS-oriented API exposed through a RESTful HTTP API the commands and queries are expressed through the HTTP verbs, the commands being asynchronous and usually returning 202 Accepted, while the queries get the information you need. Someone asked me the following: supposing they want to change some information, they would have to send a command and then a query to get the resulting state, why to force the client to make two HTTP requests when you can simply return what they want in the HTTP response of the command in a single HTTP request?

推荐答案

我们在DDD / CRQS邮件列表中进行了长时间的对话几个月前(链接)。讨论的一部分是单向命令,这就是我认为你所假设的。你可以发现Greg Young反对这种模式。命令会更改状态,因此容易出现故障,这意味着它可能会失败,您应该支持此操作。具有POST / PUT请求的REST API为此提供了完美的支持,但您不应该仅返回202 Accepted,而是真正给出一些有意义的结果。有些人返回200成功,还有一些包含URL的对象来检索新创建或更新的对象。如果命令处理程序失败,它应返回500并显示错误消息。

We had a long conversation in DDD/CRQS mailing list a couple of months ago (link). One part of the discussion was "one way command" and this is what I think you are assuming. You can find out that Greg Young is opposed to this pattern. A command changes the state and therefore prone to failure, meaning it can fail and you should support this. REST API with POST/PUT requests provide perfect support for this but you should not just return 202 Accepted but really give some meaningful result back. Some people return 200 success and also some object that contains a URL to retrieve the newly created or updated object. If the command handler fails, it should return 500 and an error message.

具有即发即弃命令是危险的,因为它可能会给消费者一个关于系统的错误想法州。

Having fire-and-forget commands is dangerous since it can give a consumer wrong ideas about the system state.

这篇关于应该从API返回CQRS命令的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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