dagger2 组件 - 在一个界面中没有那么多注入调用的设计模式

dagger2 components - design pattern to not have so many inject calls in one interface

我正在寻找一种我可以使用的方法或设计模式,以便 dagger2 组件不会有太多的注入调用。让我们看看我在企业层面的意思:

Singleton
          @Component(modules = {AppModule.class, NetworkModule.class, RepositoryModule.class})
          public interface AppComponent {

              void inject(UserDataRepository target);

              void inject(DoStandardLoginUsecase target);

              void inject(NetworkSessionManager target);

              void inject(SharedPrefRepo target);

              void inject(getSharedPrefUsecase target);

              ActivitySubComponent plus(ActivityModule activityModule);

              PresenterSubComponent plus(PresenterModule presenterModule, UseCaseModule useCaseModule);
}
//.. this list is going to be gigantic in a year. how can i minimize it or make the inject calls somewhere else or group them ?

我需要一种方法来在注入调用失控之前将它们分组到其他地方。

请记住,您的组件是对象图中 入口点 的列表,并且您的对象可以相互依赖,而无需在组件上列出。无论您使用构造函数注入还是模块配置,都是如此。因此,您的组件可能只包含 Dagger 不能或不应实例化自身的 类 的 inject,包括您的应用程序、活动、片段和其他系统服务。

在调用构造函数后,Dagger 将填充 @Inject 字段并调用 @Inject 方法本身也是事实:

public class UserDataRepository {
  @Inject public UserDataRepository() {}  /** So Dagger knows how to create one */

  @Inject FieldOne fieldOne;
  @Inject FieldTwo fieldTwo;

  @Inject void initialize(DepThree depThree, DepFour depFour) {
    // ...
  }

  // ...
}

虽然我个人更喜欢构造函数注入,所以我可以将这些字段设置为 final,但您可能更喜欢让它们保持可变和 @Inject-注释;这并不意味着您需要将它们列在您的组件中或使用模块构建它们。