其中的这些例子代表了正确的使用DDD的? [英] Which of these examples represent correct use of DDD?
问题描述
我一直在与工作DDD几个月,我已经碰到几件事情我不能确定的。
I've been working with DDD for a few months now and I've come across a few things I'm unsure of.
以添加产品
到订单$ C $的简单示例C>对象。从我们的控制器,我们就会有一个
INT
通过它代表了产品
在数据库中的用户界面通过。 ?其中下面的两个例子是正确的(让我知道如果他们都错了)
Take the simplistic example of adding a Product
to an Order
object. From our Controller, we'd have an int
passed via the UI which represents a Product
in the database. Which of the following two examples is correct (let me know if they're both wrong)?
例一:
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);
}
}
控制器实例化产品本身,并通过其添加方法:
The Controller instantiates the Product itself and adds it in via the method:
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);
}
}
的
订单
域模型传递给它注入的产品库,它获得的产品,并增加了它:
The Order
domain model has the injected product repository passed to it and it gets the Product and adds it:
Product AddProduct(int productId, IProductRepository productRepository)
{
Product product = productRepository.GetProduct(productId);
productList.Add(product);
return product;
}
目前我已经走了第一个例子,因为你的域模型应该永远在内部调用服务的方法,但是我已经看到最近的一些例子使用我的第二个例子,它看起来整洁。在我看来,那例一是近乎血虚。 示例二将移动所有的产品除了逻辑放到领域模型本身。
I've currently gone for the first example, because your Domain Model should never call a service method internally, however I've seen a few examples lately that use my second example and it does look tidy. It seems to me that Example One is verging on Anaemic. Example Two would move all the Product addition logic into the Domain Model itself.
推荐答案
二是太可怕了......
Second one is horrible...
添加产品订购不应该有其签名的存储库,因为库不是域的一部分。
Adding a product to order should not have the repository on its signature since repository is not part of the domain.
我倾向于去的第一个。
这篇关于其中的这些例子代表了正确的使用DDD的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!