DDD-使用Doctrine 2的有界上下文之间的关联映射 [英] DDD - Association mapping between bounded contexts using Doctrine 2

查看:55
本文介绍了DDD-使用Doctrine 2的有界上下文之间的关联映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解使用Doctrine 2在来自不同有界上下文的两个实体之间实现关联映射的正确方法.假设有两个"User"和"Post"实体属于"User"和"Content"绑定上下文.在内容"上下文中还有一个用户"概念,该概念通过多对一关联来确定帖子"的作者.因此,内容"上下文中的用户"只是一个包含用户ID的值对象.

I am struggling to understand the right way to implement association mapping between two entities from different bounded contexts using Doctrine 2. Suppose that there are two "User" and "Post" entities that belong to "User" and "Content" bounded contexts, respectively. There is also a "User" concept in "Content" context that determines the author of a "Post" through a Many-To-One association. Therefore, "User" in "Content" context is simply a value object containing the user id.

我的问题是我应该如何使用教义2实现这种关联?我有两个都有各自问题的解决方案:

My question is that how should I implement this association using Doctrine 2? I have two solutions that both have their own issues:

解决方案1 ​​:

/**
 * @ORM\Entity
 * @ORM\Table(name="posts")
 * @ORM\HasLifecycleCallbacks()
 */
 class Post
 {
    ...

    /**
     * @ORM\ManyToOne(targetEntity="UserBC\User", inversedBy="posts")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    private $user;

    ...
 }

解决方案2 :

/**
 * @ORM\Entity
 * @ORM\Table(name="posts")
 * @ORM\HasLifecycleCallbacks()
 */
 class Post
 {
    ...

    /**
     * @ORM\Column(type="integer")
     */
    private $user_id;

    ...
 }

在第一个解决方案中,用户"上下文中的用户"实体已在内容"上下文中使用,这违反了彼此独立的BC上的DDD规则.第二种解决方案遵循DDD规则,但会影响数据库架构(通过外键约束删除用户"和帖子"表之间的数据库级关系).

In the first solution, "User" entity from "User" context has been used inside "Content" context that violates DDD rules on BCs being independent of each other. The second solution respects DDD rules but affects database schema (removes database-level relationship between "users" and "posts" tables through a Foreign key constraint).

那么,实现这种关联的正确方法是什么?

So, what is the right way to implement such associations?

推荐答案

第二种解决方案是正确的.

正如您正确观察到的那样,应避免不同BC之间的关联.引用另一个BC中的实体的正确方法是通过ID.

As you correctly observe, associations between different BCs should be avoided. The right way to reference an entity in another BC is by ID.

其结果是,在数据库中BC之间没有约束.毕竟,您尝试使它们独立.如果您认为这是错误的,那么解决此问题的唯一方法是重新考虑BC设计,即合并两个BC.但是,这是一个决定,不应由代码的气味决定,而应由上下文映射决定.

This has the consequence that the BCs don't have constraints between them in the DB. After all, you try to make them independent. If you feel that this is wrong, then the only way around this is to reconsider your BC design, i.e. merge the two BCs. This is however a decision that should not be driven by code smells, but by your context map.

注意:仅当其他BC是聚合根时,才允许通过ID引用其他BC.如果引用的实体不是AR,则那里还有另一种设计气味.不是认真的,但仍然需要考虑.

Note: Referencing entities from other BCs by ID is only allowed if they are aggregate roots. If the referenced entity is not an AR, you have another design smell right there. Not a serious one, but still one that needs consideration.

这篇关于DDD-使用Doctrine 2的有界上下文之间的关联映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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