Search code examples
kotlinkodein

How to pass multiple parameter for Kodein Injected instances?


In Kodein, I have the below binding

    bind<AppDependent>() with multiton {
        title: String -> AppDependent(title, instance(), instance())
    }

I could get it created using

private val appDependent: AppDependent by instance(arg = "My Text")

However, if I have more than one parameter for my binding, e.g.

    bind<AppDependent>() with multiton {
        title: String, something: String -> AppDependent(title + something, instance(), instance())
    }

How could I instantiate it? I see we only have one arg in the instance() function.


Solution

  • in the next version the multi argument factories will be deprecated as there are confusing for lot of people.

    We recommend to use data classes instead, like:

    data class DiceParamerters(val startNumber: Int, val sides: Int)
    
    val kodein = Kodein {
        bind<Dice>() with factory { params: DiceParameters -> RandomDice(params) }
    }