Click here to Skip to main content
15,881,715 members
Articles / Web Development / ASP.NET

Model View Presenter And Its Best Practices

Rate me:
Please Sign up or sign in to vote.
2.73/5 (21 votes)
5 May 2008CPOL2 min read 64.9K   660   51   8
The article describes Model View Presenter Architecture, its best practices and advantages.

Introduction

With the advent of newer technology and framework, architecture design has become very important for development of application. Utilization of the same business functionality across platform based Web application and smart client needs a tactful architecture. Another challenge has been to fit in unit testing framework with the application. MVC failed to achieve this. MVP proved to be a boon to achieve this milestone.

MVP vs. MVC

MVP And MVC

Demerits of Model View Controller

  • Unit testing not possible. Websites do not have a DLL of their own. To achieve this, websites need to be converted to web applications.
  • UI tightly coupled with controller and most of the business logic resides in UI.
  • UI contains intrinsic object of .NET, thus making unit testing impossible.
  • Work stream distribution is next to impossible.

Why MVP ?

  • Work stream distribution for Designer (UI) and Developer.
  • View is decoupled from Presenter and Model (Service & Data Access).
  • This design helps smart client and web application to utilize the common Presenter and Model.
  • Unit testing framework goes well with Presenter.
  • Presenter contains the business logic and the reference of model.

Logical Architecture

Logical Architecture

Physical Architecture

Physical Architecture

Best Practices

Data Access Layer

  • Make use of partial class. Assign class name as screen or page level attribute.
    C#
    ClientLookupDataProvider.cs
    partial public class DataProvider
    {
    }       
  • Work stream distribution possible across developers.
  • Partial class must have static methods in it.

Service Layer

Logical Architecture
  • Service contains a list of all operations which is used by an application.
  • One or more web sites or windows application can utilize these services.
  • Region out each functions for maintenance purposes.
  • Service layer provides an interface which gives access to all data related operations (i.e. from different conventional data sources or by referencing components accessing various data sources).

Presenter Layer

  • Create one BasePresenter which will have all the common attributes across application such as custom exception or initialization of service class.
    C#
    public class BasePresenter
         {
             public IServiceProvider m_Service=null;
             public BasePresenter()
             {
                  m_Service=new Services.ServiceProvider()
             }
             public void ProcessPresenterLayerException()
             {
             }
        }
    
  • Must have public methods and private methods as given below:
    C#
    Public void LoadCustomerList()
    { 
        Ilist<demand> customerList = new list<demand>(); 
        customerList = GetCustomerList(m_View.CustomerID){}
    } 
    Public void SortCustomerList(){}
    Public void PagingCustomerList(){}
    
    Private IList<demand> GetCustomerList(int customerID){}
  • Public methods exposed to 'View' must not have any return parameter or input parameter.
  • Name public method that relate to UI operation terminology terms (Load/Populate/Add/Remove)
  • Name private method that relate to back-end operation it actually does such as Get/Insert/Update/Delete
  • Make sure public methods have try catch block while called private methods don't.
  • Do not make use of intrinsic .NET method calls, e.g. httpContext.Current.Server.mappath/Server.transfer/server.execute, etc.

Domain: Business Entity

  • Initialize private members wherever possible.
    C#
    private int _demandID = int.MinValue;
    private string _demandName = string.Empty;
  • Make use of parameterized constructor and default constructor. Create cyclic constructor if possible.
    C#
    Public Sample (int a, int b, int c): base (a)
    {
    ……
    }
    public Sample(int a, int b, int c, int d):this(a,b,c)
    {
    ..
    }

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Australia Australia
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

Comments and Discussions

 
GeneralMy vote of 3 Pin
khalithbasha18-Oct-12 2:04
khalithbasha18-Oct-12 2:04 
AnswerYeah, yeah, so you can make bullet lists... Pin
Shog923-Apr-08 10:13
sitebuilderShog923-Apr-08 10:13 
GeneralRe: Yeah, yeah, so you can make bullet lists... Pin
santosh poojari28-Apr-08 1:16
santosh poojari28-Apr-08 1:16 
GeneralWhy? When compared to Microsoft's MVC Pin
Mike Lang23-Apr-08 4:37
Mike Lang23-Apr-08 4:37 
GeneralRe: Why? When compared to Microsoft's MVC Pin
Oleg Zhukov24-Apr-08 6:39
Oleg Zhukov24-Apr-08 6:39 
GeneralNot clear Pin
Chris Maunder23-Apr-08 3:40
cofounderChris Maunder23-Apr-08 3:40 
You've discussed bits and peices and presented some snippets but in order to make this valuable you need to show how it all ties together.

As it stands it's not clear enough to be that useful to developers

cheers,
Chris Maunder
CodeProject.com : C++ MVP

GeneralRe: Not clear Pin
Oleg Zhukov24-Apr-08 7:49
Oleg Zhukov24-Apr-08 7:49 
GeneralRe: Not clear Pin
santosh poojari28-Apr-08 1:13
santosh poojari28-Apr-08 1:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.