Dusan Czupek's blog

Blog related .NET & web development

Model View Presenter (MVP) Pattern

Model-View-Presenter is a user interface design pattern engineered to facilitate automated unit testing and improve the separation of concerns in presentation logic.

The Model is an interface defining the data to be displayed or otherwise acted upon in the user interface.

The View is an interface that displays data (the Model) and routes user commands to the Presenter to act upon that data.

The Presenter acts upon the Model and the View. It retrieves data from repositories, persists it, manipulates it, and determines how it will be displayed in the View.

The degree of logic permitted in the View varies among different implementations.

At one extreme, the View is entirely passive, forwarding all interaction operations to the Presenter. In this formulation, when a user triggers an event method of the View, it does nothing but invoke a method of the Presenter which has no parameters and no return value. The Presenter then retrieves data from the View through methods defined by the View interface. Finally, the Presenter then operates on the Model and updates the View with the results of the operation.

Other versions of Model-View-Presenter allow some latitude with respect to which class handles a particular interaction, event, or command. This is often more suitable for web-based architectures, where the View, which executes on a client's browser, may be the best place to handle a particular interaction or command.

From a layering point of view, the Presenter class might be considered as belonging to the application layer in a multilayered architectured object-oriented system with common layers but it can also be seen as a Presenter layer of its own between the Application layer and the User Interface layer.

 

Sample of MVP implementation in few steps:

1) create an interface (later in the topic will be note like view-interface ) with properties  which will contain values from webpage/form.

public interface IDataView {
string FirstName { get; }
string LastName { get; }
string Status { set; get; } }

2) make webpage/form to implement this interface

public partial class MyPage : System.Web.UI.Page,IDataView

 

3) implement interface members and map them to webpage/form entries (such labels, textboxes, etc.)

public string FirstName { get { return this.tbFirstName.Text; } }
public string LastName { get { return this.tbLastName.Text; } }
public string Status { set { this.lblStatus.Text = value; }
get { return this.lblStatus.Text; } }

 

4) create new class for presenter

public class DataPresenter { }

 

5) declare a variable of view-interface type (created at point 1), variable value will be initialized through constructor

public class DataPresenter
{
private IDataView _view;
}

 

6) create/extend constructor with input parameter of view-interface type

public class DataPresenter
{
private IDataView _view;
public DataPresenter(IDataView view) { _view = view; }
}

 

7) create public method(s) for manipulate with view

public class DataPresenter
{
private IDataView _view;
public DataPresenter(IDataView view) { _view = view; }
public void SaveData() {
_view.Status = string.Format("FirstName={0}; LastName={1}",
_view.FirstName, _view.LastName);
}
}

 

8) in webpage/form create an instance of the presenter, constructor requires input value of view-interface type. Because the webpage/form already implements an view-interface, input value for constructor will be this.

private DataPresenter _presenter;

protected void Page_Load(object sender, EventArgs e)
{
_presenter = new DataPresenter(this);
}

 

9) now you can calling presenter’s method(s) from the events etc. If the presenter method change view’s data you’ll get back updated data automatically based on mapping from point 3.

protected void Page_Load(object sender, EventArgs e)
{
_presenter = new DataPresenter(this);
btnSave.Click += delegate { _presenter.SaveData(); };
}

 

In my sample I called the SaveData method on  _presenter presenter.



Shout it
DotNetKicks Image
Posted: Jun 30 2009, 20:56 by dczupek | Comments (9) RSS comment feed |
  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: dotNet

Comments

DotNetShoutout said:

trackbackModel View Presenter (MVP) Pattern

Thank you for submitting this cool story - Trackback from DotNetShoutout

# August 20 2009, 12:46

DotNetKicks.com said:

trackbackModel View Presenter (MVP) Pattern

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# August 20 2009, 20:24

WebDevVote.com said:

trackbackModel View Presenter (MVP) Pattern

You are voted (great) - Trackback from WebDevVote.com

# October 28 2009, 18:28

DotNetBurner - Visual Studio said:

trackbackModel View Presenter (MVP) Pattern

DotNetBurner - burning hot .NET content

# October 28 2009, 18:28

personal loans United States said:

personal loansThank you for your help!

# October 29 2009, 04:11

proxy sites United States said:

proxy sitesKeep up the good work bro.Your article is really great and I truly enjoyed reading it.Waiting for some more great articles like this from you in the coming days.

# December 04 2009, 04:14

Model Stardom United States said:

Model StardomGreatest intro to the model view pattern I have seen, and for the modeling site we plan to implement this new web 2.0 technology.

# January 16 2010, 23:09

drug addiction treatment clinic United States said:

drug addiction treatment clinicThis is indeed a topic which has been ignored by many experts & professionals. You have done a great job by bringing this topic into light. I hope quality discussions follow this post. Great job dczupek.

# January 22 2010, 05:02

chat server United States said:

chat serverI really appreciate posts, which might be of very useful for beginners in blogging as I am. I already have a small collection of blog posts and other articles, from which I step by step learn various aspects of life. Thank you for your resource.

# January 25 2010, 07:19

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading