简体   繁体   English

汇总根

[英]Aggregate Roots

Consider the following structure: Customer->Orders->OrderLines->Quantity and Customer is the aggregate root. 考虑以下结构:Customer-> Orders-> OrderLines-> Quantity,并且Customer是聚合根。

Suppose we want to change the Quantity of one OrderLine, how would we do that? 假设我们要更改一个订单行的数量,我们该怎么做? Will Customer have a method like this: 客户是否可以使用以下方法:

public ChangeQuantity(Order order, OrderLine orderLine, int quantity)
{
    order.OrderLines.First(...).Quantity = quantity;
}

or will the implementation be: 或将实现为:

public ChangeQuantity(Order order, OrderLine orderLine, int quantity)
{
    order.ChangeQuantity(orderLine, quantity);
}

Most definitely the latter. 绝对是后者。 If you think about it the first approach violates the Law of Demeter - and that is really a core property of DDD. 如果您考虑一下,第一种方法违反了Demeter法则 -这实际上是DDD的核心属性。

But if you pass in the order and the orderline already, why doesn't the caller perform the method call? 但是,如果您已经传递了订单和订单行,为什么调用者不执行方法调用?

You don't need all access to the non-root objects to go through the root object. 您不需要对非根对象的所有访问就可以通过根对象。

You just need the root object to be a consistency and persistence boundary. 您只需要使根对象成为一致性和持久性边界即可。

So there's no reason to have either method, since your user can just go to the order line object directly: 因此,没有理由使用这两种方法,因为您的用户可以直接转到订单行对象:

OrderLine.Quantity = 5;

The fact that customer is an aggregate root simply means that there is no way to, for example, commit this change to the database without committing the entire customer to the database. 客户是聚合根这一事实意味着,例如,如果没有将整个客户都提交给数据库,则无法将更改提交给数据库。

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

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