本文介绍了如何使用Kodein的直接检索来获取作为工厂绑定的依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下注射器:

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实例,并在实例化 TastyMeal后传入List<Herb>List<Bean> ?我在文档中找不到示例.

How can I use Kodein's direct injection feature to fetch a SpicyBeans instance using a factory, passing in List<Herb> and List<Bean> after TastyMeal is instantiated? I can't find an example in the documentation.

推荐答案

该解决方案称为多参数工厂.关于此的文档非常稀缺(这是一个问题,您可以打开一个票证,以便提醒我改进文档吗?).

The solution is called multi-argument factories.The documentation about this is very scarce (This is a problem, can you open a ticket so I can be reminded to improve the doc?).

同时,这是您的解决方案:

In the meantime, here is your solution:

val tastyDish: SpicyBeans by kodein.instance(arg = M(beans, herbs))

这篇关于如何使用Kodein的直接检索来获取作为工厂绑定的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 04:09