Reputation: 401
I have these three dependencies correctly created (I verified it by debugging that they indeed exist) using Dagger 2, let's call them a, b, c
, like so:
class Example {
...
@Inject A a;
@Inject B b;
@Inject C c;
...
},
provided by SomeModule
as follows:
@Module
class SomeModule {
...
@Singleton @Provides A provideA(){return new A()};
@Singleton @Provides B provideB(){return new B()};
@Singleton @Provides C provideC(){return new C()};
...
},
and the component is a fairly simple one:
@Component(modules = {SomeModule.class, ...})
class SomeComponent {
...
void inject(Example example);
...
},
and I need them to create another objects, let's call them d, e
, something like this:
public Example(){
DaggerSomeComponent.builder().build().inject(this);
...
this.d = new D(c);
this.e = new E(d, a, b);
...
}
My question is: is it possible to get to something like this?
class Example {
...
@Inject A a;
@Inject B b;
@Inject C c;
@Inject D d;
@Inject E e;
...
public Example(){
DaggerSomeComponent.builder().build().inject(this);
...
}
}
Upvotes: 0
Views: 100
Reputation: 95634
Yes, @Provides
methods can take parameters. Dagger will create instances before invoking the method.
@Provides D provideD(C c){return new D(c)};
@Provides E provideE(A a, B b, D d){return new E(d, a, b)};
Upvotes: 1