我有两种情况可以使用实体的 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
}
}
哪种方法更好,为什么?