Skip to content
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

map & set multibinding #1951

Open
wants to merge 1 commit into
base: 4.1.0
Choose a base branch
from

Conversation

luozejiaqun
Copy link

@luozejiaqun luozejiaqun commented Aug 26, 2024

Implement map & set multibinding just like dagger2.
Here is some implementation details:

Every MapMultibinding & SetMultibinding will involve 3 definitions:

  1. the multibinding itself whose qualifier is mapMultibindingQualifier or setMultibindingQualifier
  2. the element in multibinding whose qualifier is multibindingValueQualifier (use mapMultibindingQualifier/setMultibindingQualifier as prefix)
  3. the MultibindingIterateKey which used to retrieve all elements, the key's qualifier is multibindingIterateKeyQualifier (also use mapMultibindingQualifier/setMultibindingQualifier as prefix)

When getting specific element from map multibinding, the map multibinding will try to retrieve the element through multibindingValueQualifier;
When getting all elements from map or set multibinding, the multibinding will first try to get all MultibindingIterateKey and filter based on qualifier prefix, and then retrieve each element through multibindingValueQualifier.

The known problems:

  1. the multibinding qualifier can't be null, since it's qualifier will be used as prefix of element & iterate key
  2. the map multibinding doesn't support null as a key
  3. the elements order of multibiding is different with the order of element difinitions (there are ways to keep them in same order, but i don't think this is necessary)

And just like dagger2, multibinding is inherited (because MultibindingIterateKey can be retrieved from getAll), current scope's multibinding can get all elements that define in linked scope.

Here are some use case:

module {
    intoMap<String, Simple.ComponentInterface1>("keyOfComponent1") {
        Simple.Component1()
    }
    intoMap<String, Simple.ComponentInterface1>("keyOfComponent2") {
        Simple.Component2()
    }

    intoSet<Simple.ComponentInterface1> {
        Simple.Component1()
    }
    intoSet<Simple.ComponentInterface1> {
        Simple.Component2()
    }
}

val map: Map<String, Simple.ComponentInterface1> = koin.getMapMultibinding()
val set: Set<Simple.ComponentInterface1> = koin.getSetMultibinding()

issue #772

@arnaudgiuliani arnaudgiuliani changed the base branch from 4.0.0 to 4.1.0 November 15, 2024 14:17
@arnaudgiuliani arnaudgiuliani added this to the 4.1 milestone Nov 15, 2024
@arnaudgiuliani
Copy link
Member

Looks interesting, will check for 4.1 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants