KoDI is a tiny dependency injection library written in Kotlin. (It's also not the first Kotlin DI library to be called "KoDI".)
It's primarily intended as an exercise in learning Kotlin. I have no plans to promote its use, though I may use it for some of my own projects. Its goals are to be very simple, extensible, and pleasant to use.
The unit tests and source documentation will tell you most of what you need to know about using it.
interfacePluginclassMyPlugin: PluginclassYourPlugin: PluginclassPluginAssembly: Assembly {
overridefunregister(registrar:Registar) {
val group ="plugins" registrar.apply { singleton<Plugin>(tag ="my", group ="plugins") { MyPlugin() }
singleton<Plugin>(tag ="your", group ="plugins") { YourPlugin() }
}
}
}
interfaceCoolServiceclassConcreteCoolService(valtarget:String): CoolService
classCoreAssembly: Assembly {
overrideval assemblies:List<Assembly>
get() =listOf(PluginAssembly())
overridefunregister(registrar:Registrar) {
registrar.singleton<CoolService>(params(::ConcreteCoolService))
}
}
funmain() {
DI.assemble(CoreAssembly())
// Resolve all plugins, regardless of tag, in the plugins groupval plugins:List<Plugin> =DI.resolveAll<Plugin>(group ="plugins")
// Resolve a particular pluginval plugin:Plugin=DI.resolve(tag ="my", group ="plugins")
// Resolve a service with parameterval coolService:CoolService=DI.resolve("cool") }