Arjan van Wieringen
07/11/2025, 7:51 AMfun Application.myModule() {
val myDep: Dep by dependencies // or val myDep: Dep = dependencies.resolve();
// more code
}
I don't see why we should make make the functions so aware of a DI container. Also, this isn't really dependency injection. This is just a service container then 🙂 This is also pretty prevalent in the official KotlinConf backend app which runs on Koin.
Since modules are functions, and functions have arguments, isn't it better to do:
fun Application.myModule(myDep: Dep) { ... }
// And within Application scope
myModule(myDep = dependencies.resolve())
Is there a reason to do one over the other?Vampire
07/11/2025, 8:18 AMArjan van Wieringen
07/11/2025, 8:20 AMApplication.myModule
gets a new dependency, everything still compiles when I do this:
fun Application.myModule() {
val myDep: Dep by dependencies // or val myDep: Dep = dependencies.resolve();
val anotherDep: Foo by dependencies
// more code
}
But my tests can fail, because I didn't provide the Foo
dependency in some implicit manner before setting up the tests.Bruce Hamilton
07/11/2025, 8:33 AMktor.application.modules
and ktor.application.dependencies
Bruce Hamilton
07/11/2025, 8:35 AMArjan van Wieringen
07/11/2025, 8:37 AMVampire
07/11/2025, 8:42 AMWait, how am I misunderstanding it? How does it increase testability compared to constructor /function-argument injection?Might be different with those functions, I was talking about constructor vs. property injection. If you inject in the constructor, the constructor changes with each added injectable so you need to adjust all callers of the constructor to also provide that injectable even if it isn't needed for the test. Even if you have additionally a no-arg non-private constructor because it is a proxyable CDI bean that would not help as typically with constructor-injection you then also make the properties private and final so cannot manipulate them in the test. If you have property injection, the test can simply set those properties that are actually needed for the test in question.
Bruce Hamilton
07/11/2025, 8:48 AMHow would one even interchange a module at runtime? What is the use-case there?The use-case here is for when you want to load different modules based on your configuration files, like enabling some features based on the environment, or if you want to use a base server impl that can be extended.
Arjan van Wieringen
07/11/2025, 8:49 AMArjan van Wieringen
07/11/2025, 8:51 AMBruce Hamilton
07/11/2025, 8:58 AMArjan van Wieringen
07/11/2025, 8:59 AM