https://kotlinlang.org logo
#ktor
Title
# ktor
t

Tim Malseed

04/25/2021, 4:31 AM
I've got a bit of a dumb question.. Using dagger for DI in Ktor. Where do I @Inject all my things?
Copy code
fun Application.configureRouting() {

    // Starting point for a Ktor app:
    routing {


        get("/") {
            call.respondText("Hello World!")
        }
There's no 'class' to hold an
@Inject lateinit var
Sure, I can pass my dependencies to the
configureRouting()
function, but that's called from:
Copy code
fun main() {
    embeddedServer(Netty, port = 8080, host = "0.0.0.0", watchPaths = listOf("classes")) {
        configureRouting()
    }.start(wait = true)
}
I don't have the terminology to correctly phrase the question but.. Where's the 'injection point' for a route, if I'm using Dagger?
j

Joost Klitsie

04/25/2021, 8:43 AM
Even though this might not be a satisfactory answer: Perhaps you can look at different injection frameworks. I use Kodein because it has Ktor support, it is very easy to use.
t

Tim Malseed

04/25/2021, 12:23 PM
Yeah, I could - I guess I'm more interested in understanding how I'd solve this problem though. I guess Ktor uses some sort of functional programming paradigm that I'm not used to. So I'm curious if a function has many dependencies, if there's some idiomatic way that those dependencies can be fulfilled without having to manually pass them down the call hierarchy. Obviously this is possible and this is what some of the alternative injection libraries provide. I wonder how this problem is solved in general.
c

carrot

04/25/2021, 12:29 PM
I don't actually use Dagger in this project, but we could use it if we needed to - we have a small extension function and an interface to split out "handlers", which would be fine with
@Inject
The code samples aren't complete but hopefully it helps a little https://gist.github.com/CarrotCodes/f8d88b54a4cbbc60a411835775cbd60e
t

Tim Malseed

04/25/2021, 12:38 PM
Thanks, I'll have a look
6 Views