My MVP Wireframe/Walkthrough

Thursday, March 13th, 2008

My friend Iftekhar was asking for a simple wireframe/walkthrough of MVP pattern. So here it is for him (and anybody else if they need ;) ).

First I created a website. It has one page ‘Default.aspx’ with two labels, one textbox, one dropdown and one button. Then I created 2 class library projects for presenter and Viewfactory (interfaces for each webpage). I created one IDefault interfce and implemented it in the code behind of ‘Default.aspx’.

IDefault interface:

public interface IDefault
{

string username { get;set;}

string role { get;set;}

void DisablePanel();

void ShowMessage(string msg);

void ShowHistory(string msg);

}

Now I also created a presenter class for the default page. It has a private IDefault property. Now the codebehind will instantiate the presenter by passing itself to it. The presenter will initialize its private IDefault with the view instance.

The Default.aspx.cs file:

public partial class _Default : System.Web.UI.Page, IDefault
{

DefaultPresenter _presenter;

protected void Page_Init(object sender, EventArgs e)
{

_presenter = new DefaultPresenter(this);

}

protected void Page_Load(object sender, EventArgs e)
{

username = TextBox1.Text.ToString();

role = drpList1.SelectedValue.ToString();

_presenter.OnViewLoaded();

}

On the other hand the presenter class is as follows –

public class DefaultPresenter
{

Private IDefault _default;

public DefaultPresenter(IDefault def)
{

_default = def;

}………

Now every business logic Default.aspx.cs needs to perform to update the GUI, the presenter will have it done with the interface. This is the basic beauty in it. The view became pluggable and more testable. It’s like instead of going and look for the business logic classes and call it to view, view itself goes to the logic and let that do its work in its own place. Once presenter is done with all its hocus pocus it just calls methods of the interface ( actually the view) to update the UI components accordingly.

Now to test the Model, I am keeping track of the value enter in textbox and selected value of the drop down. As its not directly connected to manipulating the UI components, I put it in the model. Presenter just ask the model to log the event and if it wants to show it to view, model returns something. Here also I have a IHistory for more testability and cleanliness.

The History and IHistory:

public interface IHistory
{
string Createhistory(string username, string role);
}

public class History:IHistory
{

#region IHistory Members

public string Createhistory(string username, string role)
{

if(String.IsNullOrEmpty(username))

return “No open session”;

else

return String.Format(“{0} logged in at {1} as {2}”, username, DateTime.Now, role);

}

#endregion
}


In a nutshell –

  • First design your view interfaces,
  • create web pages implementing them ( or you can design the aspx/ascx with the designer first then write interfaces. I donno what the wise and learned people will say, I just didn’t get any difference, though most of the time it depends on what are you doing).
  • Then create presenter classes who will work with the interface to service the view. Write methods to calculate everything for the desired behavior of view in the presenter and at the end just call the interface methods so that view gets updated.
  • And if you need some more things you want to code about, do it in model.

Viola, just 3/4 projects and you are done.

The full solution file is here.

I am a humble learner. So if I made some mistakes or you would like some other angles to look at it, do say.