Frage

I am using an MVP architecture (or at least an approximation of one) and have a hierarchy of presenters, with similarly nested views. I'll stick to the first 2 levels for simplicity:

AppPresenter/AppView at the top level. DocumentListPresenter/DocumentListView and TranslationPresenter/TranslationView at the second level.

The two second level views are added to a panel on the top level view as widgets, but at the moment this is done by injecting the two second level presenters into the top level presenter, then having the top level presenter pass their views to its view through setter methods.

This seems a bit of a roundabout way of doing things. Is it possible with Gin to inject the second level views into the top level view without the presenters being involved at all?

War es hilfreich?

Lösung

I have managed to get this working with a minor change to the guice code (overridden in my local class that extends AbstractPresenterModule:

@Override
protected <D extends Display> void bindDisplay(Class<D> display, Class<? extends D> displayImpl)
{
   bind(display).to(displayImpl).in(Singleton.class);
}

The bindPresenter() method I've been using binds the presenter as a singleton, and calls bindDisplay() to bind the display interface to its implementation, but the latter is not a singleton binding so I was ending up with different instances (one linked to the presenter, the other linked to the containing display class).

Overriding the bindDisplay() method to make the display binding a singleton ensures that the display implementation injected into another display is the same instance to which the presenter is bound.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top