gpt4 book ai didi

domain-driven-design - DDD 中的标识符与引用

转载 作者:行者123 更新时间:2023-12-04 08:20:49 25 4
gpt4 key购买 nike

我有两种情况可以使用实体的 id 或将其作为引用传递。

1) 领域服务。示例:

class ProductService {
public void changePrice(Product product, long newPrice) {
// significant calculations involving several Entities here...
}
// versus
public void changePrice(long productId, long newPrice) {
Product product = productRepository.get(productId);
// significant calculations involving several Entities here...
}
}

2) 实体。示例:

class Order {
public void addItem(Product product, long quantity) {
// do stuff
}
// versus
public void addItem(long productId, long quantity) {
Product product = Registry.productRepository().get(productId);
// do stuff
}
// or even maybe this ugly version?
public void addItem(ProductRepository productRepository, long productId, long quantity) {
Product product = productRepository.get(productId);
// do stuff
}
}

哪种方法更好,为什么?

最佳答案

我喜欢 Onion Architecture了解如何从概念上思考如何保护您的域免受外部影响。

IMO,最好将存储库放在域之外。让域外的层解析域实体,然后在域内使用它们

所以,我显然更愿意看到直接使用 Product 的示例(引用)。存储库的实现不在域中。域不应该被 ID、配置或持久性弄得乱七八糟。它应该直接关注域问题,并尽可能清晰。

关于domain-driven-design - DDD 中的标识符与引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25855165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com