All, a question regarding MVP:
I have a page, working against a view. Let's call it IMyView.
I have a presenter, that populates the view. Let's call it MyViewPresenter.
I have another presenter, working in conjunction with another view. It provides a method called 'LoadComments()'. Let's call this presenter MyOtherViewPresenter.
From my page which is working against IMyView, I want to make a call to the LoadComments() method from MyOtherViewPresenter, but in terms of MVP 'legalities' I wonder if I should be permitted to do so.
My question is, how do I make use of methods provided by other presenters that work with other views, from pages using views that are seemingly unrelated? Should I be concentrating on providing the same presenter methods within the presenter my page should be working with, or is it okay to be using more than one presenter per view, e.g.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
MyViewPresenter presenter =开发者_如何转开发 new MyViewPresenter(this);
presenter.LoadProduct();
MyOtherViewPresenter collab = new MyOtherViewPresenter();
string comments = collab.LoadComments();
}
}
Thanks in advance for any thoughts and responses.
In MVP you should NOT be using methods from another presenter. The view should only now about the existence of it's own presenter. And even that knowledge should be as little as possible. The presenter controls and adjusts the view. Not otherwise. (Passive view variant of MVP)
If another presenter has some code you want to reuse than abstract it to a helper method in a helper class. If there is a common UI element (multiple controls) then put it in a usercontrol so you can reuse that as well.
If you are using the passive view then the presenter pushes the information to the view. In your code:
string comments = collab.LoadComments();
this is the view which is pulling information. It should not be doing that. But that is a totally different discussion :-)
精彩评论