Class-level multi-bindings
See original GitHub issueFrom what I understand, multi-bindings must currently be defined in a component. From the README:
@Component
abstract class MyComponent {
abstract val fooMap: Map<String, Foo>
@IntoMap
@Provides
protected fun provideFoo1(): Pair<String, Foo> = "1" to Foo("1")
@IntoMap
@Provides
protected fun provideFoo2(): Pair<String, Foo> = "2" to Foo("2")
}
It would be great if instead, the @IntoMap
and @IntoSet
annotations could be applied to an injectable class. Anvil allows this kind of syntax:
@ContributesMultibinding(Scope::class, boundType = Foo::class)
@StringKey("my-first-foo")
class Bar @Inject constructor() : Foo {}
@ContributesMultibinding(Scope::class, boundType = Foo::class)
@StringKey("my-second-foo")
class Baz @Inject constructor() : Foo {}
And the component would as a result contain a Map<String, Foo>
, itself containing {"my-first-foo": Bar(), "my-second-foo": Baz()}
This helps with composition, as adding an entry to the set/map only requires defining a type (or adding a dependency), no changes are needed to the component code itself.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Multibindings - Dagger
Dagger allows you to bind several objects into a collection even when the objects are bound in different modules using multibindings. Dagger assembles...
Read more >Guice vs Spring - Dependency Injection - Baeldung
Guice uses binding as the equivalent to wiring in Spring. Simply put, bindings allow us to define how dependencies are going to be...
Read more >Multibindings · google/guice Wiki - GitHub
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 8 and above, brought to you by Google. - Multibindings ...
Read more >WPF, passing variable to converter inside data template
I need to pass the value of a class level variable to a converter, from within side ... Visibility> <MultiBinding Converter="{StaticResource ...
Read more >How to add Custom tooltip for each column in Datagrid
<MultiBinding Converter="{StaticResource ValueToTooltipConverter}"> ... So I've to use class level property in Converter like
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In fact I don’t think there’s anything blocking someone making an ‘anvil’ for
kotlin-inject
right now, it should already support multiple rounds properly. You would just be generating the@Component
classes based on your annotations. If anyone want’s to take a stab at it I’d be glad to answer any questions/fix an issues related to it.Note that Hilt is implemented in much the same way as Anvil as a separate code generator, really the only difference is that it’s first-party in dagger where anvil is not. I will make no promises on future additions like this but if I where to do something first-party it would probably follow the same pattern. In the meantime, I’d fully support any project built on top of this that implemented some or all of anvil’s feature-set. Extensibility is a goal and I think it’s a good way to allow experimentation and more complex use-cases without complicating the core library.