以下哪些示例代表了 DDD 的正确使用?
我已经使用 DDD 几个月了,遇到了一些我不确定的事情。
以将 Product
添加到 Order
对象的简单示例为例。从我们的控制器中,我们将通过 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);
}
}
控制器实例化产品本身并通过以下方法添加它:
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 AddProduct(int productId, IProductRepository productRepository)
{
Product product = productRepository.GetProduct(productId);
productList.Add(product);
return product;
}
我目前已经选择了第一个示例,因为您的域模型永远不应该在内部调用服务方法,但是我最近看到了一些使用我的第二个示例的示例,它看起来确实很整洁。在我看来,示例一已接近贫血。 示例二会将所有产品添加逻辑移至域模型本身。
I've been working with DDD for a few months now and I've come across a few things I'm unsure of.
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)?
Example One:
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);
}
Example Two:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第二个是可怕的...
按订单添加产品不应在其签名上包含存储库,因为存储库不是域的一部分。
我倾向于选择第一个。
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.
I tend to go with the first one.
是的,伙计,第一个更好......
就好像我们以对象的形式思考......
将产品添加到列表与产品存储库无关,它应该只需要产品。
Yeah buddy the First one is better...
As if we think in form of objects...
Adding the product to the list has nothing to do with the product repository, it should take only the product.