在DDD中的两个有界上下文之间进行通信 [英] Communicating between two Bounded Contexts in DDD

查看:220
本文介绍了在DDD中的两个有界上下文之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在域中有几个不同的边界上下文。 CRUD操作的验证建立在每个有界上下文中。

I have few different Bounded Contexts in the domain. The validation of a CRUD operation is built in each Bounded Context.

例如,只有当创建者是组长时,我才能创建一个名为GAME的实体。

For example, I can create an entity called GAME only if the person creating it is a Group Leader.

在此示例中,我有两个边界上下文(BC)。一个是游戏BC ,另一个是用户BC 。为了解决该问题,在游戏BC 中,我必须先向用户BC 进行域服务调用,例如 IsGroupLeader()创建游戏。

I have two Bounded Contexts (BC) in this example. One is the Game BC and the other is the User BC. To solve the problem, in the Game BC, I have to make a domain service call like IsGroupLeader() to the User BC before proceeding on creating the Game.

我认为DDD不建议使用此类交流方式。我也可以在游戏BC 中拥有一个 User实体,但我不想这样做,因为在相同的 User实体中使用的是不同的

I don't think this type of communication is recommended by DDD. I can have a User entity also in the Game BC, but I don't want to because the same User entity is being used differently in a different context in a different BC.

我的问题是:


  1. 我是否应该使用域事件,而游戏BC 必须将事件发送给用户BC ,询问用户的状态?使用这种方法,我不会像 IsGroupLeader 那样进行同步调用,而要进行名为 is_group_leader 的事件。然后,游戏BC必须等待用户BC处理事件并返回状态。仅在用户BC处理事件后,游戏BC才会创建游戏实体。

  1. Should I use Domain events where the Game BC has to send an event to the User BC asking the status of the User? With this approach, I don't make a synchronous call like IsGroupLeader but an event called is_group_leader. Then the Game BC has to wait for the User BC to process the event and return the status. The Game BC will create the Game entity only after the User BC process the event.

CQRS是否可以解决我的问题?

Is CQRS a solution to my problem?

任何想法都很感激。

推荐答案

何时集成BC,您有几种选择。不建议拨到外部BC的原因是,这要求两个BC同时运行。但是,这通常是完全可以接受的,并且比替代方法更简单。一种替代方法是让游戏BC订阅来自用户BC的事件并保留其所需数据的本地副本,在这种情况下,该副本是有关用户是否为组长的信息。这样,当游戏BC需要确定用户是否为组长时,不需要调出用户BC,只需读取本地存储的数据即可。这种事件驱动的挑战是同步事件。您要确保Game BC接收到来自User BC的所有适当事件。另一个挑战是处理最终一致性,因为BC在任何给定的时间点可能都略有不同步。

When integrating BCs, you have a few options. The reason that calling out to an external BC is discouraged is because it requires for both BCs to be operational at the same time. However, this is often quite acceptable and is simpler than the alternative. An alternative is to have the Game BC subscribe to events from the User BC and keep local copies of the data it needs, which in this case is information about whether a user is a group leader. In this way, when the Game BC needs to determine whether a user is a group leader, it doesn't need to call out to the User BC, it just reads locally stored data. The challenge of this event-driven alternative is synchronizing the events. You have the make sure the Game BC receives all appropriate events from the User BC. Another challenge is dealing with eventual consistency, since the BCs may be slightly out of sync at any given point in time.

CQRS与这个问题有点正交。

CQRS is somewhat orthogonal to this problem.

这篇关于在DDD中的两个有界上下文之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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