Monday, December 2, 2013

MVP(Model-View-Presenter) in One Look

MVP(Model-View-Presenter)

Friends before explaining MVP I want you to remember How does MVP flows in Easiest way?

View ------- Presenter ------- Model


1) View have IView for Interface and ASPX.CS pages for implantation of Interfaces in IView and pass it to Presenter

2) Presenter takes the view and takes the result of Logic written in Model and assign to View

3) Logic written here which can have IModel and Model,Model implements the interfaces declared in IModel

In More explained way to understand Let us say there are 3 people A,B,C.A wants coffee and he has only coffee cup he tell B  by giving coffee cup to give him a coffee.B takes it but unfortunately he himself doesn't has the coffee.But C has different drinks like coffee,milk,tea etc.So B takes the appropriate drink i.e coffee from C and fill in the coffee cup (which is taken by A)gives to A.

Here A is View,B is Presenter and C is Model.

Now Let us go in detail,

Model View Presenter

Model View Presenter is a software approach pattern conceived as derivative of Model View Controller.

What is Model?

Model is a domain level or business level object. A model accomodates application data and provides behaviors to systematically access it. It should be independent, it should not have any association with user interface issues. So we can reuse the same model for different type of UI level applications.

What is View?

A View is a windowpane for showing the model data or elements to the application users. Model does not have direct link to the views. View know about their Models, but not the other way around.

What is Presenter?

Presenter will address the user input and use this to manipulate the model data. View will pass the user input actions to the Presenter for interpretation. Presenter will act on the received data from View and communicate with Model and produce results to the View.


Here is the classic example for implementing and understanding Model View Presenter pattern in an ASP.Net application. I believed this will helps you to get a good start in Model View Presenter.
Step 1: Create an interface for a business object (Model). For example:


public interface ICircleModel
{
    double getArea(double radius);
}
Step 2: Create a class for Model


public class CModel: ICircleModel
{
    public CModel(){}
    
    public double getArea(double radius)
    {
        return Math.PI * radius * radius;
    }
}
Step 3: Create an interface for View

public interface IView
{
    string RadiusText { get; set;}
    string ResultText { get; set;}
}
Step 4: Create UI like below with a Label, TextBox and Button. Textbox for getting radius of the circle, Label is for displaying the area and Button is for calculating the Area

<html xmlns="<a href="%22http://www.w3.org/1999/xhtml%22">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
<title>MVP-Easy Example for ASP.Net C#</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<th colspan="2">Calculate Area of Circle</th>
</tr>
<tr>
<td>Enter Radius</td>
<td><asp:TextBox ID="TextRadius" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Result:</td>
<td><asp:Label ID="LabelResult" runat="server" ForeColor="red"></asp:Label></td>
</tr>
<tr align="right">
<td colspan="2"><asp:Button ID="ButtonResult" runat="server" Text="Get Area?" OnClick="ButtonResult_Click" /></td>
</tr>
</table>
</form>
</body>
</html>
Step 5: Create a Presenter class for collecting user inputs from View and pass view details to the Model.


public class CPresenter
{
    IView mview;
    public CPresenter(IView view)
    {
        mview = view;
    }
    public double CalculateCircleArea()
    {
        CModel model = new CModel();
        mview.ResultText = model.getArea(double.Parse(mview.RadiusText)).ToString();
        return mview.ResultText.ToString();
    }
}
Step 6: Code-behind of ASPX page - View is communicating to the Model via Presenter

public partial class _Default : System.Web.UI.Page,IView 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void ButtonResult_Click(object sender, EventArgs e)
    {
        CPresenter presenter = new CPresenter(this);
        presenter.CalculateCircleArea();
    }
    public string RadiusText
    {
        get{return TextRadius.Text;}
        set{TextRadius.Text = value;}
    }
    public string ResultText
    {
        get { return LabelResult.Text; }
        set { LabelResult.Text = value; }
    }
}
I hope you are benefitted.

No comments: