直接の依存性のみ注入せよ
他のオブジェクトを取得するために注入することを避けよ。例えば、Accountを取得するためにCustomerを注入してはいけない。
代わりに、依存するクラスを直接注入せよ。これはテストを簡単にする。テストはテスト自身とCustomerにだけ注意すればよい。@Providesを付けたメソッドを使い、CustomerからAccountをバインドする。
依存性を直接注入することで、コードはシンプルになる。
public class ShowBudgets {
    private final Account account;
    @Inject
    ShowBudgets(Customer customer) {
        account = customer.getPurchasingAccount();
    }
代わりに、依存するクラスを直接注入せよ。これはテストを簡単にする。テストはテスト自身とCustomerにだけ注意すればよい。@Providesを付けたメソッドを使い、CustomerからAccountをバインドする。
public class CustomerModule extends AbstractModule {
    @Override public void configure() {
        ...
    }
    @Provides
    Account providePurchasingAccount(Customer customer) {
        return customer.getPurchasingAccount();
    }
依存性を直接注入することで、コードはシンプルになる。
public class ShowBudgets {
    private final Account account;
    @Inject
    ShowBudgets(Account account) {
        this.account = account;
    }
0 件のコメント:
コメントを投稿