Hello, I am trying to migrate from koin to metro f...
# ktor
p
Hello, I am trying to migrate from koin to metro for di and have trouble cleaning some architecture Right now, I have been using something like this to inject
UserResource
with koin
Copy code
fun Route.userResource() {
    val userResource: UserResource by inject()
    val logger = noCoLoggerUserResource()

    authenticate(SpuroAuthenticator.AUTH_JWT, SpuroAuthenticator.AUTH_API_KEY) {
        getUserRoute.Ping { path ->
            ///...
        }
    }
}

///
routing {
    userResource()
}
Is there any better way to pass
UserResoure
except than
userResource(userResource, ...other dependency)
codeberg.org/spuro/…/UserResource.kt
s
Hey @pushpull, Koin leans more towards service locator pattern which it leverages through Ktor's attributes so it allows Koin' to set
Koin
class into Ktor's
attributes
and then search for the dependency through look-up. Afaik Metro doesn't offer this lookup mechanism but you can still inject Graphs into your routes if you want doing something like this:
Copy code
interface AppGraph

val AppGraphKey = AttributeKeyAppGraph("AppGraph")

fun Application.install(graph: AppGraph) {
    attributes[AppGraphKey] = graph
}

fun Route.graph() = lazy { attributes[AppGraphKey] }

// Get a graph extension or specific dependency
fun A Route.graph(block(): AppGraph.() -> A) = lazy { block(attributes[AppGraphKey]) }
I personally prefer explicit injection rather than lookup so if you're using Metro for the convenience of auto-wiring your graph I would suggest exposing specific "modules" for each route". In your example:
Copy code
@DependencyGraph
interface AppGraph {
   val users: UserModule
   // ...
}

@GraphExtension
interface UserModule {
   val userResource: UserResource
   val logger: Logger
   val somethingElse: SomethingElse
}
And then pass
userResource(users: UserModule)
, or do
val module by graph { it.userModule }
or something like this. You can elaborate on this when you need
Application
in your
AppGraph
using a
.Factory
.
Copy code
@DependencyGraph
interface AppGraph {
  val app: Application

  @DependencyGraph.Factory
  fun interface Factory {
    fun create(@Provides app: Application): AppGraph
  }
}

val AppGraphKey = AttributeKeyAppGraph("AppGraph")

fun Application.install(graph: (Application) -> AppGraph) {
    attributes[AppGraphKey] = graph
}
If you're using route scoped dependencies you can so something similar.
Copy code
@GraphExtension
interface RouteGraph {
  val ctx: RoutingContext

  @DependencyGraph.Factory
  fun interface Factory {
    fun create(@Provides ctx: RoutingContext): RouteGraph
  }
}

val RouteGraphKey = AttributeKeyRouteGraph("RouteGraph")

val RoutingContext.routeGraph: RouteGraph =
    call.attributes.computeIfAbsent(RouteGraphKey) { application.graph().routeGraphFactory(this) } }
That being said I personally prefer manually passing the instances even when using auto-wiring with Metro since Ktor's flexible DSL works really well with the explicit style but it's useful to group your dependencies in sub graphs or modules.
p
Hello @simon.vergauwen, thank you for looking into this. I ended up merging ktor route extension with resource class making it a bit easier to test
Copy code
routing {
    appGraph.setupResource.buildRoute()
    appGraph.userResource.buildRoute()
}

//------------------------

class SetupResource(
    private val appConfig: AppConfig,
    private val usersManager: UsersManager
) {
    context(route: Route)
    override fun buildRoute() {
        with(route) {
            getSetupRoute.Ping {
                val response = this@SetupResource.ping()
                call.respond(apiResponse = response)
            }
        }
    }
}
s
Seems a common pattern with Metro to do it like this, I would just suggest here to just use extension functions instead of context parameters that way you'll be able to drop
with(route) { }
and any function using
context(route: Route)
within the function will still work fine.
🙌 1