in

Nikola Malovic

.NET development and architecture
VusCode - Coding dreams since 1998

Add to Google Reader or Homepage Add to Pageflakes Subscribe in NewsGator Online Subscribe in Bloglines Add to Technorati Favorites View Nikola Malovic's profile on LinkedIn

VusCode - Coding dreams since 1998!

.NET exploration, articles, cool links, surf logs, book reviews, .net, c#, smart clients, software factories, patterns & practices, web casts and much more

Model View Presenter (MVP) VS Model View Controller (MVC)

As promised in MVP design pattern - Part 2, today post would cover something which generated a lot (very well deserved) noise last days - Microsoft MVC.NET framework. After some playing with the MVC.NET framework CTP bits and after reading some MVC.NET blog posts, I found myself  thinking about next questions:

  • "What is the difference between MVP and MVC? "
  • "Having MVC .NET in place, do we need MVP?"
  • "What is easier to use for TDD?"

This blog post would try to give answers on those questions

The role of view

If we would compare the diagrams of MVP and MVC patterns

image

we could see that MVP design pattern implementation has a view totally unaware of model. Presenter is the one exclusively communicating with the model and sending DTO (in case of Supervising controller) to model or performing direct manipulation with the view UI elements.

On the other hand, in MVC we have next two "read data" cases:

  • a view reading data directly from model and perform "declarative data binding" of its controls or
  • controller retrieving data from model, pass that context data to view while loading the appropriate view and view then binds to that sent context data

In most of the "update data" use cases we have a view sending updated context data to controller, where controller  performs some validation and/or business logic and updates the model with that data.

Navigation aspects

In general, MVP pattern is based on standard ASP NET page controller pattern implementation, where Page A performs Response.Redirect("PageB") and that results with page B loading with complete page life cycle events fired etc. MVP adds to that sequence just  one additional step: injecting the view to presenter (where view for presenter is just an UI technology abstracted view interface) image

Beside that "default" ASP NET page controller based implementation, there are various ways how the MVP implementation can be enhanced.

One of them is the utilization of application controller  and Page Flow Application Block (which I presented last week on my Web Client Software Factory session) where application controller centralizes flow and navigation concerns and behaves as shared context  container for multiple views covering the same use case.

Page flow application block is a block using the NET 3.0 workflow engine enhanced with additional UI designer used in defining navigation flows. The end result is that you have a separate project with workflow definition describing navigation flow use cases. In a way, that is something very similar to the purpose front controller has.
Having in mind type of enhancement application controller and navigation flow AB bring to standard ASP NET, I am calling the WCSF sometimes "ASP NET web forms on steroids"

MVP summarized: Navigation is handled on a per page basis using page controller pattern approach.

MVC.NET framework is different - more "web suitable" approach to the same concern of efficient handling navigation. It is based on front controller design pattern driven routing engine, which in general represents intercepting of  every http request on a http handler level and checking if there are mapped controller types responsible for handling intercepted Url.

As we can see on first diagram, the flow in MVC starts with controller class and not with web page - view (which is use case in MVP). When controller is been invocated, it usually:

  • retrieves some data from model,
  • performs business logic and
  • picking the appropriate view to which he pass the processed model data
  • loads  the view - which then renders without the need of complete page life cycle being executed

In a way, that is something similar to what we have in Supervising controller MVP pattern, with a difference that in supervising controller the view (page) is still been loaded first and then the presenter class is been created)

MVC summarized: The navigation is handled in one centralized configurable place for all the pages  based on front controller pattern.

I found one thing important enough to be emphasized here: Front controller (routing engine) is a part of Microsoft MVC.NET framework, but that is its separate part and pattern different then MVC pattern.

To illustrate how routing engine is important for MVC framework, I'll just mentioned briefly that I've been working on a POC which was supposed to handle on efficient way the need for having various page UI flavors of the same page - based on multiple business conditions.

I choose to built POC platform based on MVP pattern where multiple views (representing versions of the page) are sharing the same presenter (to remove redundancy) enhanced with custom (Maverick .NET based) routing front controller engine. The end result of this solution based on a front controller + MVP is something which looks (to me at least) very much like what I've seen in MVC.NET framework but just with standard web forms used.

Advantages of the MVP

One advantage I am particular excited about MVP is something David Hayden blogged about and Ron Jacobs talked about with Atomic Object guys. It is the fact that in MVP all you need to develop the presenter is an interface of the view. That interface of the view in passive view version of MVP represents page UI elements, so in a sense it can be considered as page abstraction or even as page contract between the UX (designers) and DEV (developers).  

Although passive view is very cumbersome to be implemented and sometimes lead to complex presenters, the advantages of its usage are much higher in standard waterfall development process, because it enables tech leads  to translate the FRD (Functional Requirement Document) requirements right at the beginning of development phase and produce sets of view interfaces (abstracted pages).

Therefore, communication on relation tech lead->developer is concentrated to the beginning of development phase with very precise set of artifacts defining requirements. Once the interface of the views would be defined and verified as the one matching the FRD requirements, developers are able to start developing their presenters including the unit tests to verify that FRD functional requirements are fulfilled well.

The end result of that activity would be a list of unit tests representing FRD requirements. Percentage of successfully "green" test of that list can be used then to track realistic project progress. 5 green tests of 100 FRD tests=> 5% progress report.

image

What we get as the end result is:

  • totally decoupled and effective working streams of tech lead, developer and designer
  • set of artifacts useful in tracking the real project implementation progress  and as a guidance to developers what requirements they have to met.

IMHO, Second advantage MVP has is that while MVC pattern feels more natural in some web development scenarios,  MVP pattern based development offers "crossing the boundaries" out of the box by utilizing the fact that presenter is not aware of the view UI used technology. Both view interface and presenter  are living even in separate class library project which allows their re-usage in win form applications too.

I know, most of us thinks about that as "cool but YAGNI", "we are doing only web development", but once you would see how easy it is to get the smart client application from already built web application, you would start thinking about that too.

Think about another idea: win form UI development is much faster to be implemented and modified so imagine doing prototypes of your web sites in win form just to speed up process of verifying if business needs would be met with application. Once that would be verified, you would still use the same presenter you used for win form and build a web form UI layer on top of it but this time with already confirmed business expectations.

MVP and MVC in Test Driven Development

As stated already in this post, MVP biggest advantage is enabling writing presenter tests with only view interface available.
On the other hand, MVC.NET approach to TDD offer much more power with providing ability of mocking the flow itself (you can mock context, request, response) + providing the controller based testing on a way similar to testing the supervising controller (setting the context DTO to desired values to trigger certain test flows).

Although that looks maybe to someone on first sight as an overkill, according to the Jeremy D. Miler (and he knows what he's talking :) ) the simple nature of MVC route -> controller -> view makes writing complex web form tests much easier

I spent decent amount of time of this 2007 doing MVP tests in web forms and although there were a lot of hurdles during that time I face, I can not say that I felt too much pain mainly because I was mostly using  either front controller or application controller as MVP supplemental patterns

Right now, to me too MVC looks a little bit more complex to be TDD-ed and I don't know if the advantages of the MVP can be achieved easily in MVC too, but I realize that I don't have too much experience with real world TDD-ing MVC usage but I know that I can relay on Jeremy's opinion.

The promise of having easier TDD experience with MVC in upcoming period is making me very happy and really eager to dive deeper in MVC, so be prepared for some blog posts on that subject (if anything left to be blogged after all this MVC .NET posts appearing last days like mushrooms after the rain :)

Share this post :
Published XII 18 2007, 06:25 dop. by malovicn
Filed under:

Comments

 

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

prosince 18, 2007 7:35 dop.
 

Wöchentliche Rundablage: ASP.NET MVC, ASP.NET 3.5, ADO.NET Data Services "Astoria", ASP.NET AJAX, Silverlight… | Code-Inside Blog said:

Pingback from  Wöchentliche Rundablage: ASP.NET MVC, ASP.NET 3.5, ADO.NET Data Services "Astoria", ASP.NET AJAX, Silverlight… | Code-Inside Blog

prosince 24, 2007 12:50 odp.
 

Just another .Net Voyegar » Blog Archive » Some more reading on Dependency Injection said:

Pingback from  Just another .Net Voyegar  » Blog Archive   » Some more reading on Dependency Injection

března 12, 2008 9:13 odp.
 

Dum spiro, spero » Blog Archive » MVP / MVC said:

Pingback from  Dum spiro, spero  » Blog Archive   » MVP / MVC

září 13, 2008 6:28 odp.
 

Oleg Zhukov said:

Very comprehensive review!

You might be interested in a Model View Presenter framework implementation for .NET - <a href="http://www.mvcsharp.org" title="Model View Presenter for .NET">MVC# Framework</a>.

Thanks,

--

Oleg Zhukov

října 30, 2008 1:51 odp.
 

ASP.NET MVC Archived Buzz, Page 1 said:

Pingback from  ASP.NET MVC Archived Buzz, Page 1

listopadu 6, 2008 4:08 odp.
 

jamesannaa said:

Cialis

There are some people who say that top branded drugs are way more effective than the generic ones. However, this does not hold much truth when it comes to the generic medicines which are sold here. An online pharmacy which offers generic medicines at affordable prices. If you have been diagnosed with a sickness and your medication requires you to spend a lot for your medicines, you don't have to worry about this anymore. You can by <a href="www.onlinepharmacy.vg/.../-c-32_469.html">cialis</a>">www.onlinepharmacy.vg/.../a>and">www.onlinepharmacy.vg/.../a>and get a discount when you keep on buying the same drug. When you cannot tolerate the high price of branded medicines, your only option is an online pharmacy, since it is compassionate with your need for reasonably priced drugs.

If your medication calls for high-priced branded medicines, why not generic cialis? This is an alternative which is provided at your affordable online drug store. One of the advantages that you can get from this store is the fact that its medicines can be bought for very low prices. You can order generic drugs at the website. These medicines are formulated with ingredients which are equal to the ones which make up branded drugs. However, the prices of the two are entirely different. Even though the prices of generic drugs and branded drugs vary widely, the effects are still the same. This is due to the fact that the medicines which are sold at this online pharmacy have been approved by worldwide organizations. The effectiveness and the quality of the generic drugs are regarded as excellent. The legality of these drugs has been supported by international regulatory boards. Some of these authorities are South Africa MCC, USA FDA, and WHO.

When you buy <a href="www.onlinepharmacy.vg/.../-c-32_469.html">cialis</a>">www.onlinepharmacy.vg/.../a>, the shipping service is offered for free. If you are residing within the European continent or at the other pole of the globe, the medicines that you have ordered will still be delivered to you. If you are a resident from the USA or from Europe, the duration of the shipping service can take fourteen days. One of the services that this online drug store provides its customers with is the chance to get a discount. Each time you order the same drug, you will be offered with a five-percent discount. Since the online pharmacy is compassionate with the plight of its sick patients, it does not charge you with hidden fees. Nor does it charge you when you consult with its physicians.

You can buy <a href="www.onlinepharmacy.vg/.../-c-32_469.html">cialis</a>">www.onlinepharmacy.vg/.../a>when you are diagnosed with a sickness. Serious ailments such as cancer or manic depression can be treated with some of the medicines sold at this online pharmacy.

Author

James Ann   www.onlinepharmacy.vg/.../-c-32_469.html

listopadu 17, 2008 3:15 odp.

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

Powered by Community Server (Non-Commercial Edition), by Telligent Systems NAVRCHOLU.cz