DI
A Ktor application plugin for managing the registration and resolution of module dependencies.
The following properties are configurable:
provider
: the logic for registering new typesresolution
: the function for validating the provided types and creating the dependency mapreflection
: controls how objects are initialized from type references
You can declare dependencies using the dependencies DSL:
fun Application.databaseModule() {
dependencies {
provide<Database> { PostgresDatabase(resolve("connectionUrl")) }
}
}
Content copied to clipboard
Or through configuration:
ktor:
application:
dependencies:
- com.example.db.PostgresDatabase
Content copied to clipboard
Each item declared via configuration can be a reference to a class or a top-level function.
Resolving dependencies in application modules is also achieved through the DSL:
fun Application.routing() {
val database: Database by dependencies
val repository: MessagesRepository = dependencies.create()
}
Content copied to clipboard
Alternatively, they can be supplied automatically through parameters.