简体   繁体   English

我如何使用匕首将对象注入Android Kotlin MVP mosby应用程序的演示者中

[英]How can i inject object into presenter in android kotlin MVP mosby app with dagger

I am trying to get dagger working in my application. 我正在尝试使匕首在我的应用程序中正常工作。 After creating Module Component and MyApp i can use dagger to inject database service into view but i am having trouble doing same thing with presenter. 创建模块组件和MyApp之后,我可以使用匕首将数据库服务注入视图,但是我在使用主持人做同样的事情时遇到了麻烦。 Code: 码:

class MyApp : Application() {

    var daoComponent: DaoComponent? = null
    private set

    override fun onCreate() {
        super.onCreate()

        daoComponent = DaggerDaoComponent.builder()
            .appModule(AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
            .daoModule(DaoModule())
            .build()
    }
}

Module

@Module
class DaoModule {
    @Provides
    fun providesEstateService(): EstateService = EstateServiceImpl()
}

Component 零件

@Singleton
@Component(modules = arrayOf(AppModule::class, DaoModule::class))
interface DaoComponent {
    fun inject(activity: MainActivity)
}

AppModule 的AppModule

@Module
class AppModule(internal var mApplication: Application) {

    @Provides
    @Singleton
    internal fun providesApplication(): Application {
        return mApplication
    }
}

MainActivity 主要活动

class MainActivity : MvpActivity<MainView, MainPresenter>(), MainView {
    @Inject
    lateinit var estateService : EstateService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        (application as MyApp).daoComponent!!.inject(this)estateService.numberOfInvoicedEstates.toString()
    }

    override fun createPresenter(): MainPresenter = MainPresenterImpl()
}

After injecting estateService this way I can use it, but I cant figure out how do I inject service directly into the presenter. 以这种方式注入estateService之后,我可以使用它,但是我无法弄清楚如何将服务直接注入到演示者中。 I tried doing it like this but it isn't working. 我试图这样做,但它没有用。 Should I just pass injected objects from the activity? 我应该只从活动中传递注入的对象吗? or maybe I should pass MyApp as an argument or make static method allowing my to get it from any place in the application? 还是应该将MyApp作为参数传递或使用静态方法允许我从应用程序中的任何位置获取它?

class MainPresenterImpl 
    @Inject
    constructor(): MvpBasePresenter<MainView>(),MainPresenter {
        @Inject
        lateinit var estateService : EstateService
}

Your component should provide the presenter like that: 您的组件应像这样向演示者提供:

@Component(modules = arrayOf(AppModule::class, DaoModule::class))
@Singleton
interface MyComponent {
   mainPresenter() : MainPresenter
}

And all dependencies the presenter needs are injected to the presenter via constructor parameters: 演示者需要的所有依赖项都通过构造函数参数注入到演示者中:

class MainPresenterImpl 
    @Inject constructor(private val estateService : EstateService ) :
    MvpBasePresenter<MainView>(),MainPresenter {
   ...
}

Than in createPresenter() just grab the presenter from dagger component like this: 比在createPresenter()只需从匕首组件中获取演示者即可,如下所示:

class MainActivity : MvpActivity<MainView, MainPresenter>(), MainView {

    ...

    override fun createPresenter(): MainPresenter =  
       (application as MyApp).myComponent().mainPresenter()
}

Please note that the code shown above will not compile. 请注意,上面显示的代码将无法编译。 This is just pseudocode to give you an idea how the dependency graph could look like in Dagger. 这只是伪代码,可以使您了解Dagger中的依赖图。

请参考示例,以了解如何结合使用Dagger 2和MVP。

You have to tell your component where you want to inject it. 您必须告诉组件要在哪里注入它。

So, try with this component: 因此,请尝试使用此组件:

@Singleton
@Component(modules = arrayOf(AppModule::class, DaoModule::class))
interface DaoComponent {
    fun inject(presenter: MainPresenterImpl)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM