Dagger 2 - 提供依赖项时出错
Dagger 2 - Error while providing a dependency
我真的是 Dagger 2 的新手,我知道它的工作原理和功能,但是我在尝试将它实施到我的项目中时遇到了一些问题。
我现在的 objective 只是将演示者注入我的视图,目标是解耦我做事的视图
presenter = Presenter(myInteractor())
这是我试过的
MyAppApplication
class MyAppApplication: Application() {
lateinit var presentationComponent: PresentationComponent
override fun onCreate() {
super.onCreate()
createPresentationComponent()
}
private fun createPresentationComponent() {
presentationComponent = DaggerPresentationComponent.builder()
.presentationModule(PresentationModule(this))
.build()
}
}
PresentationComponent
@Component(modules = arrayOf(PresentationModule::class))
@Singleton
interface PresentationComponent {
fun inject(loginActivity: LoginView)
fun loginUserPresenter(): LoginPresenter
}
演示模块
@Module
class PresentationModule(application: Application) {
@Provides @Singleton fun provideLoginUserPresenter(signInInteractor: SignInInteractor): LoginPresenter {
return LoginPresenter(signInInteractor)
}
}
SignInInteractor
interface SignInInteractor {
interface SignInCallBack {
fun onSuccess()
fun onFailure(errormsg:String)
}
fun signInWithEmailAndPassword(email:String,password:String,listener:SignInCallBack)
fun firebaseAuthWithGoogle(account: GoogleSignInAccount, listener:SignInCallBack)
}
现在,我认为这就是将交互器毫无问题地注入我的演示者然后将演示者注入我的视图所需的全部内容,但是给我这个错误
error: [Dagger/MissingBinding] com.myapp.domain.interactor.logininteractor.SignInInteractor cannot be provided without an @Provides-annotated method.
我有点困惑,因为如果我只提供负责将我的 signInInteractor 绑定到我的 Presenter 的 presentationModule,它应该可以工作,但实际上没有。
在此先感谢您的帮助
正如错误消息所说,您正试图将 PresentationModule
中的 SignInInteractor
传递给 LoginPresenter
,但您并未在任何地方提供它的实现.一种可能的解决方案是将以下代码块添加到您的 PresentationModule
:
@Provides @Singleton fun provideSignInInteractor(): SignInInteractor {
return TODO("Add an implementation of SignInInteractor here.")
}
当然,TODO
需要替换为您选择的 SignInInteractor
(例如,myInteractor()
函数就可以)。然后 SignInInteractor
将被您的 LoginPresenter
使用。希望对您有所帮助!
我真的是 Dagger 2 的新手,我知道它的工作原理和功能,但是我在尝试将它实施到我的项目中时遇到了一些问题。
我现在的 objective 只是将演示者注入我的视图,目标是解耦我做事的视图
presenter = Presenter(myInteractor())
这是我试过的
MyAppApplication
class MyAppApplication: Application() {
lateinit var presentationComponent: PresentationComponent
override fun onCreate() {
super.onCreate()
createPresentationComponent()
}
private fun createPresentationComponent() {
presentationComponent = DaggerPresentationComponent.builder()
.presentationModule(PresentationModule(this))
.build()
}
}
PresentationComponent
@Component(modules = arrayOf(PresentationModule::class))
@Singleton
interface PresentationComponent {
fun inject(loginActivity: LoginView)
fun loginUserPresenter(): LoginPresenter
}
演示模块
@Module
class PresentationModule(application: Application) {
@Provides @Singleton fun provideLoginUserPresenter(signInInteractor: SignInInteractor): LoginPresenter {
return LoginPresenter(signInInteractor)
}
}
SignInInteractor
interface SignInInteractor {
interface SignInCallBack {
fun onSuccess()
fun onFailure(errormsg:String)
}
fun signInWithEmailAndPassword(email:String,password:String,listener:SignInCallBack)
fun firebaseAuthWithGoogle(account: GoogleSignInAccount, listener:SignInCallBack)
}
现在,我认为这就是将交互器毫无问题地注入我的演示者然后将演示者注入我的视图所需的全部内容,但是给我这个错误
error: [Dagger/MissingBinding] com.myapp.domain.interactor.logininteractor.SignInInteractor cannot be provided without an @Provides-annotated method.
我有点困惑,因为如果我只提供负责将我的 signInInteractor 绑定到我的 Presenter 的 presentationModule,它应该可以工作,但实际上没有。
在此先感谢您的帮助
正如错误消息所说,您正试图将 PresentationModule
中的 SignInInteractor
传递给 LoginPresenter
,但您并未在任何地方提供它的实现.一种可能的解决方案是将以下代码块添加到您的 PresentationModule
:
@Provides @Singleton fun provideSignInInteractor(): SignInInteractor {
return TODO("Add an implementation of SignInInteractor here.")
}
当然,TODO
需要替换为您选择的 SignInInteractor
(例如,myInteractor()
函数就可以)。然后 SignInInteractor
将被您的 LoginPresenter
使用。希望对您有所帮助!