1

很多讨论,比如thisthis,都使用RICH DOMAIN MODEL

amenic有两个强有力的理由,例如 1 和 3:

现在假设我需要确保我需要验证产品是否存在于库存中,如果不存在则抛出异常。

所以有一个问题:如果我们不需要一个对象依赖于ISomeRepository类似的服务,我们可以这样:

public void Order.AddOrderLine(IEnumerable<Product> products, Product product)
{
    if(!prosucts.Contains(product))
         throw new AddProductException

    OrderLines.Add(new OrderLine(product));
}

并这样称呼它:

Order.AddOrderLine(ISomeRepository.GetAll(), product);
4

2 回答 2

1

听起来您的域中缺少一个概念。我会考虑引入某种StoreInventory实体,以便产品从库存中移出并进入订单(这在许多商业领域中称为“拣货”)。

interface StoreInventory
{
    IEnumerable<Product> AvailableProducts { get; }
    Product PickProduct(guid productId); // This doesn't have to be an Id, it could be some other key or a specification.
}

void Order.AddOrderLine(StoreInventory inventory, Product product)
{
    if (!inventory.AvailableProducts.Contains(product.Id))
        throw new AddProductException();

    var item = inventory.Pick(product);
    OrderLines.Add(new OrderLine(item);
}

对我来说,这似乎更接近现实。但与 DDD 一样,只有您的领域专家才能告诉您事情应该如何流动。

这在未来似乎也更具可扩展性,例如使用这种模型可以很容易地引入多个商店 - 每个商店都有自己的库存。

于 2013-12-17T19:39:03.043 回答
0

如果你读过 DDD,你会发现 Repository 是 DDD 的核心概念。在 DDD 中,存储库是域的一部分,它说明了域需要什么样的行为才能持久工作。在您的情况下,您可以简单地将存储库传递给方法并让方法提取必要的数据。

public void Order.AddOrderLine(ISomeRepository repo, Product product)
{
    if(!repo.ProductExists(product))
         throw new AddProductException

    OrderLines.Add(new OrderLine(product));
}

// call it like
Order.AddOrderLine(someRepository, product);

我认为您的困惑问题在于存储库,或者更准确地说,它是抽象,通常与持久性本身相关。这实际上是对整个模式的误解造成的误解。正确的存储库由两部分组成:抽象,通常由接口表示,它定义了域需要从持久性中进行的操作。以及具体的实现,它通过使用的持久性技术实现这些操作。

于 2013-12-17T19:30:16.990 回答