我有一个抽象类:
abstract class MyFirstViewModel(
private val firstParamArgs: FirstParamArgs,
private val secondParam: SecondParam):ViewModel(),ActionHandler{
override fun handleAction(result: ActionResult){
// implementation of this function
}
interface ActionHandler {
fun handleAction(result: ActionResult)
}
我也有这些课程:
class MySecondViewModel(
private val firstParamArgs: FirstParamArgs,
private val secondParam: SecondParam):MyFirstViewModel(firstParamArgs, secondParam){}
class MyThirdViewModel(
private val firstParamArgs: FirstParamArgs,
private val secondParam: SecondParam):MyFirstViewModel(firstParamArgs, secondParam){}
以及这些类的 koin 模块:
viewModel<MyFirstViewModel> { (args: FirstParamArgs) ->
MySecondViewModel(
get())}
viewModel<MyFirstViewModel> { (args: FirstParamArgs) ->
MyThirdViewModel(
get())}
我有这个视图模块:
class MyInputViewModel(
inputParamArgs:InputParamArgs,
handleRequest:HandleRequest):ViewModel(){
private val handler: ActionHandler by inject()
private fun actionHandling(){
val result = handler::handleAction }
}
以及此 viewModule 的 koin 模块:
viewModel { (args: InputParamArgs) ->
MyInputViewModel(
args,
get()
)
}
我的问题是如何为 ActionHandler 接口创建 koin 模块?
我是这样做的:
factory<ActionHandler> { (firstParamArgs: FirstParamArgs, secondParam: SecondParam) ->
MySecondViewModel(firstParamArgs, secondParam)
}
private val handler: ActionHandler by inject { parametersOf(inputParamArgs.firstParamArgs, inputParamArgs.secondParam) }