question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Class-level multi-bindings

See original GitHub issue

From 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:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
evantcommented, Jul 29, 2022

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.

0reactions
evantcommented, Sep 23, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found