Dagger 2混合范围

时间:2015-11-10 00:31:37

标签: android dependency-injection scope components dagger-2

以前我只有一个带有四个模块的 AppComponent AppModule NetworkModule StorageModule PresentersModule )并在各处注入单身人士。最近,我决定在我的应用程序中进行小型重构并将其划分为范围。我认为,主持人只能在活动中生活,所以我创建了 @ActivityScope ActivityModule ,但由于我误解了如何混合这些范围,所以无法编译项目。 我已经在stackoverflow上阅读了很多文章和问题,但是到处都有模块独立的简单例子。在我的情况下,像

@Singleton
@Component(modules = { AppModule.class, StorageModule.class, NetworkModule.class })
public interface AppComponent {
  ActivityComponent plus(PresentersModule module); // <-- error
}

无效。我收到这个错误:

Error:(19, 1) error: com.my.package.di.component.ActivityComponent scoped with @com.my.package.di.scope.ActivityScope may not reference bindings with different scopes:
@Provides @Singleton android.app.Application com.my.package.di.module.AppModule.provideApplication()
@Provides @Singleton com.my.package.network.FeedBurnerApi com.my.package.di.module.NetworkModule.provideFeedBurnerApi(android.app.Application)
@Provides @Singleton android.database.sqlite.SQLiteOpenHelper com.my.package.di.module.StorageModule.provideSQLiteOpenHelper(android.app.Application)
@Provides @Singleton com.my.package.storage.Repository com.my.package.di.module.StorageModule.provideRepository(android.database.sqlite.SQLiteOpenHelper)
@Provides @Singleton com.my.package.SharedPreferencesHelper com.my.package.di.module.StorageModule.provideSharedPreferencesHelper(android.app.Application)

所以,问题是如何获取ActivityComponent的实例?

您可以在下面看到模块之间的依赖关系:

申请模块:

@Module
public final class AppModule {
  private final MyApplication mApplication;

  public AppModule(MyApplication application) { ... }

  @Provides @Singleton Application provideApplication() { ... }
}

网络模块:

@Module(includes = { AppModule.class })
public final class NetworkModule {
  @Provides @Singleton FeedBurnerApi provideFeedBurnerApi(Application application) { ... }

  @Provides @Singleton Retrofit provideRetrofit() { ... }
}

存储模块:

@Module(includes = { AppModule.class })
public final class StorageModule {
  @Provides @Singleton Repository provideRepository(SQLiteOpenHelper sqLiteOpenHelper) { ... }

  @Provides @Singleton SQLiteOpenHelper provideSQLiteOpenHelper(Application application) { ... }

  @Provides @Singleton SharedPreferencesHelper provideSharedPreferencesHelper(Application application) { ... }
}

演示者模块:

@Module(includes = { AppModule.class, NetworkModule.class, StorageModule.class })
public final class PresentersModule {
  @Provides FeedPageViewPresenter provideFeedPageViewPresenter(FeedBurnerApi api, Repository repository, SharedPreferencesHelper preferences) { ... }

  @Provides @ActivityScope SlidingTabsViewPresenter provideSlidingTabsViewPresenter(Repository repository) { ... }
}

申请组件:

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

活动组件:

@Subcomponent(modules = PresentersModule.class)
@ActivityScope
public interface ActivityComponent {
  void inject(FeedPageView view);

  void inject(SlidingTabsView view);
}

1 个答案:

答案 0 :(得分:6)

问题出在我的 PresentersModule

由于子组件可以访问父母的整个对象图,因此我不需要在子模块中包含这些依赖项。 所以,我改变了这段代码:

@Module(includes = { AppModule.class, NetworkModule.class, StorageModule.class })
public final class PresentersModule { ... }

用这个:

@Module
public final class PresentersModule { ... }

它解决了我的问题。