# Metro DI for Ktor Backend: From a God Object through manual DI to Constructor Injection

DevFeed: [Metro DI for Ktor Backend: From a God Object through manual DI to Constructor Injection](<https://devfeed.tech/articles/metro-di-for-ktor-backend-from-a-god-object-through-manual-di-to-constructor-injection-22966.md>)

Original publisher: [Read original article](<https://funkymuse.github.io/posts/metro-di-ktor-server/>)

Author: FunkyMuse

Published: 2026-05-15T15:35:00Z

Content type: article

Language: en

Sources: [FunkyMuse](<https://devfeed.tech/sources/funkymuse.md>)

Topics: [Dependency injection](<https://devfeed.tech/topics/dependency-injection.md>), [Ktor](<https://devfeed.tech/topics/ktor.md>), [constructor](<https://devfeed.tech/topics/constructor.md>), [Dagger](<https://devfeed.tech/topics/dagger.md>), [dagger-hilt](<https://devfeed.tech/topics/dagger-hilt.md>), [koin](<https://devfeed.tech/topics/koin.md>), [IntelliJ IDEA](<https://devfeed.tech/topics/intellij-idea.md>)

Tags: [accessors](<https://devfeed.tech/tags/accessors.md>), [aggregation](<https://devfeed.tech/tags/aggregation.md>), [android](<https://devfeed.tech/tags/android.md>), [android-development](<https://devfeed.tech/tags/android-development.md>), [annotations](<https://devfeed.tech/tags/annotations.md>), [anvil](<https://devfeed.tech/tags/anvil.md>), [api](<https://devfeed.tech/tags/api.md>), [auth](<https://devfeed.tech/tags/auth.md>), [authentication](<https://devfeed.tech/tags/authentication.md>), [backend](<https://devfeed.tech/tags/backend.md>), [conf](<https://devfeed.tech/tags/conf.md>), [constructor](<https://devfeed.tech/tags/constructor.md>), [default-parameters](<https://devfeed.tech/tags/default-parameters.md>), [di](<https://devfeed.tech/tags/di.md>), [framework](<https://devfeed.tech/tags/framework.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [ktor](<https://devfeed.tech/tags/ktor.md>), [lazy](<https://devfeed.tech/tags/lazy.md>), [metro](<https://devfeed.tech/tags/metro.md>), [object](<https://devfeed.tech/tags/object.md>), [var](<https://devfeed.tech/tags/var.md>)

## AI overview

The author replaces a manually wired `BackendComponent`--whose default constructor parameters could silently pull real dependencies into tests--with Metro's compile-time constructor injection. A `BackendGraph` gathers `@ContributesTo` bindings and `@ContributesIntoSet` route and plugin registrars, so adding or removing them does not require editing a central registry; plugin installers retain an explicit order. Stateless injected classes get fresh instances, while `@SingleIn(AppScope::class)` is reserved for shared or costly objects. For integration tests, TestContainers provides PostgreSQL/PostGIS, Redis, and MinIO; tests start a real Ktor server and obtain its live Metro graph to inspect repository state. The author contrasts this with Metro's direct-construction-with-fakes approach for unit and component tests, which they do not use for this backend's integration coverage. They note that Metro's compile-time verification costs extra build time and say they miss Dagger's IDE dependency navigation. The supplied text describes a single Gradle backend module and a possible future split, not an implemented multi-module setup.

## Source excerpt

The Starting Point: a God Object When Rudio was a created a few months ago it used the simplest possible DI: one big object BackendComponent with everything wired manually via by lazy and lateinit var. No framework, no annotations, no ceremony, then i started adding things and manual DI became a tedious and tiring task because everywhere i used default parameters to achieve default constructor...