应用CQRS时如何在create中获取ID?
我对 CQRS 的看法是,严格遵循命令时不会返回任何内容(返回类型 void),因此我的示例非常简单:创建某些内容时如何检索 ID?
例如,在创建信用卡交易时,返回交易 ID 似乎相当重要,或者在创建客户时,如果您获取了您创建的客户或客户 ID,那么会更容易,以便浏览器可以自动导航到该客户例如页面。
一种解决方案可能是首先要求提供 ID,然后使用该 ID 创建客户或交易,但这看起来很奇怪。
有谁有这方面的经验或知道应该如何以最有效的方式完成?也许我误解了什么?
My take on CQRS is when followed strictly your commands don't return anything (return type void), so my example is really straight forward: How do you retrieve an ID when creating something?
For example, when creating a credit card transaction it seems rather important to return a transaction ID, or when creating a customer it would be much easier if you got the customer you created or the customer ID back so a browser could navigate automatically to that customer page for example.
One solution could be to first ask for an ID and then create the customer or transaction with that ID, but it seems pretty weird.
Does anyone have any experience with this or know how it should be done in the most effective way? Maybe I have misunderstood something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CQRS 就是一劳永逸的,并且由于 GUID 非常可靠(冲突风险低),所以发送您自己生成的 GUID 是没有问题的。
步骤基本上是:
阅读有关 维基百科上的 GUID
CQRS is all about fire-and-forget, and since GUIDs are very reliable (low risk of collision) there is no problem sending in a GUID that you generate your self.
The steps would basically be:
Read more about GUIDs on Wikipedia
任何大小的整数id/GUID/字节数组在实践中都足够可靠,但它们都不符合理论要求(发生冲突),而有效的理论解决方案存在并且在大多数情况下都可以应用。
我的解决方案是:在同等级别的系统合作中,一个人的身份应该由更高级别的系统来保证。高层系统是管理协作系统生命周期的系统。
示例:
Program
是更高级别的模块。它负责正确使用John
和Site
。为John
提供唯一标识符是此责任的一部分。您会发现处理某些现实生活系统(例如人类)的身份是不可能或非常困难的。当这些系统与您的系统处于同一级别时,就会发生这种情况。典型的例子是人和网站。您的网站永远无法保证正确的人会请求该页面。在这种情况下,您应该使用基于概率的方法和可靠的哈希值。
Integer id's / GUIDs / byte arrays of any size can be reliable enough at practice, but they all do not correspond to the theoretical requirement (collisions happens), while valid theoretical solution exists and can be applied most of the time.
I'd formulate the solution as: in the equal-level system co-operation one's identity should be guaranteed by the system of a higher level. Higher-level system is the one which manages the lifetime of co-operating systems.
Example:
Program
is the higher-level module. It is responsible to useJohn
andSite
correctly. ProvidingJohn
with an unique identifier is a part of this responsibility.You will find that it is impossible or very hard to deal with the identity of some real-life systems, like a human. It happens when these systems are on the same level as your system. Typical example is a human and a web-site. Your site will never have a guarantee that the right human requesting the page. In this case you should use the probability-based approach with a reliable hash.