我正在使用DDD的幫助臺應用程序。我的問題是如何最好地處理可能引用兩個可能的AR的實體。DDD:如何處理多個可能的聚合根鏈接
我有一個RequestSubscriber
這是訂閱請求更新的人。
該訂戶是Agent
或Contact
。
問題是 - 我是否應該有一個對代理或聯繫人的可選引用,並且只填寫一個或具有關聯類型的通用人員引用以確保鏈接到達正確的位置?
型號選擇:
// 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);
}
另外一個想法是,我應該真的有兩個不同的用戶 - 代理用戶和聯繫用戶。 –
我想念你提到的'Subscriber'類。爲什麼以及它對RequestSubscriber的'IPerson'有什麼影響?那個AR保護的不變性/一致性邊界是什麼? –
我的歉意,'RequestSubscriber'是訂戶類名稱。這主要是因爲'Contact'的電子郵件通知與'Agent'不同。 –