MVP pattern - design question
We are trying to use the MVP pattern in our current project (asp.net app) and have run into some issues. The page has multiple sections and we are using user controls for these independant sections. Now each user control has its own view and presenter and the page acts as the base view. Now the question is how should the data that is needed by 开发者_如何学Cuser control be passed to it ?
As per current design, the page presenter will get the needed data for the entire page. How can this data be passed to the user control presenter ?
Other approach we are thinking is to instead create only views for user controls and use page presenter to handle all events. In this case, we will hv multiple view interfaces to be implemented by each user control view. But how would the page presenter interact with all the different views ?
Thanks, jbn
Why do the user controls have their own views and presenters?
I suggest having a view and presenter for the page and interacting with the user controls via the page view. If you need to handle events or pass data to and from the user controls you can expose them to the page and the page view can wrap it for the presenter. The wrapping prevents the presenter from communicating with the user control directly.
Here is some Pseudocode:
IFooPageView
{
string SomeData {get; set;}
event EventHandler SomeEvent;
}
public class FooPageView : IFooPageView
{
public event EventHandler SomeEvent;
public SomeData
{
get { return myUserControl.SomeData;}
set { myUserControl.SomeData = value;}
}
protected override void OnInitComplete(EventArgs e)
{
//handle the user control event
this.myUserControl.SomeEvent += SomeEvent_EventHandler;
}
private void SomeEvent_EventHandler(object sender, EventArgs e)
{
//Raise the user control event to the presenter
if (SomeEvent!= null)
SomeEvent(this, EventArgs.Empty);
}
}
I would read Phil Haack's ASP.NET Supervising Controller (Model View Presenter) From Schematic To Unit Tests to Code
There is a lot of good MVP information for web forms in Phil's article.
one of the solution which i can think of
1- Maintain a List of composed presenter at the top level presenter and when you got the data at top level presenter , you can distribute the data by picking the right presenter from the list.
Not sure if it exactly fits the MVP but you can expose a property(View) from your UCs and update that with the data in your page.
精彩评论