【发布时间】:2013-08-07 21:23:42
【问题描述】:
我在批发系统领域工作。部分商品发货时,会触发域名NewProductsDeliveredEvent。事件包含一组包含产品代码和数量的ProductDelivery 值对象。如下所示:
class NewProductsDeliveredEvent {
Set<ProductDelivery> productDeliveries;
}
class ProductDelivery {
ProductCode productCode;
Quantity quantity
}
到目前为止一切顺利。现在,当负责库存更新的组件收到此类事件时。它必须使用当前可用产品数量更新产品表。所以我有类似的东西:
class NewProudctsDeliveredHandler {
ProductRepository productRepo;
handle(NewProductDeliveryEvent event) {
for (ProductDelivery delivery : event.getProductDeliveries()) {
Product product = productRepo.getByCode(delivery.getProductCode())
product.updateQuantity(delivery.getQuantity());
}
}
}
很容易发现这样的逻辑会产生大量的数据库往返,我正在考虑一些解决方案来减轻痛苦。一种想法可能是使用Specification 模式并为产品代码构建 OR 规范。但是,在我的应用程序中,产品代码是一个业务标识符,所以这个解决方案有点味道(也许我只是在夸大其词)。
有没有更好的处理方法?任何想法都非常感谢。
【问题讨论】:
-
为什么不只是 productRepo.getByCodes(...)?
-
因为我从未见过使用这种方法的存储库,对我来说这似乎是一些设计缺陷(但可以肯定,这将是最简单的解决方案;))
-
什么是“product.updateQuantity(delivery.getQuantity());”做?更新库存?
-
@Hippoom Product 持有它的可用数量,所以 updateQuantity 实际上会减少它(我知道,方法应该有一个更好的名称)
标签: domain-driven-design repository-pattern ddd-repositories