Model View Presenter (MVP) In Aspnet


Published on

A brief overview on the Model-View-Presenter concept

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Model View Presenter (MVP) In Aspnet

  1. 1. Model View Presenter in Presented by : M. M.Al-Farooque Shubho<br /><br />
  2. 2. What is MVP?<br />MVP is a User Interface Design Pattern.<br />Used to improve Separation of concerns and increase testability of any application<br />
  3. 3. Why MVP?<br /> MVC offers a new development paradigm that offers “More control” and “Less Productivity” for real world applications.<br /> Web Forms offers a development paradigm that offers “More Productivity” and “less Control” for real world applications.<br /> Web Form MVP is a trade off between the two. <br />It offers implementation of things that were missing in Web Forms. These are “Separation of concerns and Testability”.<br />So, MVP allows to utilize the full power and productivity of Web Forms and also allows to develop components that are loosely coupled and that are testable.<br />
  4. 4. How to implement MVP?<br />MVP is to be implemented in Web forms where all the existing Pages, Controls and Data Binding features can be used.<br />MVP is all about moving codes from CodeBehind Class to Presenter Classes.<br />Today’s web pages are complex and rarely implements “Single Page, Single responsibility” philosophy. <br />Pages contain modular areas where each have its own View and Functional logic.<br />
  5. 5. How to implement MVP?<br />Web Form MVP supports this design where each area of the page accesses its own View and associated with its own Presenter.<br />Presenters could be implemented small, with minimal set of responsibility and independently.<br />The Presenters could be tested just like MVC and also supports IOC (DI) pattern.<br />
  6. 6. The Players in MVP<br />Model – This is the business object / service / data or a module which has logic in it<br />View – This is the UI which has controls / images / text etc.<br />Presenter – This is the object whose only task to join View and Model.<br />
  7. 7. A “Hello world” MVP <br />Create a web page which will have one textbox, one button and one label. <br />User can enter text in the textbox and click on the button. It should display entered text in the label. <br />For sake of logic if user enters World and click on the button then label will display “Hello world”.<br />
  8. 8. Step 1 : Create the Model<br />A) Create an Interface which will represent the model class.<br />public interface IModel<br />{<br />    string buildText(string text);<br />}<br />
  9. 9. Step 1 : Create the Model…continued<br />B) Create the Model class that implement the IModel Interface and that builds the output string that has to be rendered to the UI.<br />public class Model : IModel<br />{<br />          public Model()<br />          {<br />                   //<br />                   // TODO: Add constructor logic here<br />                   //<br />          }<br /> <br />    public string buildText(string text)<br />    {<br />        return string.Concat("Hello ", text);       <br />    }<br /> <br />}<br />
  10. 10. Step2 : Create the View<br />A) Create an Aspx page (View) having some UI controls<br /><html xmlns="" ><br /><head runat="server"><br />    <title>Untitled Page</title><br /></head><br /><body><br />    <form id="form1" runat="server"><br />    <div><br />        <asp:Label ID="lbl" runat="server"></asp:Label><br />        <asp:TextBox ID="txt" runat="server"></asp:TextBox><br />        <asp:Button ID="bt" runat="server" Text="Enter" OnClick="btn_Click" /><br />    </div><br />    </form><br /></body><br /></html><br />
  11. 11. Step2 : Create the View…continued<br />A) Create an Interface for View. Put a property for each Control in the Aspx/Ascx.<br />The IView should represent a View instance (An object with user inputs as properties) <br />public interface IView<br />{<br />    string Lable<br />    {<br />        get;<br />        set;<br />    }<br /> <br />    string Text<br />    {<br />        get;<br />        set;<br />    }<br />    <br />}<br />
  12. 12. Step3 : Create the Presenter<br />Create a class (Presenter) that talks to the View<br />public class Presenter<br />{<br />    IViewpview;<br /> <br />    public Presenter(IView view)<br />          {<br />        // Define prviate view interface as passed View<br />        pview = view;  <br />          }<br /> <br />    public string buildtext()<br />    {<br />        // Private view lable gets string back from Model object (Business object)<br />        Model m = new Model();<br />        pview.Lable = m.buildText(pview.Text.ToString());<br />        return pview.Lable.ToString();<br />    }<br /> <br />}<br />
  13. 13. Step3 : Create the Presenter…continued<br />The aim of Presenter class is :<br />It should not know anything about UI at all <br />It can communicate with model and view.<br />The Presenter class should:<br />Encapsulate an IView instance<br />Have methods that builds outputs for inputs captured in IView Instance.<br />
  14. 14. Step4: Glue them all<br />Modify the CodeBehind file of View.aspx (View.aspx.cs) so that <br />It implements the IView. <br />Include the properties declared in the IView and use the Control values as private members of the properties.<br />In the Control Event (Button_Click), it asks the Presenter to build output with the input.<br />
  15. 15. Step4: Glue them all…Continued<br />public partial class View : System.Web.UI.Page, IView<br />{<br />    // Declare IView Text property for Textbox<br />    public string Text<br />    {<br />        get { return txt.Text; }<br />        set { txt.Text = value; }<br />    } <br />    // Declare IViewLable property for Lable<br />    public string Lable<br />    {<br />        get { return lbl.Text; }<br />        set { lbl.Text = value; }<br />    }    <br />    protected void Page_Load(object sender, EventArgs e)<br />    {<br />        // Send this object to presenter <br /> <br />    } <br />    protected void btn_Click(object sender, EventArgs e)<br />    {<br />        Presenter presenter = new Presenter(this);<br />        presenter.buildtext();<br />    }<br />}<br />