Marc Knaup
03/13/2020, 6:59 AMinterface A
object B : A
fun main() {
val dkodein = Kodein.direct {
bind<B>() with instance(B)
}
println(dkodein.instance<A>()) // prints object B() <-- this should not resolve!
}
romainbsl
03/13/2020, 8:34 AMMarc Knaup
03/13/2020, 8:36 AMinstance<A>()
resolves to B
(there’s only one and no binding for plain A
). But it should not resolve at all because it’s not what the user expects. A
must only resolve in a different Kodein context where it is explicitly bound.romainbsl
03/13/2020, 9:21 AMMarc Knaup
03/13/2020, 9:36 AMKodein {
bind<SpecialContext>() with singleton { // SpecialContext : RootContext
SpecialContextImpl(
configuration = instance(), // <SpecialContextConfiguration>
parent = instance() // <RootContext>
)
}
}
I should have had a RootContext
in this scope which serves as the parent of my SpecialContext
.
Instead, Kodein reported a cyclic dependency.
The actual problem was that RootContext
wasn’t supposed to be access in this context - i.e. I was in the wrong Kodein instance. So instead Kodein resolved the instance to itself.
But that was absolutely not clear from the error.
Would it have said that there is no RootContext
bound and had printed the entire module in the exception, I would’ve understand the issue much faster.
I have to use a lot of sub-interfaces over multiple levels to have a good API and because I can only use a single interface as a receiver parameter in many cases. So I cannot keep interfaces completely distinct and will keep running into this issue.