Multibinding with @ContributesBinding
See original GitHub issueI’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:
- Created 3 years ago
- Reactions:7
- Comments:9 (1 by maintainers)
Top 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 >
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

PRs are up #216 and #219.
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
@ContributesBindingwithout 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.