https://kotlinlang.org logo
Title
f

fbodr

06/28/2022, 12:47 PM
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

christophsturm

06/28/2022, 1:06 PM
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

fbodr

06/28/2022, 1:08 PM
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

christophsturm

06/28/2022, 1:09 PM
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

fbodr

06/28/2022, 1:55 PM
that would work for a single class, but not for integration tests
c

christophsturm

06/28/2022, 2:02 PM
what I mean is a method like
fun createServer(clock: Clock=Clock.systemUTC())
Doing manual DI does not make injection test classes any harder.