Hello everyone, I'd like to ask a question:
I currently start using Koin on the Backend with Ktor, and have the following situation:
Copy code
fun Application.rooms(db: DB) {
install(Koin) {
modules(roomsDI)
}
val roomFooBar: RoomFooBar by inject()
routing {
roomRoutes(roomFooBar)
}
}
val roomsDI = module {
single<RoomFooBar> { RoomService(get()) }
single<RoomRepository> { RoomDatabaseAccessor(???)}
}
class RoomDatabaseAccessor(private val db: DB) : RoomRepository {...}
I'm not sure how to pass the
db
parameter (which is a function parameter of
Application.rooms()
) into the koin module. The parameter changes depending on the context, so just declaring it inside the
module { }
would not work.
Any hints for that? I could not really find a satisfying solution in the docs.
Thanks a lot!
f
Florian Eula
12/30/2020, 2:57 PM
If Application.rooms() is only called once through the lifetime of your application, you could simply make it so that roomsDI is a function that takes the db as a parameter, and use it in the module.
`
Copy code
fun roomsDi(db: DB) = module {
single<Database> { db }
single<RoomFooBar> { RoomService(get()) }
single<RoomRepository> { RoomDatabaseAccessor(db) }
}
👍 1
p
Philipp Mayer
12/30/2020, 3:09 PM
I was so focused on solving it with a koin mechanism that I didn't think of that. Thank you very much!