Skip to content

Repository files navigation

KoDI

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.

Examples

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

About

Kotlin Dependency Injection

Resources

Code of conduct

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages