-
-
Notifications
You must be signed in to change notification settings - Fork 694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get/Inject all beans of T as Set<T> or List<T> #146
Comments
Ok, we'll see what we can propose for that 👍 Keep in touch. |
I've implemented a proposal and added a pull request. #148 |
Ok, we'll check that 👍 |
I would really love this. Could you use it in Android like this? |
I see. Not sure to include it immediately for 1.0.0, perhaps one minor version behind. |
I think It should be implemented with explicit binders instances which we expect to collect into set/list/map. I may declare some instance which shouldn't be part of set of all instances, My current multi-binding approach:
additional binding:
It would be preferred to implement separated definition methods for multi-binding:
or it may be placed inside current definition methods:
|
Is there any roadmap for this? I'd love to use that feature :) |
Just wanted to give another +1 for this feature. Here's my use case: One of the exposed methods to the business is the capability to run index operations (compactations, delete, recreate), and currently, every time a new index is added, we need to add a new entry to the list of index. Have we had something like |
wait and see what I can propose in 2.0 |
@arnaudgiuliani : Any news on this? |
not for 2.0 at least. Have to check that for next version. You can provide snippets to help around that for now. |
@arnaudgiuliani , @amirabiri : The following does the job for me. As-is it only works for inline fun <reified T : Any> KoinComponent.getAllOfType(): Collection<T> =
getKoin().let { koin ->
koin.beanRegistry
.getAllDefinitions()
.filter { it.isKind(Single) }
.filter { it.primaryType.isSubclassOf(T::class)}
.map { koin.get<T>(clazz = it.primaryType, qualifier = null, parameters = null) }
} |
Why was this closed? This feature is essential in a mature DI framework. Spring has had this feature for years. |
update on @REPLicated's comment inline fun <reified T : Any> KoinComponent.getAllOfType(): Collection<T> =
getKoin().let { koin ->
koin.rootScope.beanRegistry
.getAllDefinitions()
.filter { it.kind == Kind.Single }
.filter { it.primaryType.isSubclassOf(T::class)}
.map { koin.get<T>(clazz = it.primaryType, qualifier = null, parameters = null) }
} Just spotted
|
@blundell : Exactly. Now you can do something like: single { FooBarJob(get()) } bind Job::class getKoin().getAll<Job>() |
@REPLicated thanks, this works for me, but it still requires some boilerplate ( |
@RomanMinenok I'm using a less boilerplate verions, something like: inline fun <reified T: Job> Module.job(
crossinline job: Scope.() -> T,
): BeanDefinition<T> =
factory<T> { job() } bind Job::class |
Update on @REPLicated's comment inline fun <reified T : Any> getAll(): Collection<T> =
getKoin().let { koin ->
koin.instanceRegistry.instances.values.map { it.beanDefinition }
.filter { it.kind == Kind.Singleton }
.filter { it.primaryType.isSubclassOf(T::class) }
.map { koin.get(clazz = it.primaryType, qualifier = null, parameters = null) }
} |
For certain use cases I'd like to register multiple beans of
SomeInterface
and then get them from koin as aSet<SomeInterface>
orList<SomeInterface>
.For example:
The text was updated successfully, but these errors were encountered: