DDD - 聚合根 - 示例顺序和OrderLine [英] DDD - Aggregate Root - Example Order and OrderLine

查看:687
本文介绍了DDD - 聚合根 - 示例顺序和OrderLine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的手肮脏学习DDD(通过开发一个例如电子商务网站,像订单 OrderLines 产品类别等)。
从我可以感知到的聚合根概念我认为订单类应该是 OrderLine 的聚合根

I am trying to get my hands dirty learning DDD (by developing a sample eCommerce site with entities like Order, OrderLines, Product, Categories etc). From what I could perceive about Aggregate Root concept I thought Order class should be an aggregate root for OrderLine.

到目前为止,事情进展顺利,但是当从UI定义创建订单流时,我感到困惑。
当我想在订单对象中添加订单行时,应该如何获取/创建一个 OrderLine 对象的实例:

Things went fine so far, however I am confused when it define a create order flow from UI. When I want to add an order line to my order object, how should I get/create an instance of an OrderLine object:


  1. 我应该在我的UI / Service类中为 OrderLine()语句进行硬编码
  2. 我应该在订单中定义一个参数如 productID 数量等参数 class?

  1. Should I hardcode the new OrderLine() statement in my UI/Service class
  2. Should I define a method with parameters like productID, quantity etc in Order class?

另外,如果要从UI或<$中删除硬编码的实例c $ c>订单使用DI的类。这是最好的方法?

Also, what if I want to remove the hardcoded instantiations from the UI or the Order class using a DI. What would be the best approach for this?

推荐答案


从我可以感知到的
总体根概念我认为订单
类应该是
OrderLine的aggreagrte根。

From what I could perceive about Aggregate Root concept I thought Order class should be an aggreagrte root for OrderLine.

是的,OrderLine的应该很可能在一个订单根目录下,因为OrderLine在父级订单之外可能没有任何意义。

Yes, OrderLine's should most likely be under an Order root, since OrderLine's likely make no sense outside of a parent Order.


我应该对新的OrderLine )
语句在我的UI /服务类

Should I hardcode the new OrderLine() statement in my UI/Service class

可能不是,虽然这是它经常发生,工作。正如我所看到的,问题是,对象构造通常发生在不同的上下文中,验证约束根据上下文而有所不同。

Probably not, though this is how it happens often and it is made to work. The problem, as I see it, is that object construction often happens in different contexts, and the validation constraints differ depending on that context.


我应该在Order类中定义一个
参数的方法,如productID,数量等

Should I define a method with parameters like productID,quantity etc in Order class?

如下所示:

public OrderLine AddOrderLine(Product product, int Quantity ... )

这是一种方法。注意我使用了Product类而不是ProductId。有时一个比另一个更好。我发现我有很多因为各种原因 - 有时我有ID,没有什么好的理由来拉根,有时我需要另一个根来验证操作。

This is one way to do it. Notice I used a Product class instead of a ProductId. Sometimes one is preferable to the other. I find I use both a lot for various reasons - sometimes I have the ID and there's no good reason to pull the aggregate root, sometimes I need the other root to validate the operation.

另一种方法是为孩子们实现一个自定义集合。

Another way I do this is to implement a custom collection for the children.

所以我有:

order.OrderLines.Add(product, quantity);

这感觉有点自然或OO,特别是如果一个实体根有许多子集合避免混乱。

This feels a little more natural or OO, and in particular if an entity root has many child collections it avoids clutter.

order.AddOrderLine() order.AddXXX() order.AddYYY() order.AddZZZ()

order.OrderLines.Add() order.ZZZs.Add() order.YYYs.Add()


此外,如果我要从UI
或使用DI的Order类中删除
硬编码的实例。什么
将是最好的方法?

Also, what if I want to remove the hardcoded instantiations from the UI or the Order class using a DI. What would be the best approach for this?

这将是工厂模式的教科书案例。我将这样的工厂注入我的自定义集合,以支持 Add()方法中的实例化。

This would be a textbook case for the Factory pattern. I inject such a Factory into my custom collections to support instantiation in those Add() methods.

这篇关于DDD - 聚合根 - 示例顺序和OrderLine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆