0

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.

Elye
  • 53,639
  • 54
  • 212
  • 474

2 Answers2

1

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) }
}
romainbsl
  • 534
  • 3
  • 10
  • As of passing through data class, that is assume the parameter are serializable data only, I assume? If for something that is not serializable, then we have to get back to `M` I guess. – Elye Sep 25 '19 at 13:48
  • Nope, no need to be serializable. The `M` will be deprecated in 6.5 and removed in 7.0, you should use `data classes` instead – romainbsl Sep 27 '19 at 07:55
  • It seems clear to me, not sure who is confused and why. However, `data` classes have one benefit over varargs, we can set default values for parameters. The docs still say `multiton` can take up to 5 params, which doesn't compile with v7. OSS and docs, don't get me started. – Abhijit Sarkar Aug 08 '20 at 00:59
  • This is definitely a documentation issue. `data class` are the recommended way when you have more than one parameter. The benefit of using data classes is that we can put an unlimited amount of parameters. otherwise, we would have to write N implementation for N parameter for the binding area. – romainbsl Aug 08 '20 at 05:33
0

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.

Elye
  • 53,639
  • 54
  • 212
  • 474