As not to revive a dead thread, what's a use case ...
# kodein
i
As not to revive a dead thread, what's a use case for
allProviders
as mentioned here: https://kotlinlang.slack.com/archives/C0BLU9K96/p1615530173009900?thread_ts=1615493023.009800&cid=C0BLU9K96
b
Here's a use-case of extended version of this https://github.com/Kodein-Framework/Kodein-DI/issues/361
j
I use it for KTOR to register my components
and initialize my db's
Copy code
for (bind in di().container.tree.bindings) {
                        val bindClass = bind.key.type.jvmType as? Class<*>?
                        if (bindClass != null && Component::class.java.isAssignableFrom(bindClass)) {
                            val res by di().Instance(bind.key.type, bind.key.tag)
                            println("Registering '$res' routes...")
                            with(res as Component) {
                                installRouting(this@routeWithRules)
                            }
                        }
                    }
If I launch my application in Microservice mode, I actually check if there is any DB repository in my DI. If not, I do not initialize a connection to the database
Copy code
di().container.tree.bindings.mapNotNull { bind ->
        val bindClass = bind.key.type.jvmType as? Class<*>?
        if (bindClass != null && DBRepository::class.java.isAssignableFrom(bindClass)) {
            val res by di().Instance(bind.key.type)
            res as DBRepository<*, *>
        }
        else {
            null
        }
    }.onNotEmpty { dbRepositories ->
        Database.connect(hikari())
        transaction {
            dbRepositories.forEach { dbRepository ->
                dbRepository.initialize()
            }
        }
    }
I will btw refactor this to use the allProviders 😄
b
@Joost Klitsie, allProviders will not give you tags, though and will also filter out all tagged bindings
So your way currently is the only way to achieve this (hopefully my issue above will get accepted soon to streamline this with allProviderBindings API)
j
ohh that is good to know!
thanks for the heads up
i
Interesting. For those scenarios I use the multi-binding solution which I think is effectively the same outcome. https://docs.kodein.org/kodein-di/7.4/core/multi-binding.html
I guess not for the initialization solution described though, just for "static" instances
thank you for clarifying everyone 👍