应用CQRS时如何在create中获取ID?

发布于 2024-10-06 07:56:19 字数 260 浏览 9 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然#的苍凉 2024-10-13 07:56:19

CQRS 就是一劳永逸的,并且由于 GUID 非常可靠(冲突风险低),所以发送您自己生成的 GUID 是没有问题的。

步骤基本上是:

  1. 创建命令
  2. 生成并向其分配您的身份 (GUID)
  3. 触发命令
  4. 返回之前生成的身份

阅读有关 维基百科上的 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:

  1. Create your command
  2. Generate and assign your identity (GUID) to it
  3. Fire the command
  4. Return the identity earlier generated

Read more about GUIDs on Wikipedia

爱冒险 2024-10-13 07:56:19

任何大小的整数id/GUID/字节数组在实践中都足够可靠,但它们都不符合理论要求(发生冲突),而有效的理论解决方案存在并且在大多数情况下都可以应用。

我的解决方案是:在同等级别的系统合作中,一个人的身份应该由更高级别的系统来保证。高层系统是管理协作系统生命周期的系统。

示例

class John
{
    private readonly int id;

    public John(int id)
    {
        this.id = id;
    }

    public void UseSite(Site site)
    {
        site.CreateAccount(id, "john");

        site.SetPassword(id, "john", "123");

        /* ... */
    }
}

class Site
{
    public void CreateAccount(int humanId, string accName) { /* ... */ }

    public void SetPassword(int humanId, string accName, string pwd) { /* ... */ }

    /* ... */
}

class Program
{
    static void Main(string[] args)
    {
        Site s = new Site();

        // It's easy to guarantee the identity while there's only one object 
        John j = new John(4);

        Console.ReadLine();
    }
}

Program是更高级别的模块。它负责正确使用 JohnSite。为 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:

class John
{
    private readonly int id;

    public John(int id)
    {
        this.id = id;
    }

    public void UseSite(Site site)
    {
        site.CreateAccount(id, "john");

        site.SetPassword(id, "john", "123");

        /* ... */
    }
}

class Site
{
    public void CreateAccount(int humanId, string accName) { /* ... */ }

    public void SetPassword(int humanId, string accName, string pwd) { /* ... */ }

    /* ... */
}

class Program
{
    static void Main(string[] args)
    {
        Site s = new Site();

        // It's easy to guarantee the identity while there's only one object 
        John j = new John(4);

        Console.ReadLine();
    }
}

Program is the higher-level module. It is responsible to use John and Site correctly. Providing John 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文