ddd中的多对多关系 [英] many to many relationship in ddd

查看:696
本文介绍了ddd中的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体Publisher和SocialAccount。 SocialAccount包含多个帐户,例如
Twitter,Facebook等。

I have two entities Publisher and SocialAccount. SocialAccount contains multiple accounts like Twitter, Facebook etc.

1个发布者可以附加到多个社交帐户,而1个社交帐户包含多个发布者,此社交帐户也是与另一个实体广告系列有关。我的意思是,两者都是独立实体

1 publisher can attach to many social account and 1 social account contains multiple publishers, this social account is also connected with another entity campaign. I mean both are independent entity

在创建发布者实例时,不必订阅社交帐户,也可以在稍后阶段进行订阅。

When creating publisher instance it is not necessary that it should subscribe to social account, it can subscribed to it at later stage.

我需要为发布者订阅1个或多个社交帐户。我该怎么做

I need to subscribe publisher to 1 or multiple social account . how do i do that

我如何将发布者与社交帐户之间的m对m关系转换为1对多关系。我不确定,因为我在很多地方都读过,我们应该避免实体之间的M到M关系。

how can i convert m to m relationship into 1 to many relationship, between publisher and social account . I am not sure because i read in many places that we should avoid M to M relationship between entities.

推荐答案

让我继续不要回答,因为我认为他正在带领您走上正确的道路。

Let me expand on Don's answer, as I think he is leading you on the right track.

M对N的关系是自然的,有用的,可以在DDD中处理。如果您需要M对N的关系,则无需尝试将其转换为(或者更可能是多个)M对1的关系。 乌迪·达汉的女演员给出一个很好的示例,说明了如何处理实体之间的M对N关系。

M-to-N relationships are natural, useful, and can be handled in DDD. If you require an M-to-N relationship, you don't need to try and convert it to an (or more likely multiple) M-to-1 relationship(s). Udi Dahan's acticle gives a nice example of how to handle an M-to-N relationship between entities.

首先,确定哪个实体应包含另一个实体的ID列表。 Udi使用职位发布( Job )和职位发布板( JobBoard )的示例。由于没有工作板就可以存在工作,没有工作板就不能存在工作,因此选择 JobBoard 作为聚合根,并且将包含 List<。 Job> 。这似乎是一对一的关系,但是,由于每个 Job 可以在多个 JobBoard s,实际上是M对N。

First, determine which entity should contain a list of IDs of the other. Udi uses the example of job postings (Job) and job posting boards (JobBoard). Since a job can exist without a job board and a job board cannot exist without jobs, JobBoard is chosen as the aggregate root and will contain a List<Job>. This might seem like an M-to-1 relationship, but, as each Job can be in the list for multiple JobBoards, it is really M-to-N.

在您的 SocialAccount Publisher ,我在C#中推荐这样的内容:

In your case of SocialAccount and Publisher, I recommend something like this in C#:

public class Publisher
{
    public int ID {get; private set;}

    private readonly IList<int> _AssignedSocialAccounts = new List<int>();
    public IEnumerable<int> AssignedSocialAccounts { get { return this._AssignedSocialAccounts; } }

    public Publisher(int ID) //Pass required fields to the constructor.
    {
        this.ID = ID;
    }

    public AssignSocialAccount(int SocialAccountID)
    {
        if(!this._AssignedSocialAccounts.Contains(SocialAccountID))
            this._AssignedSocialAccounts.Add(SocialAccountID);
    }
}

public class SocialAccount
{
    public int ID {get; private set;}

    public SocialAccount(int ID) //Pass required fields to the constructor.
    {
        this.ID = ID;
    }
}

(此示例使用类似于麦博加德的恶领域模型。)

(This example uses domain encapsulation similar to Jimmy Bogard's Wicked Domain Models.)

请注意,由于 Publisher 作为汇总根> SocialAccount 可以单独存在,但是 Publisher 如果没有 SocialAccount

Note that I chose Publisher to be the aggregate root since a SocialAccount can exist on its own, but a Publisher has no meaning without the existence of a SocialAccount.

还要注意,我传递的是唯一的ID,而不是对象本身的引用。这是DDD中的一种常用方法,并且允许延迟加载相关实体,尽管权衡是要在访问实体时必须调用存储库以获取实体。

Also note that I am passing around unique IDs, not references to the objects themselves. This is a common approach in DDD and allows for the lazy loading of related entities, although the tradeoff is you have to call the repository to get the entities when you want to access them.

此方法还意味着您没有将所有 SocialAccount s作为一个枚举。它们在各个 Publisher 之间拆分。要获取所有 SocialAccount 的列表,将需要单独的查询。

This approach also means that you do not have all SocialAccounts as a single enumeration. They are split up between the various Publishers. To get a list of all SocialAccounts will require a separate query.

这篇关于ddd中的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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