我如何使用匕首将对象注入 android kotlin MVP mosby 应用程序中的演示者
How can i inject object into presenter in android kotlin MVP mosby app with dagger
我正在尝试让 dagger 在我的应用程序中工作。
创建模块组件和 MyApp 后,我可以使用匕首将数据库服务注入视图,但我无法用演示者做同样的事情。
代码:
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
class DaoModule {
@Provides
fun providesEstateService(): EstateService = EstateServiceImpl()
}
组件
@Singleton
@Component(modules = arrayOf(AppModule::class, DaoModule::class))
interface DaoComponent {
fun inject(activity: MainActivity)
}
AppModule
@Module
class AppModule(internal var mApplication: Application) {
@Provides
@Singleton
internal fun providesApplication(): Application {
return mApplication
}
}
主要活动
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()
}
以这种方式注入 estateService 后,我可以使用它,但我不知道如何将服务直接注入演示者。
我试过这样做,但没有用。
我应该只传递来自 activity 的注入对象吗?或者我应该将 MyApp 作为参数传递或创建静态方法以允许我从应用程序的任何位置获取它?
class MainPresenterImpl
@Inject
constructor(): MvpBasePresenter<MainView>(),MainPresenter {
@Inject
lateinit var estateService : EstateService
}
请参考this示例,了解如何结合使用 Dagger 2 和 MVP。
你必须告诉你的组件你想把它注入到哪里。
所以,试试这个组件:
@Singleton
@Component(modules = arrayOf(AppModule::class, DaoModule::class))
interface DaoComponent {
fun inject(presenter: MainPresenterImpl)
}
您的组件应该像这样提供演示者:
@Component(modules = arrayOf(AppModule::class, DaoModule::class))
@Singleton
interface MyComponent {
mainPresenter() : MainPresenter
}
Presenter 需要的所有依赖项都通过构造函数参数注入到 Presenter 中:
class MainPresenterImpl
@Inject constructor(private val estateService : EstateService ) :
MvpBasePresenter<MainView>(),MainPresenter {
...
}
比起在 createPresenter()
中,只需像这样从 dagger 组件中获取演示者:
class MainActivity : MvpActivity<MainView, MainPresenter>(), MainView {
...
override fun createPresenter(): MainPresenter =
(application as MyApp).myComponent().mainPresenter()
}
请注意,上面显示的代码将无法编译。这只是伪代码,让您了解依赖图在 Dagger 中的样子。
我正在尝试让 dagger 在我的应用程序中工作。 创建模块组件和 MyApp 后,我可以使用匕首将数据库服务注入视图,但我无法用演示者做同样的事情。 代码:
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
class DaoModule {
@Provides
fun providesEstateService(): EstateService = EstateServiceImpl()
}
组件
@Singleton
@Component(modules = arrayOf(AppModule::class, DaoModule::class))
interface DaoComponent {
fun inject(activity: MainActivity)
}
AppModule
@Module
class AppModule(internal var mApplication: Application) {
@Provides
@Singleton
internal fun providesApplication(): Application {
return mApplication
}
}
主要活动
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()
}
以这种方式注入 estateService 后,我可以使用它,但我不知道如何将服务直接注入演示者。 我试过这样做,但没有用。 我应该只传递来自 activity 的注入对象吗?或者我应该将 MyApp 作为参数传递或创建静态方法以允许我从应用程序的任何位置获取它?
class MainPresenterImpl
@Inject
constructor(): MvpBasePresenter<MainView>(),MainPresenter {
@Inject
lateinit var estateService : EstateService
}
请参考this示例,了解如何结合使用 Dagger 2 和 MVP。
你必须告诉你的组件你想把它注入到哪里。
所以,试试这个组件:
@Singleton
@Component(modules = arrayOf(AppModule::class, DaoModule::class))
interface DaoComponent {
fun inject(presenter: MainPresenterImpl)
}
您的组件应该像这样提供演示者:
@Component(modules = arrayOf(AppModule::class, DaoModule::class))
@Singleton
interface MyComponent {
mainPresenter() : MainPresenter
}
Presenter 需要的所有依赖项都通过构造函数参数注入到 Presenter 中:
class MainPresenterImpl
@Inject constructor(private val estateService : EstateService ) :
MvpBasePresenter<MainView>(),MainPresenter {
...
}
比起在 createPresenter()
中,只需像这样从 dagger 组件中获取演示者:
class MainActivity : MvpActivity<MainView, MainPresenter>(), MainView {
...
override fun createPresenter(): MainPresenter =
(application as MyApp).myComponent().mainPresenter()
}
请注意,上面显示的代码将无法编译。这只是伪代码,让您了解依赖图在 Dagger 中的样子。