问题描述
在许多ddd示例中,我们有一个简单的示例:
In many ddd examples we have a simple:
Order(聚合根[AR])
和
OrderLine(聚合成员[AM ])
Order (aggregate root [AR])and OrderLine (aggregate member [AM])
+
发票(总根[AR])
和
InvoiceLine(总成员[AM])
Invoice (aggregate root [AR])and InvoiceLine (aggregate member [AM])
在这些示例中,我们在订单上开具发票,因此它是从一个AR(发票)直接引用到另一个(订单)的显然是正确的。
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.
因此,在一张发票上,我们有:
InvoiceLine#1引用了订单#1的OrderLine#1,
是另一张InvoiceLine #2引用OrderLine#2(来自另一个Order#2)
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)
,例如,没有InvoiceLine引用Order#1的OrderLine#2。
and, for example, there is no InvoiceLine referencing to OrderLine#2 from Order #1.
如何解决这种情况?
似乎我们必须在InvoiceLine中保留对OrderLine的引用,这是不正确的,afaik。但我没有其他想法:/
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.
推荐答案
您不应持有从一个AR到另一个AR的真实对象实例引用。任何实体引用都应该是瞬态的。
You should not hold real object instance references from one AR to another. Any entity references should be transient.
您应该只存储标识符。
在您的情况下,您可以存储相关InvoiceLine条目上的订单ID和OrderLine号。甚至可能是一个价值对象。
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.
通过这种方式,当您从发票库中获取发票AR时,就不会遇到任何对象检索问题。
In this way you are not faced with any object retrieval issues when you get you Invoice AR from its repository.
这篇关于如何避免在另一个非根聚合中保留对非根聚合的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!