Hi all, good day. I would like to hear about you e...
# ktor
f
Hi all, good day. I would like to hear about you experience with DI in ktor apps. I see that ktor has a example with Kodein , which I looked into. Also, I know some people do use Koin. I can’t find a good comparison between both. Koin API seems more ‘kotlin’ to me then kodein, but I would like to hear about the experiences… Most info on internet is more about Android then Ktor …
c
My recommendation is to do manual DI, by just calling constructors from kotlin code. DI maybe makes sense in an android app or in IDEA where you constantly create and destroy components, but for a web server app you just create all components at startup and then just use them.
f
but sometimes you wanna replace some components for testing, but don’t want expose that to your apis .. I had that while trying to replace the Clock instance for a fixed one so I could test some functionality that was time dependent. So, a ‘light’ DI is nice to have
c
maybe but theres not a lot of things that you want to replace in integration tests (maybe clock, maybe for some tests a database), and for unit tests you will not use the DI container anyway,and just create the class that you need.
so you can get quite far with a method that wires everything up and takes a clock as parameter with SystemClock as default value
f
that would work for a single class, but not for integration tests
c
what I mean is a method like
fun createServer(clock: Clock=Clock.systemUTC())
Doing manual DI does not make injection test classes any harder.