没有AssistedInjection,我有以下
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 { ... }
但是要使用 AssistedInjection,我需要
class UberFactory {
B createB(@Assisted("stringB") String p)
C createC(@Assisted("stringC") String p)
}
B
和的相应变化C
。
这有两个问题:
- 应用程序代码必须了解是调用
createB
还是createC
- 跨越我的
UberFactory
包结构以实例化IA
在我看来,更直观的 API 将是:
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));
这样我就可以简单地注释IAFactory
我注入到我的应用程序代码中的实例。
但据我所知,没有这样的 API。所以,
- 有什么我遗漏的东西使这样的 API 不合理吗?
- 鉴于当前可用的功能,有没有办法近似这种行为?