سؤال

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.

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top