【发布时间】:2015-12-31 04:19:46
【问题描述】:
我的应用中有以下 Dagger2 架构:
-- AppComponent (@PerApplication)
-- UserComponent (@PerUser)
-- ActivityComponent (@PerActivity)
-- ChatComponent (@PerActivity) <-- 1
在哪里: 应用组件:
@PerApplication
@Component(modules = {ApplicationModule.class, StorageModule.class, NetworkModule.class})
public interface ApplicationComponent {
UserComponent plus(UserModule userComponent);
//Exposed to sub-graphs.
Context application();
}
用户组件:
@PerUser
@Subcomponent(modules = {UserModule.class, RosterModule.class})
public interface UserComponent {
ActivityComponent plus(ActivityModule activityModule);
User getMe();
UserRepository userRepository();
}
活动组件:
@PerActivity
@Subcomponent(modules = ActivityModule.class)
public interface ActivityComponent {
ChatComponent plus(ChatModule chatComponent);
//Exposed to sub-graphs.
Context context();
}
聊天组件:
@PerActivity
@Subcomponent(modules = {ChatModule.class})
public interface ChatComponent {
void inject(ChatListFragment chatListFragment);
void inject(ConversationFragment conversationFragment);
void inject(NewConversationFragment newConversationFragment);
void inject(CloudFilesFragment cloudFilesFragment);
void inject(ChatActivity chatActivity);
void inject(ConversationActivity conversationActivity);
void inject(NewConversationActivity newConversationActivity);
void inject(NewGroupActivity newGroupActivity);
void inject(NewGroupFragment newGroupFragment);
}
我面临两个问题:
首先,如何将不同的Context 注入到我的课程中?应用或活动??
其次,我在尝试编译代码时遇到了一个奇怪的问题,错误是:
错误:(23, 10) 错误: br.com.animaeducacao.ulife.domain.interactor.UseCase 不能 在没有 @Provides 注释的方法的情况下提供。 br.com.animaeducacao.ulife.presentation.view.fragment.ChatListFragment.chatListPresenter [类型的注入字段: br.com.animaeducacao.ulife.presentation.presenter.ChatListPresenter 聊天列表演示者] br.com.animaeducacao.ulife.presentation.presenter.ChatListPresenter.(br.com.animaeducacao.ulife.domain.interactor.UseCase 聊天对话用例, br.com.animaeducacao.ulife.domain.interactor.UseCase adviceUserPresence,android.content.Context 上下文)[参数: @javax.inject.Named("getChatDialogs") br.com.animaeducacao.ulife.domain.interactor.UseCase chatDialogsUseCase]
我的 ChatListFragment 是:
@PerActivity
public class ChatListFragment extends BaseFragment implements ChatListView {
@Inject
ChatListPresenter chatListPresenter;
...
//called onActivityCreated()
private void initialize() {
this.getComponent(ChatComponent.class).inject(this);
}
基本片段:
protected <C> C getComponent(Class<C> componentType) {
return componentType.cast(((HasComponent<C>)getActivity()).getComponent());
}
ChatListPresenter:
@PerActivity
public class ChatListPresenter implements Presenter {
private final UseCase chatDialogsUseCase;
private final UseCase adviceUserPresence;
private final Context context;
private ChatListView chatListView;
@Inject
public ChatListPresenter(@Named("getChatDialogs") UseCase chatDialogsUseCase,
@Named("adviceUserPresence") UseCase adviceUserPresence,
Context context) {
this.chatDialogsUseCase = chatDialogsUseCase;
this.adviceUserPresence = adviceUserPresence;
this.context = context;
}
问题是,在我的 ChatModule 类中,我已经实现了所有必要的 @Provides:
@Provides
@PerActivity
@Named("getChatDialogs")
public UseCase provideChatDialogs(@Named("transactionalChatRepository") ChatRepository chatRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread) {
return new GetUserChatDialogs(chatRepository, threadExecutor, postExecutionThread);
}
这是一个好方法吗?为什么这不编译,我在这里缺少什么? 抱歉发了这么长的帖子,谢谢!
【问题讨论】:
-
请阅读更多关于匕首的教程。您得到的错误可能是因为您只提供了一个命名用例。另外,你不应该关心
context。如果你需要activity,注入activity,如果你需要上下文,你可能应该去app context