DDD:如何处理多个可能的聚合根链接

时间:2017-09-14 17:04:57

标签: domain-driven-design aggregate

我正在使用DDD处理帮助台应用程序。我的问题是如何最好地处理可以引用两个可能的AR的实体。

我有RequestSubscriber订阅请求更新的人。

此订阅者可以是AgentContact

问题是 - 我是否应该对代理或联系人提供可选引用,并且只填写一个或具有相关类型的通用人员引用,以确保链接到达正确的位置?

模型选项:

// This
public class RequestSubscriber : DomainEntity, IPerson
{
    // Constuctors...

    public Guid? Agent_Id { get; private set; }
    public Guid? Contact_Id { get; private set; }
    public SubscriberType Type { get; private set; }
    public Email Email { get; private set; }
    public PersonName Name { get; private set; }
}

// Or This
public class RequestSubscriber : DomainEntity, IPerson
{
    // Constuctors...

    public Guid Person_Id { get; private set; }
    public SubscriberType Type { get; private set; }
    public Email Email { get; private set; }
    public PersonName Name { get; private set; }
}

Contructors:

    // This
    public RequestSubscriber(Guid id, Request request, IPerson person) : base(id)
    {
        Guard.ForNull(request, nameof(request));
        Guard.ForNull(person, nameof(person));

        if(person is Agent agent)
        {
            Email = agent.Email;
            Name = agent.Name;
            Type = SubscriberType.Agent;
        }
        else if (person is Contact contact)
        {
            Email = contact.Email;
            Name = contact.Name;
            Type = SubscriberType.Contact;
        }
        else
        {
            throw new ArgumentException("Subscribers must be an agent or contact", nameof(person));
        }

        request.Subscribe(this);
    }

    // Or This
    public RequestSubscriber(Guid id, Request request, Agent agent) : base(id)
    {
        Guard.ForNull(request, nameof(request));
        Guard.ForNull(agent, nameof(agent));

        Email = agent.Email;
        Name = agent.Name;
        Type = SubscriberType.Agent;

        request.Subscribe(this);
    }

    public RequestSubscriber(Guid id, Request request, Contact contact) : base(id)
    {
        Guard.ForNull(request, nameof(request));
        Guard.ForNull(contact, nameof(contact));

        Email = contact.Email;
        Name = contact.Name;
        Type = SubscriberType.Contact;

        request.Subscribe(this);
    }

1 个答案:

答案 0 :(得分:0)

一个选项可能是添加另一个间接级别:)

您的RequestSubscriber似乎汇集了RequestSubscriber。您传递的是Request,但Subscriber部分可能缺少某个概念。

同样是Request摘要无论请求是什么,Subscriber都可以抽象出订阅者所代表的任何内容。

如果还有其他原因需要SubscriberType,那么它可能会改变实施。