我如何使用 Kodein 的直接检索来获取作为工厂的依赖绑定?
How can I use Kodein's direct retrieval to fetch a dependency bound as a factory?
考虑以下注入器:
class Injector constructor(secretSauce: SecretSauce) {
val kodein = Kodein {
bind<SpicyBeans>() with factory { beans: List<Bean>, herbs: List<Herb> ->
SpicyBeans(secretSauce, beans, herbs)
}
}
}
以及以下业务逻辑:
class TastyMeal {
private lateinit var injector : Kodein
private lateinit var spicyBeans : SpicyBeans
fun initialiseWithInjector(kodein : Kodein) {
injector = kodein
val herbs = listOf(Coriander(), Cumin())
val beans = listOf(Pinto(), BlackEyed())
// fetch SpicyBeans via given Kodein Factory, given herbs and beans here
}
}
如何使用 Kodein 的直接注入功能通过工厂获取 SpicyBeans
实例,传入 List<Herb>
和 List<Bean>
after TastyMeal
是否实例化?我在文档中找不到示例。
尝试这样的事情:
class Injector constructor(secretSauce: SecretSauce) {
val kodein = Kodein {
bind<SecretSauce> with instance(secretSauce)
bind<SpicyBeans>() with factory { beans: List<Bean>, herbs: List<Herb>
->
SpicyBeans(instance(), beans, herbs)
}
}
}
然后:
val spicyBeans by kodein.newInstance { SpicyBeans(instance(), beans, herbs) }
解决方案称为 multi-argument 工厂。
这方面的文档非常稀少(这是个问题,能不能开个工单提醒我完善文档?)。
同时,这是您的解决方案:
val tastyDish: SpicyBeans by kodein.instance(arg = M(beans, herbs))
考虑以下注入器:
class Injector constructor(secretSauce: SecretSauce) {
val kodein = Kodein {
bind<SpicyBeans>() with factory { beans: List<Bean>, herbs: List<Herb> ->
SpicyBeans(secretSauce, beans, herbs)
}
}
}
以及以下业务逻辑:
class TastyMeal {
private lateinit var injector : Kodein
private lateinit var spicyBeans : SpicyBeans
fun initialiseWithInjector(kodein : Kodein) {
injector = kodein
val herbs = listOf(Coriander(), Cumin())
val beans = listOf(Pinto(), BlackEyed())
// fetch SpicyBeans via given Kodein Factory, given herbs and beans here
}
}
如何使用 Kodein 的直接注入功能通过工厂获取 SpicyBeans
实例,传入 List<Herb>
和 List<Bean>
after TastyMeal
是否实例化?我在文档中找不到示例。
尝试这样的事情:
class Injector constructor(secretSauce: SecretSauce) {
val kodein = Kodein {
bind<SecretSauce> with instance(secretSauce)
bind<SpicyBeans>() with factory { beans: List<Bean>, herbs: List<Herb>
->
SpicyBeans(instance(), beans, herbs)
}
}
}
然后:
val spicyBeans by kodein.newInstance { SpicyBeans(instance(), beans, herbs) }
解决方案称为 multi-argument 工厂。 这方面的文档非常稀少(这是个问题,能不能开个工单提醒我完善文档?)。
同时,这是您的解决方案:
val tastyDish: SpicyBeans by kodein.instance(arg = M(beans, herbs))