DDD:通过其身份引用聚合根内部的实体 [英] DDD: refer to an entity inside an aggregate root by its identity

查看:335
本文介绍了DDD:通过其身份引用聚合根内部的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们只有它们的 identities 出现时,我一直在寻找正确的方法来引用位于聚合根中的实体来自网址参数.我问了一个上一个问题,该问题最终集中在值对象,所以我从这里开始另一个例子.

I'm stuck on finding the proper way to refer to entities located inside an aggregate root, when we only got their identities coming from URL parameters. I asked a previous question which ended up focused on value objects, so I'm starting with another example here.

假设我们要在Order内修改OrderLine:

  • 用户转到一个页面,在该页面上可以看到订单摘要"及其所有订单行.
  • 用户单击订单行旁边的编辑按钮.
  • 他被引导到edit-order-line?orderId=x&orderLineId=y
  • The user goes to a page where he can see the Order summary along with all its Order Lines.
  • The user clicks on the edit button next to an Order Line.
  • He gets directed to edit-order-line?orderId=x&orderLineId=y

现在,如果我需要更新订单行中的数量,可以执行以下操作:

Now if I need to update the quantity in the OrderLine, I can do:

Order order = orderRepository.find(orderId);
order.updateQuantity(orderLineId, 2);

但是,我对将命令的责任留给Id来检索其本身的部分的想法感到不太满意.我对此主题的看法是,在域内,我们应该与对象交谈,而不要与Ids交谈. ID不是普遍存在的语言的一部分,我认为它们应该不在域中,例如在Controller中.

However, I don't feel very comfortable with the idea of leaving the responsibility to the Order to retrieve parts of itself by Id. My view on the subject is that within the domain, we should just talk with objects, and never with Ids. Ids are not part of the ubiquitous language and I believe they should live outside of the domain, for example in the Controller.

我会对以下内容感到更加自信:

I would feel more confident with something like:

Order order = orderRepository.find(orderId);
OrderLine orderLine = em.find(OrderLine.class, orderLineId);
order.updateQuantity(orderLine, 2);

尽管我也不喜欢直接与实体管理器进行交互的想法.我觉得我绕开了存储库和汇总根目录的职责(因为我可以可能直接与OrderLine交互).

Though I don't like the idea of interacting directly with an Entity Manager, either. I feel like I'm bypassing the Repository and Aggregate Root responsibilities (because I could, potentially, interact with the OrderLine directly).

您如何解决?

推荐答案

我认为这种方法没有错:

In my opinion there is nothing wrong with this approach:

Order order = orderRepository.find(orderId);
order.updateQuantity(orderLineId, 2);

orderLineId是本地身份".它特定于聚集根,在根外部没有任何意义.您不必将其称为"id",它可以是"order line number".来自埃里克·埃文(Eric Evan)的书:

orderLineId is a 'local identity'. It is specific to aggregate root and does not make sense outside of it. You don't have to call it an 'id', it can be 'order line number'. From Eric Evan's book:

边界内的实体具有本地身份,仅在内部具有唯一性 聚集.

ENTITIES inside the boundary have local identity, unique only within the AGGREGATE.

...仅AGGREGATE根可以直接通过数据库查询获得.所有其他对象都必须通过关联遍历来找到.

...only AGGREGATE roots can be obtained directly with database queries. All other objects must be found by traversal of associations.

这篇关于DDD:通过其身份引用聚合根内部的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆