エンティティの ID を使用するか、参照として渡すかを使用できる 2 つのケースがあります。
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
}
}
どちらのアプローチが優れているか、またその理由は?