1

没有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

这有两个问题:

  1. 应用程序代码必须了解是调用createB还是createC
  2. 跨越我的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。所以,

  1. 有什么我遗漏的东西使这样的 API 不合理吗?
  2. 鉴于当前可用的功能,有没有办法近似这种行为?
4

0 回答 0