Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi injection in dagger

Is it possible to get a list of implementations of an interface/class in dagger?

I am looking at something like Ninject's Multi-Injection.

like image 212
Avinash R Avatar asked Mar 19 '23 10:03

Avinash R


1 Answers

UPDATED for Dagger2

Already documented.

TL;DR:

In your provider

@Provides @IntoSet Foo provideAFoo() { return AFoo(); } 

... other module or same module ...

@Provides @IntoSet Foo provideBFoo() { return BFoo(); } 

... and somewhere else...

class Bar { 
    @Inject Set<Foo> allMyFoos; 
} 

Starting with Dagger2, the dependencies can be mapped (i.e., java.util.Map).

Original Answer (applicable for Dagger1)

Looks like the documentation is not complete, but Dagger already provides this.

for example (extracted from dagger's google group), provide the implementations using Provides.Type.SET

@Provides(type=SET) Foo provideAFoo() { return AFoo(); } 

... other module or same module ...

@Provides(type=SET) Foo provideBFoo() { return BFoo(); } 

... and somewhere else...

class Bar { 
    @Inject Set<Foo> allMyFoos; 
} 

REF: post in dagger's google group

like image 133
Avinash R Avatar answered Mar 27 '23 09:03

Avinash R