如何使用@Qualifers与Dagger 2相同类型的多个实例?

时间:2017-08-15 12:00:05

标签: android dagger-2

我使用Dagger 2作为DI工具,我有多个数据源:

本地数据库 远程服务器 我有以下用例:

GetNewspaperUseCase 现在我希望能够从本地数据库和远程服务器获取报纸。

我有两个资格:

  • DatabaseQualifer
  • NetworkQualifer

这是Database qualifer:

@Qualifier
@Documented
@Retention(RUNTIME)
public @interface DatabaseQualifer {
 }

这是网络资格:

@Qualifier
@Documented
@Retention(RUNTIME)
public @interface NetworkQualifer {
}

我有模块:

@Module
public class NewspaperModule {

private GetNewspaperView getNewspaperView;
private DownloadNewspaperView downloadNewspaperView;
private SaveNewspaperView saveNewspaperView;

public NewspaperModule(GetNewspaperView getNewspaperView, DownloadNewspaperView downloadNewspaperView, SaveNewspaperView saveNewspaperView) {
    this.getNewspaperView = getNewspaperView;
    this.downloadNewspaperView = downloadNewspaperView;
    this.saveNewspaperView = saveNewspaperView;
}

@Provides
@FragmentScoped
@NetworkQualifer
NewspaperDataSource providesDataSource(Retrofit retrofit){
    return new NewspaperNetworkDataSource(retrofit);
}

@Provides
@FragmentScoped
@DatabaseQualifer
NewspaperDataSource providesDatabaseDataSource(AppDatabase appDatabase){
    return new NewspaperDatabaseDataSource(appDatabase);
}


@Provides
@FragmentScoped
NewspaperRepository provideRepository(NewspaperDataSource dataSource){
    return new NewspaperRepositoryImpl(dataSource);
}

@Provides
@FragmentScoped
@NetworkQualifer
GetNewspapersInteractor provideGetNewspaperInteractor(NewspaperRepository repository){
    return new GetNewspapersInteractorImpl(repository);
}

@Provides
@FragmentScoped
@DatabaseQualifer
GetNewspapersInteractor provideGetNewspaperFromDatabase(NewspaperRepository repository){
    return new GetNewspapersFromDatabaseInteractor(repository);
}

@Provides
@FragmentScoped
@NetworkQualifer
GetNewspaperPresenter provideGetNewspaperPresenter(GetNewspapersInteractor interactor){
    return new GetNewspaperPresenterImpl(interactor, getNewspaperView);
}

@Provides
@FragmentScoped
@DatabaseQualifer
GetNewspaperPresenter provideGetNewspaperFromDatabasePresenter(GetNewspapersInteractor interactor){
    return new GetNewspapersFromDatabasePresenter(interactor, getNewspaperView);
}

@Provides
@FragmentScoped
DownloadNewspaperInteractor provideDownloadNewspaperInteractor(NewspaperRepository repository){
    return new DownloadNewspaperInteractorImpl(repository);
}

@Provides
@FragmentScoped
DownloadNewspaperPresenter provideDownloadNewspaperPresenter(DownloadNewspaperInteractor interactor){
    return new DownloadNewspaperPresenterImpl(interactor, downloadNewspaperView);
}

@Provides
@FragmentScoped
SaveNewspaperInteractor provideSaveNewspaperInteractor(NewspaperRepository repository){
    return new SaveNewspaperInteractorImpl(repository);
}

@Provides
@FragmentScoped
SaveNewspaperPresenter provideSaveNewspaperPresenter(SaveNewspaperInteractor interactor){
    return new SaveNewspaperPresenterImpl(interactor, saveNewspaperView);
}

}

以下是Component:

中的代码
@FragmentScoped
@Component(dependencies = DataComponent.class, modules =    NewspaperModule.class)
public interface NewspaperComponent {
void inject(NewspaperFragment fragment);
}

现在我有片段,我正在尝试使用@NetworkQualifer @Inject,但是我收到了错误。

以下是Fragment的代码:

@Inject @NetworkQualifer GetNewspaperPresenter getNewspapersPresenter;

这是错误:

  

错误:(14,10)错误:如果没有@ Provide-annotated方法,则无法提供at.volksoper.volksoperrefactored.domain.newspapers.get.GetNewspapersInteractor。   at.volksoper.volksoperrefactored.domain.newspapers.get.GetNewspapersInteractor注入   at.volksoper.volksoperrefactored.presentation.newspapers.NewspaperModule.provideGetNewspaperPresenter(交互器)   @ at.volksoper.volksoperrefactored.presentation.newspapers.NetworkQualifer at.volksoper.volksoperrefactored.presentation.newspapers.get.GetNewspaperPresenter注入   at.volksoper.volksoperrefactored.presentation.newspapers.NewspaperFragment.getNewspapersPresenter   at.volksoper.volksoperrefactored.presentation.newspapers.NewspaperFragment注入   at.volksoper.volksoperrefactored.presentation.newspapers.NewspaperComponent.inject(片段)

0 个答案:

没有答案