Without AssistedInjection, I have the following
interface IA { ... }
class B implements IA {
B (String p)
}
class C implements IA {
C (String p)
}
interface IAFactory {
IA create(String p)
}
class BFactory implements IAFactory { ... }
class CFactory implements IAFactory { ... }
But to use AssistedInjection, I'd need
class UberFactory {
B createB(@Assisted("stringB") String p)
C createC(@Assisted("stringC") String p)
}
and the corresponding changes in B
and C
.
There are 2 problems with this:
- Application code has to understand whether to call
createB
orcreateC
- The
UberFactory
cuts across my package structure to instantiate instances ofIA
It seems to me that a more intuitive API would be:
install(new FactoryModuleBuilder()
.implement(IA.class, B.class)
.annotatedWith(BFactoryQualifier.class)
.build(IAFactory.class));
install(new FactoryModuleBuilder()
.implement(IA.class, C.class)
.annotatedWith(CFactoryQualifier.class)
.build(IAFactory.class));
That way I can simply annotate the IAFactory
instance I inject into my application code.
But as far as I can see, there's no such API. So,
- Is there something I'm missing that makes such an API unreasonable?
- Is there a way to approximate this behavior given the currently available features?