Domain driven design 存储库和批量更新—避免数据库往返

Domain driven design 存储库和批量更新—避免数据库往返,domain-driven-design,repository-pattern,ddd-repositories,Domain Driven Design,Repository Pattern,Ddd Repositories,我在批发系统领域工作。当某些产品交付时,会触发域NewProductsDeliveredEvent。事件包含一组包含产品代码和数量的ProductDeliveryvalue对象。如下所示: class NewProductsDeliveredEvent { Set<ProductDelivery> productDeliveries; } class ProductDelivery { ProductCode productCode; Quantity quantit

我在批发系统领域工作。当某些产品交付时,会触发域
NewProductsDeliveredEvent
。事件包含一组包含产品代码和数量的
ProductDelivery
value对象。如下所示:

class NewProductsDeliveredEvent {
  Set<ProductDelivery> productDeliveries;
}

class ProductDelivery { 
  ProductCode productCode;
  Quantity quantity
}
很容易发现这样的逻辑会产生大量的DB往返,我正在考虑一些解决方案来减轻痛苦。一个想法可能是使用
规范
模式和产品代码的构建或规范。然而,在我的应用程序中,产品代码是一个业务标识符,所以这个解决方案有点异味(也许我只是夸大了)


有没有更好的办法处理?非常感谢任何想法。

如果您允许稍微偏离主题,但您确定批量更新对您来说是个好主意吗

如果管理库存,则产品是高含量的集合。试想一下,可能有数百人同时在Amazon.com上订购同一产品,而很少有人会同时修改您的订单

例如:

Event1: A-5, B-1 Event2: C-1, D-2 Event3: A-2, D-3
也许将事件拆分为只触发一个产品更新的多个子事件更合适。

为什么不只使用productRepo.getByCodes(…)?因为我从未见过使用这种方法的存储库,对我来说,这似乎是一些设计缺陷(但肯定是最简单的解决方案;)“product.updateQuantity”是什么(delivery.getQuantity());“是否更新库存?@Hippoom产品保持其可用数量,因此updateQuantity实际上减少了它(我知道,方法应该有一个更好的名称)非常好的观点!非常感谢,我刚刚忽略了这一方面。我知道repo.getById(IDs…)有问题@我相信批量更新在某些情况下仍然是有用的,这取决于你的领域。我在一个预订项目上工作了好几年,我们在一次交易中更新了订单和产品。它工作得很好,因为大多数订单来自b2b营销渠道(销售办事处、代理商),而且吞吐量不是很高。 Event1: A-5, B-1 Event2: C-1, D-2 Event3: A-2, D-3
handle(NewProductDeliveryEvent event) {
    for (ProductDelivery delivery : event.getProductDeliveries()) {
        updateProductTransactionally(); 
        // How to retry the whole event 
        // when the second one failed and the first one committed?
    }
}