google-site-verification=ECX1M6_Vb39ty4VtQDXQce6wOjmPTxSettd3hTIZb9Q

MVP User Interface Design Patterns - C# Example

random

MVP User Interface Design Patterns - C# Example


This article provides a basic introduction to the MVP pattern. Using the Model-View-Presenter pattern to encourage proper separation of concerns between presentation and business logic.

MVP - Enterprise Architecture Patterns

Model View Presenter is a software approach pattern conceived as derivative of Model View Controller. It is the evolution of the MVC design pattern and it's aimed at providing a cleaner separation of concerns between the view, the model, and the controller improving the architecture (you can use several UI technologies without recompiling the business logic components) and testability of the enterprise solution.

What is Model View Presenter Design Pattern?


MVP is a User Interface Design Pattern, It is basically used to improve separation of concerns and increase testability of any application. Use model view presenter pattern if you want to maximize the amount of code that can be tested with automation. (because views are difficult to test.)


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


Use model view presenter design patterns you want to separate business logic from user interface (UI) logic to make the code easier to understand and maintain.


Why MVP? and How it is differ from MVC?

  • ASP.Net MVC offers a new development paradigm that offers "More Control" & "Less Productivity" for real world applications. Where as ASP.Net web forms offers a development paradigm that offers "More Productivity" and "Less Control" for real world applications.
  • MVP offers implementation of things that were missing in ASP.Net web forms like separation of concerns and testability.
mvp,MVP design pattern, c# mvp, user interface,Model View Presenter (MVP) design pattern ui user interface, user interface guidelines, design patterns,Model View Presenter,example,sample,asp.net
MVP Design Patterns Architecture

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;
            }
        }

        #endregion

        protected void btnEnter_Click(object sender, EventArgs e)
        { 
            Presenter p = new Presenter(this, new MVP_Pattern_Sample.Model());
            p.BindModalView();
        } 
    }
}
Download the source code, click here

ConclusionIt was fun in learning and writing an article on MVP - Enterprise Architecture Patterns with an example. I hope this article will be helpful for enthusiastic peoples who are eager to learn and implement some interesting stuffs in new technology. 
Please feel free to comment your opinion about this article or whatever you feel like telling me. Also if you like this article, don't forget to share this article with your friends. Thanks!
MVP User Interface Design Patterns - C# Example Reviewed by Ravi Kumar on 11:24 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.