gpt4 book ai didi

android - Dagger 2 和普通对象的 MVP

转载 作者:行者123 更新时间:2023-11-29 19:15:40 25 4
gpt4 key购买 nike

我在 Dagger 2 中使用 MVP 模式。

我的项目有两个使用公共(public)存储库的功能。这意味着我必须将此存储库注入(inject)两次,每个功能一次。但是当它尝试这样做时,我得到了这个错误:"...Repository is bound multiple times"

我发现这可以使用 @Named 来解决。所以我在我的模块中添加了它,但现在我收到一个新错误 “...没有 @Provides-annotated 方法就无法提供存储库。”

我想我必须在项目的其他地方添加这个 @Named 才能使其正常工作,因为我得到了一些解释这个的链接(比如这个 multiple instance of same object with named )。问题 我对这一切都很陌生,无法在项目架构的其他地方找到添加此 @Names 的位置。

所以,我实际上遇到了这个错误“...没有@Provides-annotated 方法就无法提供存储库。”

我的项目结构如下。

==== 包含这三个类的根包:

应用类

public class App extends Application {
private ApplicationComponent component;

@Override
public void onCreate() {
super.onCreate();

final String AUTH_TOKEN = getResources().getString(R.string.aqicn_token);
final String BASE_URL = getResources().getString(R.string.aqicn_api_base_url);

component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.pollutionApiModule(new PollutionApiModule(BASE_URL))
.pollutionLevelsModule(new PollutionLevelsModule())
.build();

}

public ApplicationComponent getComponent() {
return component;
}
}

ApplicationComponent 类

@Singleton
@Component(modules = {ApplicationModule.class, PollutionApiModule.class, PollutionLevelsModule.class, DonutModule.class})
public interface ApplicationComponent {

void injectPollutionLevels(PollutionLevelsFragment target);
void injectDonut(DonutFragment target);

}

ApplicationModule 类

@Module
public class ApplicationModule {
private Application application;
public ApplicationModule(Application application) {
this.application = application;
}

@Provides
@Singleton
public Context provideContext() {
return application;
}
}

==== 一个 pollutionlevels 包 包含一个 Dagger 模块,这个包是 MVP 结构的( fragment ,模型,模块,Presenter ...)并且与从中获取数据的单个功能相关我的公共(public)存储库。此功能的目的是将我的数据显示为文本:

PollutionLevelModule 类,你可以在这里看到我尝试添加@Name 注释来解决我的问题:

@Module
public class PollutionLevelsModule {
@Provides
public PollutionLevelsFragmentMVP.Presenter providePollutionLevelsFragmentPresenter(PollutionLevelsFragmentMVP.Model pollutionLevelsModel) {
return new PollutionLevelsPresenter(pollutionLevelsModel);
}

@Provides
public PollutionLevelsFragmentMVP.Model providePollutionLevelsFragmentModel(Repository repository) {
return new PollutionLevelsModel(repository);
}

@Singleton
@Provides
@Named("levelsRepo")
public Repository provideRepo(PollutionApiService pollutionApiService) {
return new CommonRepository(pollutionApiService);
}
}

此包包含一个 fragment ,我在其中将存储库注入(inject) onActivityCreated()。在这里,我调用了在我的 App 类(我在上面向您展示的类)中实现的方法 injectPollutionLevels():

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

((App) getActivity().getApplication()).getComponent().injectPollutionLevels(this);
}

==== 一个包含 Dagger 模块的 donut 包,这个包是 MVP 结构的( fragment 、模型、模块、Presenter...)并且与从中获取数据的单个功能相关我的公共(public)存储库。此功能的目的是将我的数据显示为图表:

DonutModule 类,你可以在这里看到我试图添加@Name 注释来解决我的问题:

@Module
public class DonutModule {
@Provides
public DonutFragmentMVP.Presenter providedDonutFragmentPresenter(DonutFragmentMVP.Model donutModel) {
return new DonutPresenter(donutModel);
}

@Provides
public DonutFragmentMVP.Model provideDonutFragmentModel(Repository repository) {
return new DonutModel(repository);
}

@Singleton
@Provides
@Named ("donutRepo")
public Repository provideRepo(PollutionApiService pollutionApiService) {
return new CommonRepository(pollutionApiService);
}
}

此包包含一个 fragment ,我在其中将存储库注入(inject) onActivityCreated()。在这里,我调用了在我的 App 类(我在上面向您展示的类)中实现的方法 injectDonut():

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

((App) getActivity().getApplication()).getComponent().injectDonut(this);
}

==== 包含我的存储库的通用包

public class CommonRepository implements Repository {
private PollutionApiService pollutionApiService;

public CommonRepository(PollutionApiService pollutionApiService) {
this.pollutionApiService = pollutionApiService;
}

@Override
public Observable<Aqicn> getDataFromNetwork(String city, String authToken) {
Observable<Aqicn> aqicn = pollutionApiService.getPollutionObservable(city, authToken);

return aqicn;
}
}

如果可以帮助您更好地指出如何解决此问题,这是我的架构的屏幕截图。如果您需要更多源代码,请告诉我。谢谢。

enter image description here

最佳答案

只要提供@Named Repository,还需要索要@Named Repository

@Provides
public DonutFragmentMVP.Model provideDonutFragmentModel(@Named("donutRepo") Repository repository) {
return new DonutModel(repository);
}

关于android - Dagger 2 和普通对象的 MVP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43602016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com