I have a quite complex aggregate, with aggregate root Order. It contains entities (eg. OrderItem) which are meaningless outside an aggregate. But there are also entities that are supposed to be part of this aggregate, but also do make sense outside this aggregate (eg. ShippingMethod or Invoice).
Is it right to have a repository for this complex aggregate (loading whole aggregate by root's id) and also have CRUD repository for managing possible shipping methods and another repository for listing invoices?
More generally, is it possible in DDD to have an aggregate, which is part of another aggregate?
You can consider "an aggregate, which is part of another aggregate" as "an aggregate holds another aggregate's reference".
For example
public class Order {
private Invoice invoice;
}
<class name="Order" table="T_ORDER">
<one-to-one name="invoice" column="INVOICE_ID" />
</class>
If both Order and Invoice are aggregates in this context, I'll have a OrderRepository and a InvoiceRepository.You can retrieve an Order using
orderRepository.findBy(orderId)
And you can retrieve an Invoice using
invoiceRepository.findBy(invoiceId)
or
Order order = orderRepository.findBy(orderId);
Invoice invoice = order.getInvoice();
And there is famous article about how to design aggregates(http://dddcommunity.org/library/vernon_2011/) which suggests realizing this relationships using Identity reference.
public Class Order {
private InvoiceId invoiceId;
}
<class name="Order" table="T_ORDER">
<component name="invoiceId">
<property name="value" column="INVOICE_ID" />
</component>
</class>
You can retrieve an Order using
orderRepository.findBy(orderId)
And you can retrieve an Invoice using
invoiceRepository.findBy(invoiceId)
or
Order order = orderRepository.findBy(orderId);
Invoice invoice = invoiceRepository.findBy(order.getInvoiceId());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With