简体   繁体   English

避免在聚合中引用实体的最佳方法是什么?

[英]What is the best way to avoid referencing an entity inside an aggregate?

I am new to DDD.我是 DDD 的新手。 I just started a project by DDD architecture.我刚刚通过 DDD 架构启动了一个项目。 In domain design I faced a problem.在域设计中,我遇到了一个问题。 Given I have a customer that supposed to send me a list of his/her orders, for example:假设我有一个客户应该向我发送他/她的订单清单,例如:

customer1:{Onion,Apple,orange,wireless Mouse,PC,fish,egg}

every item can be delegated to different store:每个项目都可以委托给不同的商店:

Grocery Store:{onion,apple,orange,fish,egg}
Digital Store:{wireless mouse,PC}
butchery:{fish,egg}

every order just have a name.每个订单只有一个名字。 to validate list of orders for customer, I should delegate validation to customer as an 'AggregateRoot' and consider list of orders as 'List':要验证客户的订单列表,我应该将验证委托给客户作为“AggregateRoot”,并将订单列表视为“列表”:

public class Customer : AggregateRoot<long>
{
    public long Id { get;private set; }
    public string CustomerName { get;private set; }

    private List<Order> _orders = new List<Order>();
    public IReadOnlyCollection<Order> Orders => _orders.AsReadOnly();

    public Customer(long id, string customerName)
    {
        Id = id;
        CustomerName = customerName;
    }

    public void AssignOrders(List<Order> orders)
    {
        //check some validation on orders before assignments
        this._orders=orders;
    }
}

public class Order : Entity<long>
{
    public long Id { get;private set; }
    public string Name { get;private set; }

    public Order(long id,string name)
    {
        //check some validation for single order before creation
        this.Id=id;
        this.Name=name; 
    }
}

but my design problem arises when i want assign 'orders' to 'stores': based on DDD Rule "An aggregate root should not hold references to an entity in another aggregate root"但是当我想将“订单”分配给“商店”时出现了我的设计问题:基于 DDD 规则“聚合根不应包含对另一个聚合根中的实体的引用”

public class Store:AggregateRoot<long>
{
    public long Id { get;private set; }
    public string StoreName { get;private set; }
    private List<Order> _orders = new List<Order>();
    public IReadOnlyCollection<Order> Orders => _orders.AsReadOnly();

    public Store(string storeName, long id)
    {
        StoreName = storeName;
        Id = id;
    }

    public void AssignOrders(List<Order> orders)
    {
        //check some validation on orders before assignments
        this._orders = orders;
    }
}

In My design 'Store' is an aggregate root and needs to have list of orders.在我的设计中,“商店”是一个聚合根,需要有订单列表。 Beside there are orders assigned to multiple stores.旁边有分配给多个商店的订单。 now what is your suggestion:现在你的建议是什么:

  1. Should I keep 'orders' as entity in 'Customer' and assigns a list of 'Store' to every single 'Order'.我是否应该将“订单”作为“客户”中的实体保留,并将“商店”列表分配给每个“订单”。
  2. Should I Keep 'Orders' as an Aggregate root with a list of orders and a 'customerId', but then how to validate list altogether?我应该将“订单”保留为带有订单列表和“customerId”的聚合根,但是如何完全验证列表?
  3. Should I change 'Orders' in 'customer' to list of value objects and just copy them for orders (Hard to maintain, Cause there are other agregate roots that need a list of 'orders', and store is one of them)我是否应该将“客户”中的“订单”更改为价值对象列表,然后将它们复制为订单(难以维护,因为还有其他聚合根需要“订单”列表,而商店就是其中之一)

I suggest you talk to your domain experts to clarify how the business is done, if you can.如果可以的话,我建议您与您的领域专家交谈,以澄清业务是如何完成的。 Then you can find distinct entities that you need to model.然后,您可以找到 model 所需的不同实体。

It looks like you may have 2 subdomains ("world views") here: Customer and Store which will both have a concept of an Order , but those orders are different.看起来您可能在这里有 2 个子域(“世界视图”): CustomerStore两者都有一个Order的概念,但这些订单是不同的。

Possibly this model can be of use:可能这个 model 可以使用:

CustomerOrder - a set of items that customer wants to buy. CustomerOrder - 一组客户想要购买的商品。 It has a number of Customer Order Line objects which contain Product ID / Quanity / Volume .它有许多包含Product ID / Quanity / VolumeCustomer Order Line对象。 When you create this CustomerOrder object you can validate it.当您创建此CustomerOrder object 时,您可以对其进行验证。 You can also validate each Customer Order Line with some simple checks, for example, that at least volume or quantity > 0 etc.您还可以通过一些简单的检查来验证每个Customer Order Line ,例如,至少数量或数量 > 0 等。

StoreOrder is the order that store receives. StoreOrder是商店收到的订单。 It also contains a list of Store Order Line objects.它还包含一个Store Order Line对象列表。 It is different thing to the CustomerOrder .这与CustomerOrder不同。 It will contain different items, may be aggregated, can have additional information in it etc.它将包含不同的项目,可以汇总,可以包含其他信息等。

Talk to your domain experts to validate the model.与您的领域专家交谈以验证 model。 If you don't have any domain experts handy, implement something that will get you going and evolve it when needed.如果您手边没有任何领域专家,请实施一些可以让您继续前进并在需要时对其进行改进的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM