I've got two cases where I can use whether an id of an Entity or pass it as reference.
1) Domain Services. Example:
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) Entity. Example:
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
}
}
Which approach is better and why?