Elye
Elye

Reputation: 60311

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.

Upvotes: 0

Views: 930

Answers (2)

romainbsl
romainbsl

Reputation: 544

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) }
}

Upvotes: 1

Elye
Elye

Reputation: 60311

Apparently there's something with M as wrapper to the arguments

private val appDependent: AppDependent by instance(arg = M("abc", "def"))

Found the answer from https://weekly-geekly.github.io/articles/431696/index.html. Can't find them in the Kodein documentation :(

The arguments can go up to 5, as stated in https://kodein.org/Kodein-DI/?6.3/core

Just like a factory, a multiton can take multiple (up to 5) arguments.

Upvotes: 0

Related Questions