开发者

What is the recommended way to add child entities to aggregate roots?

Which is a better approach, create child entities first, then pass to the aggregate root to add them, or have the aggregate root create them? For example:

Order.AddOrderLine(new OrderLine开发者_JAVA百科(product, quantity, ...));

Or

Order.AddOrderLine(product, quanity, ...);

Which is a better approach? I'm sure this is purely subjective, but I want to see which has more pros vs cons.


Ok, so basically my opinion is that you should create an object before, because:

  • creating of the object is itself a separate concern, which can be a fairly complex by the way. And if, for instance, constructor of the OrderLine will be changed later you will need to change an Order type too. This is bad, cos you will need to change the Order only because of some changes in the OrderLine. So interface of the Order Root shouldn't depend on the OrderLine.

  • the second approach may be hard to test if you method will contain some additional logic exept of only calling this.OrderLines.Add(orderLine);

Edit After discussing this with a friend of mine, I came up with the following opinion:

  • Order Root should control lifetime of his child, because this will be more clear for other person who uses this API and will minimize the possibility of an inappropriate usage. And also will better reveal intent.

  • Using this approach also allows us not to worry about validating incoming OrderLine, because if we are responsible of the OrderLine creation then we can create it properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜