1

我正在使用 Guice Assisted Inject库为我建立一个工厂。我目前的设置是这样的:

class MyObject {
  @Inject public MyObject(@Assisted FirstDep first, @Assisted SecondDep second, ThirdDep third) { /**/ }
}

class FirstDep { /* nothing to see here */ }
class SecondDep {
  @Inject public SecondDep(@Assisted FirstDep first) { /**/ }
}
class ThirdDep { /* nothing to see here either */ }

class MyModule extends AbstractModule {
  @Override public void configure() {
    bind(ThirdDep.class);
    install(new FactoryModuleBuilder().build(MyObjectFactory.class));
  }
}

interface MyObjectFactory {
  SecondDep createSecond(@Assisted FirstDep first);
  MyObject createMyObject(@Assisted FirstDep first, @Assisted SecondDep second);
}

这迫使我明确地创建一个SecondDepusing factory.createController(first, factory.createSecond(first))。是否可以更改我的绑定,以便我可以简单地做factory.createController(first),它会自动使用SecondDep绑定和我传入的参数?

4

1 回答 1

2

考虑您在此处创建的 API,尤其是关于未来的扩展或实现。显然,您周围有许多有状态的 FirstDep 实例,因为 SecondDep 依赖于特定的 FirstDep,但未来的 SecondDep 可能不依赖于同一个 FirstDep(或任何 FirstDep)。createMyObject(first)可以作为速记的事实createMyObject(first, factory.createSecond(first))是特定于您的业务案例的,我认为 Guice 中没有速记可以做出这样的假设。

也就是说,你有两个选择。一、你可以创建一个非常小的助手:

// Encapsulate the createMyObject(first) shorthand.
class MyObjectHelper {
  @Inject MyObjectFactory rawFactory;

  MyObject createMyObject(FirstDep first) {
    return rawFactory.createMyObject(first, rawFactory.createSecond(first));
  }
}

或者,两个,您可以使用@AssistedInjectGuice 有效地重载构造函数:

class MyObject {
  // Multiple @AssistedInject constructors, selected based on which parameters
  // are marked with @Assisted.

  @AssistedInject public MyObject(@Assisted FirstDep first,
       SecondFactory secondFactory, ThirdDep third) {
    this(first, secondFactory.createSecond(first), third);
  }

  @AssistedInject public MyObject(@Assisted FirstDep first,
      @Assisted SecondDep second, ThirdDep third) { /**/ }
}

interface SecondFactory {
  // Note that @Assisted annotations are not needed here. Every parameter in
  // these interfaces is for assisted injection, by definition.
  SecondDep createSecond(FirstDep first);
}

interface MyObjectFactory {
  MyObject createMyObject(FirstDep first);
  MyObject createMyObject(FirstDep first, SecondDep second);
}

尽管为每个类创建一个单独的工厂稍微有点冗长,但我认为您会发现它有助于使您的类/工厂保持独立且易于遵循,并且它巧妙地避免了我可以的潜在循环引用'如果 Guice 支持,请不要立即记住。我倾向于将我的工厂暴露为嵌套接口:

class SecondDep {
  interface Factory {
    SecondDep create(FirstDep first);
  }

  @Inject public SecondDep(@Assisted FirstDep first) { /**/ }
}

...然后,您可以找到并更新Second.Factory与其支持的类完全相邻的内容。

于 2014-03-03T19:16:04.277 回答