取决于过去事件的传奇

发布于 2024-12-03 16:33:51 字数 736 浏览 2 评论 0原文

我有一个问题,如何处理决策制定的传奇故事 取决于传奇创建之前发布的事件。

下面是一个说明我的问题的示例:

假设我有一个 CustomerAR 和一个 OrderAR。当客户AR是 创建了一个验证过程开始,该过程的结果是 客户可以自由消费的订单金额,无需特殊说明 授权。我不会详细介绍这个过程,因为 这是断章取义的。计算金额后会发送命令 到 CustomerAR 并计算出金额和 CustomerAR 发布具有该值的事件 (CustomerMaxOrderAmountEvent)。所以 到目前为止还不错。

几周后,客户下了订单。订单 AR 是 创建并启动我的 OrderSaga。传奇故事等待订单完成 创建完成,然后必须做出是否需要发送的决定 该订单的 AutorizationCommand。为了做出这个决定,它必须 了解 CustomerMaxOrderAmountEvent 是否已发布以及其值 量。通常 OrderSaga 也会订阅 CustomerMaxOrderAmountEvent,但问题是这个事件永远不会 发生是因为过去已经发生过。

我该如何处理这个问题。我应该查询读取模型来了解 值,我应该发送一个命令来获取该值,我应该做一个 参考 CustomerAR,我应该重播所有历史事件吗 传奇故事,所以他已经知道历史了。

更新

请注意,这是关于概念的,而不是关于这个具体示例的。该示例纯粹是为了澄清问题:“2 个不相关的聚合根,它们不属于同一有界上下文。”

感谢您的帮助。

梅尔文

I have a question how to deal with a saga where the decision making
depends on an event that was published before the saga was created.

Here is an example to illustrate my issue:

Imagine I have a CustomerAR and a OrderAR. When the customerAR is
created a validation process starts, the result of that process is the
amount of an order that the customer is free to spend without special
authorization. I will not go into detail about this process because
it's out of context. When the amount is calculated a command is sent
to the the CustomerAR with the calculated amount and the CustomerAR
publishes an event (CustomerMaxOrderAmountEvent) with that value. So
far so good.

Then a few weeks later the customer places an order. The OrderAR is
created and starts my OrderSaga. The saga waits until the order is
created completely and then has to make a decision if it needs to send
an AutorizationCommand for that order. To make that decision it has to
know if the CustomerMaxOrderAmountEvent is published and the value of
the amount. Normally the OrderSaga will also subscribe to the
CustomerMaxOrderAmountEvent, but the problem is this event will never
occur because it already did in the past.

How should I deal with this. Should I query the read model to know the
value, should I send a command to get the value, should I make a
reference to the CustomerAR, should I replay all historic events in
the saga so he knows the history already.

UPDATE

Please note that it's about the concept not about this concrete example. The example is pure for clarification of the problem: "2 non related aggregate roots that are not part of the same bounded context."

Thankful for help.

Melvin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

薯片软お妹 2024-12-10 16:33:51

我会寻求更简单的解决方案 - 只需将此信息(例如以 HasCustomerReachedMaxOrderAmount 的形式)添加到启动 Saga 的事件中。

我选择的第二个选项是准备一个设计用于 Sagas 的读取模型并从那里查询数据,但我什至会在 Sagas 开始之前收集所有必需的信息。这种丰富可以由 AR 引发的原始事件的处理程序执行,因为这不是聚合/有界上下文的一部分。

然而,在大多数情况下,基于随事件传递的数据就足够了,因为 Sagas 应该仅以流程形式包含业务逻辑。这通常意味着您最终可能会得到多个 Saga(按照您的示例 OrderSagaOrderWithMaxAmountSaga)。

尽管如此,考虑到您提供的示例场景,我认为订单是否需要授权的决定应该是您的域本身的一部分,并在启动传奇时通过。

我会像这样模拟这个场景 - CustomerAR 计算最大金额;客户下订单-> OrderAR 是使用有关该客户最大金额的信息创建的,Order 验证是否需要额外授权 -> Saga 开始吧。要点是,有关最大金额的信息对于 CustomerAR(可以更改)和 OrderAR(不可变)都很重要。

I would go for simpler solution - just add this information (for example in a form of HasCustomerReachedMaxOrderAmount) to the event that starts the Saga.

The second option I'd choose is to prepare a read-model designed to be used with the Sagas and query data from there, but I'd gather all the required information even before the saga is started. This enrichment could be performed by a handler to the original event raised by AR as this is not something that is part of Aggregate/Bounded Context.

In most cases however basing on data passed with events is sufficient due to the fact that Sagas should contain business logic only in form of process. This very often means that you might end up with more then one Saga (to follow your example OrderSaga and OrderWithMaxAmountSaga).

All that said, considering the sample scenario you've provided, I think that decision whether order requires authorization or not should rather be part of your domain itself and passed while starting saga.

I'd model this scenario like this - CustomerAR calculates max amount; customer places an order -> OrderAR is created with the information about max amount for this customer, Order verifies if it needs additional authorization -> Saga get started. The point is that information about max amount is important for both CustomerAR (where it can change) and OrderAR (where it's immutable).

雄赳赳气昂昂 2024-12-10 16:33:51

我刚刚开始涉足 DDD,我不打算重复 kstaurch 所说的任何内容,但以下是我在查看您的场景时的一些想法:

  1. 将最大订单金额作为账单信息的一部分怎么样?这必须在某个地方得到,我确信您会处理付款处理被拒绝的情况。

  2. 如果传奇总是发送授权命令,但处理最大订单屏幕的是授权部分怎么办?如果您说“如果金额超过 X 美元,则获得授权”,也许您的业务模式也会将其视为“如果金额低于 X 美元,则自动批准”。这样做的好处是,所有审批考虑事项(包括是否获得审批)都可以转移到审批实体或 saga。

I'm just starting to dabble with DDD and I don't intend to repeat anything that kstaurch said, but here are some thoughts I had looking at your scenario:

  1. What about making the max order amount part of the billing information? That has to be gotten somewhere and I'm sure you take care of the condition where payment processing is denied.

  2. What if the saga always sends the authorization command, but it's the authorization part that handles the max order screen? If you're saying "If the amount is over $X then get authorization", maybe your business model also thinks of it as "If it's under $X, auto-approve." The benefit is that all approval considerations (including whether to get approval) can be moved to an approval entity or saga.

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