@Service 注入聚合?
@Service injection into aggregates?
我有一个 Order
集合,其中包含以下命令:
CreateOrderCommand
PlaceOrderCommand
- ...(因为它们与问题无关,所以已编辑)...
PlaceOrderCommand
是指将Order
放置到外部执行场所。我已经捕获了在单独的(非 CQRS)@Service
中向外部执行场所下订单的行为。然而,我正在努力(由于缺乏使用 Axon 的经验)如何最好地将我的 @Service
与聚合连接起来。
我的正常思维方式应该是:
- 将
@Service
注入聚合的 @Autowired
构造函数。
- 发出
PlaceOrderCommand
时,使用该服务将订单下达到相关执行场所,完成后发出事件(OrderPlacedSuccessfullyEvent
或 ErrorInOrderPlacementEvent
)。
- 更改相关
@EventSourcingHandler
内的聚合状态。
我的问题是:
- 我上面关于如何使用 Axon 处理这个特定用例的描述是否有意义(特别是将
@Service
注入到聚合中对我来说感觉有点不对劲)?
- 或者在使用 CQRS/event 采购与 Axon 时,是否有不同的最佳实践来模拟我的场景?
- Axon 在聚合中需要一个空的构造函数。这与
@Autowired
构造函数如何协调?
我可能考虑的另一件事是:
- 有一个
PlaceOrderInstructionCommand
(而不是简单的 PlaceOrderCommand
),它发出一个单独的事件侦听器正在侦听的 ReceivedPlaceOrderInstructionEvent
。
- 该事件侦听器会将相关的
@Service
注入其中并放置 Order
。
- 然后在放置
Order
之后,它会向聚合发送一个命令(或者它应该发出一个事件?)通知它更新它的状态。
能否就此场景建模的最佳实践提出建议?
The PlaceOrderCommand refers to placing the Order onto an external execution venue.
我假设将订单下达到外部执行场所意味着与外部系统进行交互。如果是,那么它不应该是您域的一部分。在这种情况下,您需要提出 Integration Event
.
如您所述,您可以从您的域中提出 Command
之类的 ProcessOrder
。在那个 Command
中,您可以更新 Domain
(例如,将 OrderStatus
设置为 Processing
)并引发一个集成事件,例如 OrderArrived
,然后由一个单独的过程。
来自 Microsoft Docs:
The purpose of integration events is to propagate committed transactions and updates to additional subsystems, whether they are other microservices, Bounded Contexts or even external applications.
Integration events must be based on asynchronous communication between multiple microservices (other Bounded Contexts) or even external systems/applications.
您可以将集成事件作为 Domain
之外的单独进程(或工作程序)来处理。这是你的@Service
将被注入的地方。成功处理订单后,您可以广播名为 OrderPlaced
.
的集成事件
现在,任何与下订单有关的订阅者都将订阅该事件。在您的情况下,您的 Domain
有兴趣在下订单后更新状态。因此,您会在 Domain
中 Subscribe
到 OrderPlaced
事件来更新 Order
.
的状态
希望对您有所帮助。
我有一个 Order
集合,其中包含以下命令:
CreateOrderCommand
PlaceOrderCommand
- ...(因为它们与问题无关,所以已编辑)...
PlaceOrderCommand
是指将Order
放置到外部执行场所。我已经捕获了在单独的(非 CQRS)@Service
中向外部执行场所下订单的行为。然而,我正在努力(由于缺乏使用 Axon 的经验)如何最好地将我的 @Service
与聚合连接起来。
我的正常思维方式应该是:
- 将
@Service
注入聚合的@Autowired
构造函数。 - 发出
PlaceOrderCommand
时,使用该服务将订单下达到相关执行场所,完成后发出事件(OrderPlacedSuccessfullyEvent
或ErrorInOrderPlacementEvent
)。 - 更改相关
@EventSourcingHandler
内的聚合状态。
我的问题是:
- 我上面关于如何使用 Axon 处理这个特定用例的描述是否有意义(特别是将
@Service
注入到聚合中对我来说感觉有点不对劲)? - 或者在使用 CQRS/event 采购与 Axon 时,是否有不同的最佳实践来模拟我的场景?
- Axon 在聚合中需要一个空的构造函数。这与
@Autowired
构造函数如何协调?
我可能考虑的另一件事是:
- 有一个
PlaceOrderInstructionCommand
(而不是简单的PlaceOrderCommand
),它发出一个单独的事件侦听器正在侦听的ReceivedPlaceOrderInstructionEvent
。 - 该事件侦听器会将相关的
@Service
注入其中并放置Order
。 - 然后在放置
Order
之后,它会向聚合发送一个命令(或者它应该发出一个事件?)通知它更新它的状态。
能否就此场景建模的最佳实践提出建议?
The PlaceOrderCommand refers to placing the Order onto an external execution venue.
我假设将订单下达到外部执行场所意味着与外部系统进行交互。如果是,那么它不应该是您域的一部分。在这种情况下,您需要提出 Integration Event
.
如您所述,您可以从您的域中提出 Command
之类的 ProcessOrder
。在那个 Command
中,您可以更新 Domain
(例如,将 OrderStatus
设置为 Processing
)并引发一个集成事件,例如 OrderArrived
,然后由一个单独的过程。
来自 Microsoft Docs:
The purpose of integration events is to propagate committed transactions and updates to additional subsystems, whether they are other microservices, Bounded Contexts or even external applications.
Integration events must be based on asynchronous communication between multiple microservices (other Bounded Contexts) or even external systems/applications.
您可以将集成事件作为 Domain
之外的单独进程(或工作程序)来处理。这是你的@Service
将被注入的地方。成功处理订单后,您可以广播名为 OrderPlaced
.
现在,任何与下订单有关的订阅者都将订阅该事件。在您的情况下,您的 Domain
有兴趣在下订单后更新状态。因此,您会在 Domain
中 Subscribe
到 OrderPlaced
事件来更新 Order
.
希望对您有所帮助。