質問

My GWT applications follow MVP pattern with singleton EventBus, and Display+Presenter bound together in Activity (with help of GIN):

public class MyActivity implements Activity {
  @Inject MyDisplay display; //usually bound in Singleton scope
  @Inject MyPresenter presenter;

  ...
  @Override
  public void start(AcceptsOneWidget panel, EventBus eventBus) {
    diplay.bindPresenter(presenter);
    presenter.start(eventBus);
    panel.setWidget(display);
  }
  ...
}

Usually I inject new instances of specific RequestFactory into specific Presenter with GIN. My presenters have start() method, where I can pass EventBus instance created on behalf of the Activity (ResettableEventBus).

public MyPresenter {

  @Inject MyRequestFactory requestFactory;

  public void start(EventBus eventBus) {
    requestFactory.initialize(eventBus);
    ...
  }
  ...
}

I assume it is always safe, to let GIN create and inject new instance of MyRequestFactory. But if I want to unit test MyPresenter using RequestFactorySource, I would rather need to initialize the RequestFactory using:

requestFactory.initialize(eventBus, new InProcessRequestTransport(processor));

And here comes the question. Can I bind MyRequestFactory in scope Singleton (does the generated instance keep any state?), and initialize it globally with my singleton EventBus (does it depend on Activity's EventBus?) - what are the consequences? I would assume that MyRequestFactory instance injected to MyPresenter is already initialized either for production, or for testing. Another solution would be to inject some additional RequestFactory initialization strategy into MyPresenter, but is it necessary?

役に立ちましたか?

解決

RequestFactory is designed to be used as a singleton.

Assuming you also have an application-wide event bus, it'll work just the same as you use it today: RequestFactory only posts to the event bus, it doesn't listen to events.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top