DDD-如何设计不同有界上下文之间的关联 [英] DDD - How to design associations between different bounded contexts
问题描述
我已经设置了一个域项目,该项目正在填充ORM。该域包含不同的聚合,每个聚合都有自己的根对象。
我的问题是,应该如何对待跨越总边界的属性?
- 这些属性是否应该简单地忽略边界,以便
- 或者,是否应该没有从上下文A到B的直接链接,并且上下文A中的对象是否具有 int ContextBId属性可用于通过B聚合根从B获取域对象?
- 或...
示例:
上下文A =用户
上下文B =游戏
在 Users
上下文中,有一个对象 UserOwnedGames
。该对象具有属性 User
,该属性是对相同 Users
上下文中的对象的引用。该对象还具有 Game
的属性,该属性显然不在用户中,而在 Games
上下文中。 / p>
这种关系的样子(或应该怎样?)?在数据库中很明显(例如2个外键),但是代码应该是什么样子?
听起来像您的 User
上下文也需要一个 Game
实体。但是请注意,这不一定是 Game
实体,它是 Game
上下文的根。这两个有界上下文对于 Game
是什么以及它具有什么属性可能有不同的想法。只有身份将两个游戏对象绑定在一起。
用户上下文
{
根用户总数
{
身份;
名称;
OwnedGames:游戏价值实体列表
}
价值实体游戏
{
Identity;
名称;
}
}
游戏上下文
{
总根游戏
{
身份;
名称;
所有者:用户值实体
HighScore:int
TimesPlayed:int
...大量与用户上下文无关的其他属性
}
价值实体用户
{
身份;
名称;
//没有OwnedGames属性,在这种情况下,我们不在乎用户拥有哪些其他游戏。
}
}
I have setup a domain project which is being populated with an ORM. The domain contains of different aggregates each with its own root object. My question is how properties that cross the aggregate boundries should be treated?
- Should these properties simply ignore the boundries so that a domain object in bounded context A has a reference to an object in context B?
- Or, should there be no direct link from context A to B and does the object in context A have an "int ContextBId" property that can be used to get the domain object from B through the B aggregate root?
- Or ...
An example:
Context A = Users
Context B = Games
Inside the Users
context there is an object UserOwnedGames
. This object has a property User
which is a reference to an object in the same Users
context. The object also has a property to a Game
which is obviously not in the Users but rather in the Games
context.
How would (or should?) this relation look like? It's clear in the database (ie 2 foreign keys) but what should the code look like?
It sounds like your User
context also needs a Game
entity. Note however that this is not necessarily the same Game
entity which is the root of the Game
context. These two bounded contexts may have different ideas of what a Game
is, and what properties it has. Only the identity ties the two Game objects together.
User Context
{
Aggregate Root User
{
Identity;
Name;
OwnedGames : List of Game value entities
}
Value Entity Game
{
Identity;
Name;
}
}
Game Context
{
Aggregate Root Game
{
Identity;
Name;
Owner : User value entity
HighScore : int
TimesPlayed : int
... A whole bunch of other properties which are not relevant in the User context
}
Value Entity User
{
Identity;
Name;
// No OwnedGames property, in this context we don't care about what other games the user owns.
}
}
这篇关于DDD-如何设计不同有界上下文之间的关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!