language-agnostic - DDD : Aggregates and sub-aggregates

标签 language-agnostic doctrine-orm domain-driven-design aggregateroot

我有一个非常复杂的聚合,聚合根 Order .它包含在聚合之外毫无意义的实体(例如 OrderItem )。但是也有一些实体应该是这个聚合的一部分,但在这个聚合之外也有意义(例如 ShippingMethodInvoice )。

为这个复杂的聚合拥有一个存储库(通过 root 的 id 加载整个聚合)并且还有一个用于管理可能的运输方式的 CRUD 存储库和另一个用于列出发票的存储库是否正确?

更一般地说,在 DDD 中是否有可能有一个聚合,它是另一个聚合的一部分?

最佳答案

您可以将“一个聚合,它是另一个聚合的一部分”视为“一个聚合包含另一个聚合的引用”。

例如

public class Order {
    private Invoice invoice;
}

<class name="Order" table="T_ORDER">
    <one-to-one name="invoice" column="INVOICE_ID" />
</class>

如果在这种情况下订单和发票都是聚合,我将有一个 OrderRepository 和一个 InvoiceRepository。您可以使用检索订单
orderRepository.findBy(orderId)

您可以使用检索发票
invoiceRepository.findBy(invoiceId) 

或者
Order order = orderRepository.findBy(orderId);
Invoice invoice = order.getInvoice();

并且有一篇关于如何设计聚合的著名文章( http://dddcommunity.org/library/vernon_2011/ )建议使用标识引用来实现这种关系。
public Class Order {
    private InvoiceId invoiceId;
}

<class name="Order" table="T_ORDER">
    <component name="invoiceId">
        <property name="value" column="INVOICE_ID" />
    </component>
</class>

您可以使用检索订单
orderRepository.findBy(orderId)

您可以使用检索发票
invoiceRepository.findBy(invoiceId) 

或者
Order order = orderRepository.findBy(orderId);
Invoice invoice = invoiceRepository.findBy(order.getInvoiceId()); 

关于language-agnostic - DDD : Aggregates and sub-aggregates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17720040/

相关文章:

language-agnostic - 一般 - 函数在内存中是如何表示的?

language-agnostic - 区分音乐文件中的乐器

java - 为什么 switch 语句在 case 之后继续

symfony - SonataDoctrineORMAdminBundle 抛出 array_combine 错误

symfony - 在 Doctrine 2 中获取自定义字段

domain-driven-design - DDDD:事件数据

unicode - 用 utf-8 字符定义变量名是个好主意吗?

doctrine-orm - 上下文错误异常 : Undefined Index when using composite key for doctrine associations with Symfony 3 entities

c# - 是否有在基于 DDD 的分层架构中的模型和数据访问层之间使用 LINQ 的建议模式

repository - 存储库是否应该通过传入 id 或实体本身来删除/删除实体