KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I try to build a Webapp with the MVP paradigm. Because I want the API to be clean and make everything easy testable I try to inject everything possible via Contructor Injection. Now I came to a point where I have a view with multiple Textfields. These Textfields get filled by the presenter when there are values for it in the DB so my presenter needs a reference of the view but the vie obviously needs a reference of the Presenter, too. CDI tells me that injection of Presenter into the view and otherwise is not possible because of circular dependencies. Ist it possible to avoid setteing the presenter in the view via a setter method? The code looks something like this: View: public Class ViewImpl implements view { private PresenterImpl presenter; private User user; @Inject public ViewImpl(PresenterImpl presenter, User user) { super(); this.presenter = presenter; this.user = user; } public void attach() { super.attach(); presenter.fetchNames(); showUser(); } public void setUser(User user) { this.user = user; } } Presenter: public Class PresenterImpl implements Presenter { private ViewImpl view; private User user; @Inject public PresenterImpl(ViewImpl view, User user) { this.view = view; this.user = user; } public void fetchNames() { fetchFromDB(); view.setUser(user); } } Here I get the cyclic dependency. To avoid that I tried to use the Instance Interface and changed the presenter to this: Presenter: public Class PresenterImpl implements Presenter { private ViewImpl view; private Instance<ViewImpl> instanceView; private User user; @Inject public PresenterImpl(Instance<ViewImpl> instanceView, User user) { this.instanceView = instanceView; this.user = user; bind(); } public void bind() { this.view = instanceView.get(); } public void fetchNames(
Tags (comma-separated)
Save Edits
Cancel