'Dagger2: Mark external type as singleton
I am depending on an external type (that I don't control) with several constructor args. The constructor is marked with @Inject, but I want to have dagger treat the type as @Singleton.
Is there a way to do this without copying all the constructor args into a @Provides-annotated function?
@Module
interface MyModule {
/** Is this the only way? */
@Provides @Singleton
static ExternalType makeSingleton(
ExternalDepA a, ExternalDepB b, ExternalDepC c,
ExternalDepD d, ExternalDepE e, ExternalDepF f) {
return new ExternalType(a, b, c, d, e, f);
}
}
Update: The following doesn't work either; first, @Binds is for interface-to-implementation binding (as @YavorMitev also points out in the comments); second it produces a circular dependency error.
@Module
interface ThisDoesNotWork {
/** BAD */
@Binds @Singleton ExternalType makeSingleton(ExternalType external);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
