Alice
11/26/2018, 1:25 PMrobin
11/26/2018, 1:35 PM@Suppress("RemoveExplicitTypeArguments")
val dbModule = module {
// ...
single<Database>(createOnStart = true) { connectToDb(get(), get()) }
// ...
}
fun connectToDb(dbConfig: DBConfig, secrets: Secrets) =
Database.connect(
url = dbConfig.url,
driver = dbConfig.driver,
user = dbConfig.user,
password = secrets[dbConfig.`password-secret`].value
).apply { if (dbConfig.`verify-on-start`) verify() }
fun Database.verify() = // Verification logic
data class DBConfig(
val url: String,
val driver: String,
val user: String,
val `password-secret`: String,
val `verify-on-start`: Boolean
)
robin
11/26/2018, 1:36 PMarnaud.giuliani
11/26/2018, 1:39 PMAlice
11/26/2018, 2:05 PMAlice
11/26/2018, 2:06 PMrobin
11/26/2018, 2:10 PMval appModule = module {
single<Database>() { // see above }
single<Service>()
}
// use site:
val koin = startKoin(listOf(appModule))
val service = koin.koinContext.get<Service>()
You might have to replace single<Service>()
with single { Service(get()) }
if you don't want to use the experimental api that uses reflection in the background.robin
11/26/2018, 2:11 PMService
is defined like class Service(val db: Database)
)Alice
11/26/2018, 3:10 PMAlice
11/26/2018, 3:11 PMAlice
11/26/2018, 3:16 PMrobin
11/26/2018, 3:17 PM