kotlin - In Kodein dependency injection, how can you inject instances of Kodein itself into instances? -


in kodein, have modules imported parent module, , classes need instance of kodein can injection later. problem code:

val parentmodule = kodein {     import(someservice.module) } 

where someservice.module needs kodein instance later, kodein isn't yet created. passing later module seems bad idea.

in kodein 3.x see there kodein-conf module has global instance, want avoid global.

how other modules or classes kodein instance?

note: this question intentionally written , answered author (self-answered questions), idiomatic answers commonly asked kotlin/kodein topics present in so.

in kodein 3.x (and maybe older versions) have access property within initialization of module called kodein can use in bindings.

within module, binding like:

bind<someservice>() singleton { someservice(kodein) } 

for complete example , using separation of interfaces vs. implementation, might this:

interface someservice {    // ... }  class defaultsomeservice(val kodein: kodein): someservice {     companion object {         val module = kodein.module {             bind<someservice>() singleton { defaultsomeservice(kodein) }         }     }      val mapper: objectmapper = kodein.instance()     // ... } 

you can import module parent noted , receive own reference current kodein instance.

val kodein = kodein {     import(defaultsomeservice.module) } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -