Comparison of Architec…

来源:互联网 发布:电脑麦克风软件 编辑:程序博客网 时间:2024/04/30 21:41
http://www.codeproject.com/Articles/66585/Comparison-of-Architecture-presentation-patterns-M


Introduction

This article will compare four important architecture presentationpatterns i.e. MVP(SC),MVP(PV),PM,MVVM and MVC. Many developers areconfused around what is the difference between these patterns andwhen should we use what. This article will first kick start with abackground and explain different types of presentation patterns. Wewill then move ahead discussing about the state , logic andsynchronization issues. Finally we will go in detail of eachpattern and conclude how they differ from each other.

Here’s my small gift for all my .NET friends , a complete 400 pagesFAQ Ebook which covers various .NET technologies like Azure , WCF ,WWF , Silverlight , WPF , SharePoint and lot morefrom here. 

Background- Presentation patterns  

One of the biggest problems associated with user interface is lotof cluttered code. This cluttered code is due to two primaryreasons , first the UI has complicated logic to manipulate the userinterface objects and second it also maintains state of theapplication. Presentation patterns revolve around how to remove theUI complication and make the UI more clean and manageable. Beloware different variety and classifications of presentation patternsas shown in the below figure.

Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

Presentation pattern are divided in to three important categoriesMVP ( Model view presenter ) , MVC ( Model view controller) andfinally (PM) presenter model. MVP is further divided in tosupervising controller and passive view. Presenter model is furtherdivided by Microsoft team in two technology specific patterns MVVMfor silverlight and MVVM for WPF.

3 big problems of UI :- State , Logic and Synchronization

There are 3 big problems associated with UI as listed below.

State :-State / data is one of the biggest concern in UI. State means thecurrent data picture of your user interface. In web terminologiesit can be session variable and in windows application it can be asimple UI level data. The more the UI maintains states ,the morecomplication is increased.

Logic :-User interface normally have UI logics like manipulating textboxes, combo boxes or any other UI elements.The more these kind of logicexists in the UI the more it becomes complex.

Synchronization :-User interfaces normally co-ordinate with domain / businesscomponents. UI also needs to synchronize data between UI elements (textboxes , comboboxes etc) and business objects. If your UI istaking of synchronization again the UI complexity increases.

Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

 

The Hero - Presentation design Pattern

Presentation design pattern helps to solve the above UI problems.The base logic of presentation design patterns is that we need tocreate a extra class which will consume complicated logic , dataand synch issues which currently the UI does , thus making the UIdump , clean and simple. Depending on how much this class takesresponsibilities defines further whether its a SC , PV , PM designpattern etc. In other words its the maturity of the presenter classwhich will define what kind of design pattern it is.

Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

 

Some acronyms to simplify the article

AcronymFullformVView or user Interface.PPresenter class which has the UI logic.LUI logicSState of the UIMBusiness components or domain objects.SCSupervising controller .PVPassive view.PMPresenter model.

 

We will use the above acronym to simplify our explanation ofpresentation design pattern.

 

Supervising controller pattern ( SC )

Fundamentals about SC :-

  • State is stored in the view.
  • Presenter owns the complex presentation logic. Simple UI bindinglogic is taken care by using binding technologies like WPF bindingand Silverlight binding. Anything complex is taken care presenterclass.
  • Presenter is aware of the view.
  • View is not aware of the presenter.
  • View connects with model using technical bindings provided by WPFand Silverlight.

Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

Passive view (PV)

Fundamentals about PV :-

  • State is stored in the view.
  • All logic of UI is stored in presenter.
  • View is completely isolated from the model. It also takes the extratask of synchronizing data between model and view.
  • Presenter is aware of the view.
  • View is not aware of the presenter.

You can read more about MVP (PV) from this , it also has a samplecode which shows how MVP comes in toaction link

Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

 

Presentation Model (PM)

Fundamentals about SC :-

  • State is stored in the presenter.
  • Logic is stored in presenter.
  • Presenter represents a abstract view of the UI.
  • Presenter is not aware of the view.
  • View is aware of the presenter.
  • View is completely isolated from the model.
Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

 

 

MVVM

Fundamentals about SC :-

  • Presenter model is the base.
  • State is stored in the presenter.
  • Logic is stored in presenter.
  • Presenter represents an abstract view of the UI.
  • Presenter is not aware of the view.
  • View is aware of the presenter.
  • View is completely isolated from the model.
  • Uses WPF and Silverlight bindings.

Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

 

MVC

Fundamentals about MVC :-

  • Does not have a presenter , has a controller.
  • Request first comes to the controller.
  • Controller binds the model with the view.
  • Logic is stored in the controller.

You can read more about MVC from this, it also has a sample codewhich shows how MVC comes in to action link

Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

 

Summarizing

Below is a summary comparison table for all presentation patternsfrom the aspect of state , logic and synchronization.

  StateLogicSynchronizationSupervisingcontroller     Presenter XX ViewX   ModelView connects to model for data using databindings like WPFbindings , Silverlight bindings etc.PassiveView     Presenter XX ViewX  Presentermodel     PresenterXX  View  XMVVM     PresenterXX  View  X Databinding commands of WPF , Silverlight , ASP.NET is used.MVCController XX ViewX  

 

Below is a visual comparison of what we discussed above.

Comparison <wbr>of <wbr>Architecture <wbr>presentation <wbr>patterns <wbr>MVP(SC),MVP(PV),PM,MVV

 

When to use MVP and When to use MVC ?

First thing all the above patterns are variations of MVC. In otherwords all MVP patterns are twists of MVC. We will compare betweenMVC and MVP first and then we will compare between differentvariations of the MVP.

Below are the situations of when MVP is preferred over MVC.

UI unittest :- If your project is laying morestress on automated unit test on UI then MVP is preferred over MVCbecause the presenter class of MVP isolates all the UI logic in tothe presenter. The presenter is a complete mock of the UI so itcan  be unit tested separately using VSTS testsuite or NUNIT.

Views aresame with slight variations :- If yourapplication nature is such that it has views which can be reusedwith different data then MVC is a good thought. Especially if youhave a application which is more report oriented. For example youhave a two views which are almost same like 'Sales by month' and'Sales by year'. Both these reports can be shown by using the sameASPX page probably with different models. So if your applicationhas same UI look and feel with slight variations MVC is a good fit.Especially if you have reporting nature kind of application MVCwill fly as compared to MVP.

Multi UIsupport :- If you forecast that yourapplication will consume different UI technologies then MVP is goodfit. In other words if your application will be consumed by windowsUI as well as web application UI then  MVP is agood fit. It will help you to increase reusability by using thepresenter class.

ComplexScreens :- If you have complex screenwith lot of user interaction MVC can be complicated as you end upwith lot of controller classes for every interaction. MVP here is abetter suit because you can encapsulate those complex logic in to aclass and test it separately to ensure a bug free code.

Technologyused :- Technology is a very importantfactor. If technology provides automation for architecture buildingit becomes more sense to use patterns accordingly. For instance ifyou are purely using ASP.NET application with out Silverlight orWPF its more sensible to go with MVP. ASP.NET pages expect thecontrol events to be handled on the behind code while MVC expectsthese events to be handled on the controller. So we end up withrewriting the complete code of event handling for ASP.NET controlson the controller. If you are using silverlight and WPF they havenice binding support which makes MVP a better fit. On the otherhand if you have same view with slight variations and you are have3.5 as a framework you can use MVC framework to automate and suityour needs.

Technology selection drives 60% of whether you will select MVC orMVP.