聚合根
考虑以下结构:Customer->Orders->OrderLines->Quantity,Customer 是聚合根。
假设我们想要更改一个 OrderLine 的数量,我们该怎么做?客户是否有这样的方法:
public ChangeQuantity(Order order, OrderLine orderLine, int quantity)
{
order.OrderLines.First(...).Quantity = quantity;
}
或者实施方式是:
public ChangeQuantity(Order order, OrderLine orderLine, int quantity)
{
order.ChangeQuantity(orderLine, quantity);
}
Consider the following structure: Customer->Orders->OrderLines->Quantity and Customer is the aggregate root.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要对非根对象的所有访问都通过根对象。
您只需要根对象作为一致性和持久性边界。
因此,没有理由使用任何一种方法,因为您的用户可以直接转到订单行对象:
客户是聚合根这一事实仅仅意味着没有办法,例如,在不提交的情况下将此更改提交到数据库整个客户到数据库。
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:
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.
绝对是后者。如果您考虑一下,第一种方法违反了 德米特法则 - 这确实是一个核心属性DDD。
但是如果您已经传入订单和订单行,为什么调用者不执行方法调用?
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.
But if you pass in the order and the orderline already, why doesn't the caller perform the method call?