问题
In many ddd examples we have a simple:
Order (aggregate root [AR]) and OrderLine (aggregate member [AM])
+
Invoice (aggregate root [AR]) and InvoiceLine (aggregate member [AM])
In that examples we issue an invoice on order so it is direct reference from one AR (Invoice) to another (Order) which is clearly correct.
However, in my case things are more complicated:
We issue an Invoice on many OrderLines from different Orders.
So on one Invoice, we have: InvoiceLine #1 referencing to OrderLine #1 from Order #1, another InvoiceLine #2 referencing to OrderLine #2 (from another Order #2)
and, for example, there is no InvoiceLine referencing to OrderLine#2 from Order #1.
How to solve this case?
It seems like we have to hold reference to OrderLine in InvoiceLine which is not correct, afaik. But I have no other ideas :/
Thanks in advance for any suggestions.
回答1:
You should not hold real object instance references from one AR to another. Any entity references should be transient.
You should store only identifiers.
In your case you could be storing the Order ID and OrderLine Number on the relevant InvoiceLine entries. It may even be a value object.
In this way you are not faced with any object retrieval issues when you get you Invoice AR from its repository.
来源:https://stackoverflow.com/questions/16920521/how-to-avoid-holding-reference-to-non-root-aggregate-in-another-non-root-aggrega