재우니의 블로그

MVP, model view presenter, user interface design,model view presenter example,model view presenter c#, enterprise architecture patterns,presenter controller

 

Model View Presenter C# Example

So MVP allows to utilize the full power and productivity of web forms and also allows to develop components that are loosely coupled and testable. Find the code below:-

Simple Example For MVP Design Pattern

Simple Example For MVP Design Pattern

 

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MVP_Pattern_Sample.Default" %>

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>MVP User Interface Design Patterns - C# Example</title>

</head>

<body>

    <form id="form1" runat="server">

    <h5>

        MVP Pattern Sample Demo</h5>

    <div>

        <table>

            <tr>

                <td>

                    <asp:Label ID="lblText" runat="server" Text="--"></asp:Label>

                </td>

                <td>

                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

                </td>

                <td>

                    <asp:Button ID="btnEnter" runat="server" OnClick="btnEnter_Click" Text="Click Here!" />

                </td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

 

 

IView.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 


namespace MVP_Pattern_Sample

{

    public interface IView

    {

        string Label { get; set; }

        string TextBox { get; set; }

    }

 

}

 

IModel.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 


namespace MVP_Pattern_Sample

{

    public interface IModel

    {

        List<String> setinfo();

    }

 

}

 

Model.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 


namespace MVP_Pattern_Sample

{

    class Model : IModel

    {      

        public List<String> setinfo()

        {

            List<String> lst = new List<string>();

            lst.Add("Enter Name:");

            lst.Add("Use capital letter only");

            return lst;

        }

    }

 

}


Presenter.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 


namespace MVP_Pattern_Sample

{

    public class Presenter

    {

       IView _pView;

       IModel _pModel;

 


       public Presenter(IView PView, IModel PModel)

       {

           _pView = PView;

           _pModel = PModel;

       }

 


       public void BindModalView()

       {

           List<String> ls = _pModel.setinfo();

           _pView.Label = ls[0];

           _pView.TextBox = ls[1];

       }

    }
}

 

Default.aspx.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace MVP_Pattern_Sample

{

    public partial class Default : System.Web.UI.Page, IView

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 


        }

 


        #region IView Members

 


        public string Label

        {

            get

            {

                return lblText.Text;

            }

            set

            {

                lblText.Text = value;

            }

        }

 


        public string TextBox

        {

            get

            {

                return txtName.Text;

            }

            set

            {

                txtName.Text = value;

            }

        }

 

        protected void btnEnter_Click(object sender, EventArgs e)

        {

            Presenter p = new Presenter(this, new MVP_Pattern_Sample.Model());

            p.BindModalView();

        }

    }

}