我遇到了一个问题,需要注入一个泛型类型接口。不确定如何实现或搜索相关内容,因为我不知道要搜索哪些确切术语。如果我完全错误,请原谅,我刚开始使用dagger。
基本上,我有一个用例类。
public class LoadConversations<C extends IConversation>
extends UseCase<List<C>, LoadConversations.Type> {
private final IConversationRepository<C> messageRepository;
@Inject LoadConversations(@NonNull IConversationRepository<C> messageRepository) {
this.messageRepository = messageRepository;
}
....
public enum Type {
ALL, NEWER, OLDER
}
}
使用接口IConversationRepository
。
public interface IConversationRepository<C extends IConversation> {
Observable<List<C>> conversations(LoadConversations.Type params);
}
IConversation
是一个空白接口,ConversationModule
则提供 IConversationRepository
。
我使用以下代码进行注入时遇到了问题。是否有什么地方遗漏或者完全错误?谢谢!
尝试按照以下方式提供:
@Provides IConversationRepository<Conversation> provideConversationRepository(
ConversationRepository conversationRepository) {
return conversationRepository;
}
我正在尝试将这个注入到我的演示者中
private final LoadConversations<Conversation> loadConversations;
@Inject public ConversationListPresenter(LoadConversations<Conversation> loadConversations) {
this.loadConversations = loadConversations;
}
实现ConversationRepository
public class ConversationRepository implements IConversationRepository<Conversation> {
@Override public Observable<List<Conversation>> conversations(LoadConversations.Type params) {
....
}
}
错误日志:
Error:(15, 10) error: com.rbttalk.android.data.repository.ConversationRepository cannot be provided without an @Inject constructor or from an @Provides-annotated method.
com.rbttalk.android.data.repository.ConversationRepository is injected at
com.rbttalk.android.di.module.sub_modules.ConversationModule.provideConversationRepository(conversationRepository)
com.rbttalk.android.domain.repository.IConversationRepository<com.rbttalk.android.domain.models.Conversation> is injected at
com.rbttalk.android.domain.usecase.conversation.LoadConversations.<init>(arg0, …)
com.rbttalk.android.domain.usecase.conversation.LoadConversations<com.rbttalk.android.domain.models.Conversation> is injected at
com.rbttalk.android.ui.main.conversation.ConversationListPresenter.<init>(loadConversations)
com.rbttalk.android.ui.main.conversation.ConversationListPresenter is injected at
com.rbttalk.android.ui.main.conversation.ConversationListFragment.userListPresenter
com.rbttalk.android.ui.main.conversation.ConversationListFragment is injected at
com.rbttalk.android.di.component.ConversationComponent.inject(conversationListFragment)