以下哪些例子代表DDD的正确使用?

我已经和DDD合作了几个月了,我遇到了一些我不确定的事情。

举一个将Product添加到Order对象的简单示例。 从我们的Controller中,我们通过UI传递了一个int ,它表示数据库中的Product 。 以下哪两个例子是正确的(如果它们都错了,请告诉我)?

示例一:

 public class OrderController { // Injected Repositories private readonly IProductRepository _productRepository; // Called by UI public void AddProduct(int productId) { Order order = ...; // Persisted Order Product product = _productRepository.GetProduct(productId); order.AddProduct(product); } } 

Controller实例化产品本身并通过以下方法添加它:

 void AddProduct(Product product) { productList.Add(product); } 

示例二:

 public class OrderController { // Injected Repositories private readonly IProductRepository _productRepository; // Called by UI public void AddProduct(int productId) { Order order = ...; // Persisted Order order.AddProduct(productId, _productRepository); } } 

Order域模型将注入的产品存储库传递给它,它获取Product并添加它:

 Product AddProduct(int productId, IProductRepository productRepository) { Product product = productRepository.GetProduct(productId); productList.Add(product); return product; } 

我现在已经去了第一个例子,因为你的域模型不应该在内部调用服务方法,但是我最近看到了一些使用我的第二个例子并且看起来很整洁的例子。 在我看来, 示例一正在接近贫血。 示例二会将所有产品添加逻辑移动到域模型本身。

第二个是可怕的……

将订单添加到订单中不应该在其签名上具有存储库,因为存储库不是域的一部分。

我倾向于选择第一个。

是的哥们第一个更好……

好像我们以对象的forms思考……

将产品添加到列表与产品存储库无关,它应该只接受产品。