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.

Multibinding with @ContributesBinding

See original GitHub issue

I’d like to propose a(couple) multibinding version(s) of @ContributesBinding to reduce the need for obvious, hand-written modules.

I often use multibindings in tandem with factories to enable constructor injection for types the Android framework is ultimately responsible for instantiating.

Map Multibinding

@ContributesBindingIntoMap(AppScope::class, mapKey = PlaylistViewModel::class)
class PlaylistViewModel @Inject constructor(streamer: AudioStreamer): ViewModel

// resulting in

[...]
interface Module {
    @Binds
    @IntoMap
    @Named("com.example.PlaylistViewModel")
    fun providesComExamplePlaylistViewModel(viewModel: PlaylistViewModel): ViewModel

Set Multibinding

@ContributesBindingIntoSet(AppScope::class)
class IndexMigration @Inject constructor(currentUser: User): Migration

// resulting in

interface Module {
    @Binds
    @IntoSet
    fun providesComExampleIndexMigration(migration: IndexMigration): Migration

Alternative: Modify the existing ContributesBinding annotation

@Target(CLASS)
@Retention(RUNTIME)
annotation class ContributesBinding(
  val scope: KClass<*>,
  val boundType: KClass<*> = Unit::class,
  val replaces: Array<KClass<*>> = [],
  val mapKey: KClass<*> = Unit::class,
  val intoSet: Boolean = false
)

This would minimize the Anvil API surface, but it doesn’t provide a strong API. For example, there’s nothing preventing a person from suggesting they would like map and set multibinding.

I have a rough prototype running as well, with a few issues in the map multibinding scenario.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
vRallevcommented, Mar 3, 2021

PRs are up #216 and #219.

2reactions
vRallevcommented, Feb 9, 2021

That is pretty much what we agreed on internally. At the moment we’re leaning towards option 1. The first step will be to make qualifiers work for @ContributesBinding without multibindings. There’s no reason to not support this qualifiers for that use case.

We want to do quite some testing internally first (also benchmarks). I’m currently on parental leave, but want to take a look at this in Q2.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dagger + Anvil: Learning to Love Dependency Injection on ...
Powerful: Dagger is full of functionality such as multibinding that ... Adding @ContributesBinding and a constructor with @Inject is all you ...
Read more >
Extending Anvil for Fun and Profit - Slack Engineering
AnvilInjector itself is just a simple interface that allows us to collect all of these in a multibinding in the host component. interface...
Read more >
Dagger + Anvil: Learning to Love Dependency Injection
Anvil is a Kotlin compiler plugin that makes dependency injection with Dagger 2 easier. Anvil reduces boilerplate code, improves code ...
Read more >
N26 Path to Anvil - DEV Community ‍ ‍
We created a @FragmentKey and used Dagger's multi-binding to wire ... @ContributesBinding(Singleton::class, FragmentFactory::class) class ...
Read more >
Newest 'dagger-2' Questions - Page 2 - Stack Overflow
Dagger Hilt Multibindings can not support List<T> type? ... Aable : A { override fun doSomething1() {} } The following doesnt work @ContributesBinding(....
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