Dagger 2 混合瞄准镜
Dagger 2 mixed scopes
以前我只有一个 AppComponent 有四个模块(AppModule, NetworkModule, StorageModule, PresentersModule) 和到处注入的单例。最近,我决定在我的应用程序中进行小型重构并将其划分为范围。我认为,演示者只能生活在活动中,所以我创建了 @ActivityScope 和 ActivityModule,但是由于我误解了如何混合这些范围,因此无法编译项目。
我在 Whosebug 上阅读了很多文章和问题,但到处都有模块独立的简单示例。在我的例子中
@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)
所以,问题是如何获取我的 Activity组件的实例?
您可以在下面看到模块之间的依赖关系:
应用模块:
@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 {}
Activity分量:
@Subcomponent(modules = PresentersModule.class)
@ActivityScope
public interface ActivityComponent {
void inject(FeedPageView view);
void inject(SlidingTabsView view);
}
问题出在我的 PresentersModule。
由于子组件可以从它们的父组件访问整个对象图,我不需要在我的子模块中包含这些依赖项。
所以,我更改了这段代码:
@Module(includes = { AppModule.class, NetworkModule.class, StorageModule.class })
public final class PresentersModule { ... }
有了这个:
@Module
public final class PresentersModule { ... }
它解决了我的问题。
以前我只有一个 AppComponent 有四个模块(AppModule, NetworkModule, StorageModule, PresentersModule) 和到处注入的单例。最近,我决定在我的应用程序中进行小型重构并将其划分为范围。我认为,演示者只能生活在活动中,所以我创建了 @ActivityScope 和 ActivityModule,但是由于我误解了如何混合这些范围,因此无法编译项目。 我在 Whosebug 上阅读了很多文章和问题,但到处都有模块独立的简单示例。在我的例子中
@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)
所以,问题是如何获取我的 Activity组件的实例?
您可以在下面看到模块之间的依赖关系:
应用模块:
@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 {}
Activity分量:
@Subcomponent(modules = PresentersModule.class)
@ActivityScope
public interface ActivityComponent {
void inject(FeedPageView view);
void inject(SlidingTabsView view);
}
问题出在我的 PresentersModule。
由于子组件可以从它们的父组件访问整个对象图,我不需要在我的子模块中包含这些依赖项。 所以,我更改了这段代码:
@Module(includes = { AppModule.class, NetworkModule.class, StorageModule.class })
public final class PresentersModule { ... }
有了这个:
@Module
public final class PresentersModule { ... }
它解决了我的问题。