3

エンティティの 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
    }
}

どちらのアプローチが優れているか、またその理由は?

4

1 に答える 1

2

外部の影響からドメインを保護する方法を概念的に考える方法についてのオニオン アーキテクチャビューが気に入っています。

IMO、リポジトリをドメイン外に保持するのが最善です。ドメインの外側のレイヤーでドメイン エンティティを解決し、ドメインの内側でそれらを使用します

したがって、私は明らかに、直接使用する例を見たいと思いますProduct(リファレンス)。リポジトリの実装はドメインにありません。ドメインは、ID、構成、または永続性で乱雑にすべきではありません。代わりに、ドメインの問題に直接、できるだけ明確に焦点を当てる必要があります。

于 2014-09-15T19:29:08.103 に答える