Click here to Skip to main content
Email Password   helpLost your password?

Table of Contents

Introduction

Clear separation of responsibilities along with a low coupling level is a sign of a well-designed application. Whereas design patterns are proven solutions to reduce coupling between small sets of objects, architectural patterns help to improve a system's design on the whole. One popular architectural pattern is Model-View-Controller. It originally came from Smalltalk and now it has implementations in various languages. In Java for example such Frameworks as Spring and Struts have gained high popularity and are widely used. However in the .NET world, the existing implementations aren't much spread, being mainly inspired by Java implementations they fit well only for Web applications and are not suitable for Windows thick client applications.

Another architectural pattern that in fact is an evolution of MVC is Model-View-Presenter. The underlying ideas of MVP are very similar to those in MVC however MVP is designed to be more usable and understandable. MVP has much less implementations than MVC does, one known MVP implementation for .NET is the Microsoft User Interface Process application block (UIPAB). In spite of numerous problems, and hence low usability of UIPAB, its reasonable key concepts inspired us to create something new.

This article starts a series covering the development of a native Model-View-Presenter Framework for .NET platform. Although it will be an MVP Framework, we will still use the term "Controller" as it seems to be more pertinent than the "Presenter" notation (UIPAB follows the same manner describing itself as MVC, though in fact it is closer to MVP). Let us start with clarifying the basic concepts of an application's structure. After that we will proceed to the discussion of the existing architectural solutions (including MVC and MVP).

Basic Concepts

Every application can be split into a number of tasks. Here by task we mean a sequence of actions which accomplish a job, problem or assignment. There are different ways of breaking an application into tasks, for example, tasks may be associated with use cases. Each task involves interaction with a user done via so called interaction points. An interaction point is represented by one or more classes which serve the following needs:

The following figure illustrates the described relationships:

As we can see, the interaction point responsibilities are rather vast. That is why architectural solutions such as MVC make their aim to split interaction points into simpler parts. Next we will consider various ways of splitting the interaction point (including the MVC paradigm). Though the division of the interaction point into smaller elements is preferred, we will start with a case when an interaction point is represented by a single view class.

Interaction Point as a Single View

When the interaction point is made of a single view class it is the view which is responsible for all intermediate data processing. Such view class handles user input, makes proper calls to the business objects, analyses the data received from them, decides what to display to the user and actually displays it.

In order to demonstrate this and the other approaches, let us now introduce the example we will refer to throughout the article. Consider the encyclopedia application where a user enters some term and the system gives back the explanation for it. In most cases, the explanation is unambiguously found for a term. However sometimes several explanations of a term (especially if the term is an abbreviation) may apply and the system then asks the user which one to display.

With a single view approach, the sequence diagram for our encyclopedia application would look as follows:

This figure reveals the drawbacks of combining all intermediate logic into a view class. The view then becomes too bloated and difficult for understanding. Moreover such approach violates the single responsibility principle by uniting in the view two conceptually different responsibilities. First is making requests to the model and deciding what to display (application logic) and the second is actually displaying (presentation mechanism). As a result if we want to make our encyclopedia work both as a Windows and a Web application, we will have to duplicate the application logic in two view classes: one for Windows and the other for Web environment.

Model-View-Controller

We have seen the downsides of the solution when the interaction point is represented by a single view class. On the contrary to the single view technique MVC does break the interaction point into three parts. These parts are: Controller, View and Presentation model.

Controller handles user input, makes subsequent calls to the business objects and manages the application flow. In particular the controller decides what to display to the user. However it is not allowed in MVC to access the view directly, instead the underlying model should be altered and the changes will be propagated to the view through the observer mechanism. Thus in order to make the view update itself, the controller should change the presentation model object.

In our encyclopedia example, the controller class asks the model for the proper explanations (1.1 in the figure below) and passes the returned explanations to the presentation model (1.2). Depending on how many explanations were found (one or more) the controller sets a flag in the presentation model indicating whether the user should choose the desired variant (1.3). The view then reflects the changes in the presentation model (1.3.1) and either displays dialog with several variants (1.3.1.3) or displays the only found explanation (1.3.1.4).

The main advantage of MVC is a clear distribution of responsibilities between parties. The controller drives the application flow specifying what and when it should be done. The view only renders the underlying business and presentation models and presents them to the user. Since views in MVC do not contain any application logic, they can be harmlessly substituted, for example there may be different view classes for Windows and Web interfaces.

Nevertheless there are two major drawbacks in the traditional MVC approach. First is a higher complexity because of the observer mechanism: in order to update the view the controller must make changes to the presentation model, which will trigger the view update. Such indirect relationship may be difficult to understand. Instead the controller could simply send a message to the view, however such direct linking is not allowed in MVC.

The other shortcoming is that MVC does not conform to the modern UI programming environments where widgets themselves handle user gestures. For example form classes (either Web or Windows) in .NET applications by default contain handlers for user input. Thus it would be difficult to break the common paradigm and make controllers receive the user input instead of views.

The next architectural pattern we consider was designed to eliminate the drawbacks of MVC, while preserving its separation of application logic and presentation mechanism.

Model-View-Presenter

Model-View-Presenter approach appeared in the late 1990’s and was an evolution of MVC pattern. Above we have described two typical shortcomings of the MVC pattern. Now let us look at how MVP eliminates these two.

Firstly according to MVP, direct requests from the controller to the view become possible. Thus the controller itself may trigger the view updates instead of performing a round trip through the presentation model. In this sense the controller in MVP contains the presentation model from MVC. That is probably the reason why the controller in MVP is referred to as presenter (however we will continue naming it controller).

Here we must note that although the controller has a link to the view object it does not depend on the concrete view class. Instead the controller treats the view in an abstracted way by the means of a separated interface implemented by the view (see the figure above). For example, the encyclopedia controller will communicate the view via the IEncyclopediaView interface with chooseExplFrom(…) and displayExpl(…) operations. Since such separated interface is a part of the application logic, the dependency runs from the presentation to the application logic but not vice versa.

Next thing that makes MVP more convenient (in contrast to MVC) is that it allows views to receive user input. Such behavior fits well modern UI environments. For instance in Windows keyboard and mouse events are handled by UI controls and forms, in ASP.NET user requests are processed by Web page classes. Though the view in MVP receives the user input it should merely delegate all the processing to the controller.

The next figure demonstrates the interactions in MVP by the example of the encyclopedia application. As we can see direct calls from the controller to the view (via the IEncyclopediaView interface) make the overall picture less complicated than that in MVC. Yet the clear separation of responsibilities between the controller (application logic) and the view (presentation mechanism) still remains, in particular allowing the developer to easily support interchangeable views for Web and Windows environments.

Summary

Let us sum up what we have discussed so far. Among the architectural patterns we have considered, MVP seems to be the most attractive one. It allows building flexible systems without overheads peculiar to the classic MVC approach. That is why it is MVP that we are going to base our Framework on.

Proceed to Part 2: Implementing Core MVP Functionality

Project Website

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralWhat will be the future..
Nirosh
5:06 19 Oct '08  
Hi Oleg,

Given that this is 2008, and having .net 3.0 - 3.5.

How applicable these concepts, as an indication of future direction of the user interface processing within the Microsoft .NET Framework? Don't you think that this is already out dated?

Let me have your thoughts..

- A random opportunity is like a taller chair, those who sit hang on, those who hang on fall

L.W.C. Nirosh.
Colombo,
Sri Lanka.

GeneralRe: What will be the future..
Oleg Zhukov
14:33 20 Oct '08  
Hi Nirosh,

This is a fair question and it just needs to be answered!

The concepts of interaction points (namely views) and possible navigation between them cannot be outdated. They reflect a human's natural ability to work with one interaction context (view) at a moment and possibility to switch to other adjacent contexts.

The above concepts originate from Constantine's "Software for use" legendary book. He talks about Content Model which declares a set of interaction context (interaction points in my terminology) and context navigation map (possible navigation routes between points).

Interaction points simply represent small parts of the user interface and are applicable both in "old" Windows Forms and in brand new Silverlight 2.0. And they will remain topical for .NET FX 4.0, 5.0, etc... Smile

Regards,

--
Oleg Zhukov

Generalthanks, and encouragement for a second article
BillWoodruff
17:31 5 Sep '08  
Hi Oleg,

I enjoyed your article !

In a response here dated June 8 you wrote : "To be honest, I will more likely write a new article with a deeper insight to the problem than changing this one."

I would really enjoy reading any analysis you might care to share of MVC as offered in FrameWork 3.5 SP1 and ASP.NET 2.0 in light of your model and your knowledge of the "classic" MVP paradigms.

I've been trying to think about the issues of "Undo/Redo" and persistence of user preferences and "state" in MVC, but so far feel I am not smart Smile

thanks, Bill

"The greater the social and cultural distances between people, the more magical the light that can spring from their contact." Milan Kundera in Testaments Trahis

GeneralThanks a lot
Abhijit Jana
3:58 3 Aug '08  
Thansk for sharing. Its help me a lot.
good job !!! Full credit from me to you !!

cheers,
Abhijit

QuestionPresentation Model
taras.di
8:57 2 Jul '08  
Hi there,

I'm confused as to your definition of MVC.. in MVC, AFAIK, there are only 3 interacting components, the model, the view, the controller (and no 'presentation model'). In this version, the view and the controller both have references to each other. Is the MVC you are describing in your article a variant of this 'traditional' MVC?

Thanks

Taras
AnswerRe: Presentation Model
Oleg Zhukov
9:35 2 Jul '08  
Hi Taras,

Don't be confused - I'm using the classic MVC definition. As for "presentation model" - it's merely a part of the Model (since Model can be split into two parts: the Domain model and the Presentation model).

Next, the classic MVC approach doesn't allow direct references between controllers and views. However in practice many frameworks violate this rule by allowing to pass data from controllers to views (e.g. in ASP.NET MVC framework controllers use RenderView(..) method to pass data to views).

Best regards,

--
Oleg Zhukov

GeneralRe: Presentation Model
taras.di
9:54 2 Jul '08  
Hi Oleg,

Do you have a reference explaining the 'presentation model' and the 'domain model'. I'm guessing perhaps that the presentation model stores persistent data relating to the presentation of the model (eg: colors of lines, or something along those lines), whereas the 'domain model' stores domain specific data (eg: encyclopedia definitions), but that's only a guess.

With regards to the 'classic MVC approach' - all references I have seen so far allow the direct coupling of the view and controller (eg: Gamma et all, there's been a whole bunch of others as well). In fact, this is the first article I've seen on MVC where the view and the controllers aren't coupled. Do you have any references to your classic MVC definition?

I'm not trying to critique your article just for the sake of it, I'm just trying to make sense of the information about MVC/MVP on the internet Smile.

Taras
GeneralRe: Presentation Model
Oleg Zhukov
10:44 2 Jul '08  
taras.di wrote:
Do you have a reference explaining the 'presentation model' and the 'domain model'. I'm guessing perhaps that the presentation model stores persistent data relating to the presentation of the model (eg: colors of lines, or something along those lines), whereas the 'domain model' stores domain specific data (eg: encyclopedia definitions), but that's only a guess.


Yes, you're quite correct. As for a comprehensive explanation of all things about MVC/MVP, I recommend reading this Martin Fowler's article: http://www.martinfowler.com/eaaDev/uiArchs.html

taras.di wrote:
I'm not trying to critique your article just for the sake of it, I'm just trying to make sense of the information about MVC/MVP on the internet .


Unfortunately there are dozens of contradictory definitions of MVC on the Internet, and none of them is 100% correct Smile My definition is close to the Fowler's one and assumes the controller-view communication to go across the Model (via Observer mechanism).

--
Oleg Zhukov

QuestionRe: Presentation Model
taras.di
9:45 2 Jul '08  
Secondly, I'm confused as to the benefits of MVP over MVC.

The first is no direct coupling between the controller and view - my previous post has highlighted that I don't even know if this is MVC, so I'll wait for your response on that one Wink.

The second is the fact that the pattern is incompatible with current frameworks, as the the event handlers are often wired into the view class (where the controller should be handling the events). You write that in MVP that the view still handles the event, but simply delegates the handling to the controller (and thus bypassing the problem). However, the exact same tactic can be used in MVC, so I'm not quite sure how this is an advantage of MVP over MVC.
AnswerRe: Presentation Model
Oleg Zhukov
10:49 2 Jul '08  
taras.di wrote:
The first is no direct coupling between the controller and view - my previous post has highlighted that I don't even know if this is MVC, so I'll wait for your response on that one .


See my reply above.

taras.di wrote:
You write that in MVP that the view still handles the event, but simply delegates the handling to the controller (and thus bypassing the problem). However, the exact same tactic can be used in MVC, so I'm not quite sure how this is an advantage of MVP over MVC.


If you use the same tactic in MVC then it automatically turns into MVP Smile One firm MVC's requirement is that Controllers handle user input but not views!

--
Oleg Zhukov

Generalvery nice presentation
Deepthi Viswanathan Nair
12:44 28 May '08  
hi,

It is very nice that you have explained the model in a simple manner with the comparison of other models, so everyone can unsderstand easily. the pictorial representation of various models are very nice too.

I hope that it would be better if you could highlight the pros and cons of each model.

Smile

Thx
Deepthi V

GeneralRe: very nice presentation
Oleg Zhukov
3:28 20 Jun '08  
Deepthi, thank you for appreciating the work!

To be honest, I will more likely write a new article with a deeper insight to the problem than changing this one Smile The most comprehensive explanation of above patterns is given by Martin Fowler here: http://www.martinfowler.com/eaaDev/uiArchs.html - I recommend reading it thoroughly.

Thanks,

--
Oleg Zhukov

GeneralGreat Article Series even for the Invoice...
Farrukh_5
23:04 18 Feb '08  
Great Article Series, even for the Invoice...

Thanks for sharing... Shucks Rose

---------------------------
Life is a game... with limited life line and unlimited power!

http://www.idlsol.com

GeneralRe: Great Article Series even for the Invoice...
Oleg Zhukov
1:53 19 Feb '08  
Thanks for such a positive feedback Smile

--
Oleg Zhukov

GeneralNice Article
Joey Chömpff
8:13 12 Feb '08  
Hi,

Nice article, I'm a big fan of the MVP pattern, because it's simple and has an logical design.

You should also read(for ideas and for the right terminology):
http://martinfowler.com/eaaDev/SupervisingPresenter.html[^]
http://martinfowler.com/eaaDev/PassiveScreen.html[^]
http://www.codeplex.com/smartclient[^]
http://www.codeplex.com/websf[^]
http://www.codeproject.com/KB/architecture/ModelViewPresenter.aspx[^]

If I were you I would say that the Model could also be an (Web-)(WCF-)Service, DTO, DataContract, etc.
Don't forget to mention (unit)-testing.

Keep on going.

Greetz

Joey Chömpff
.NET Developer

GeneralRe: Nice Article
Oleg Zhukov
14:27 12 Feb '08  
Joey, thank you for valuable comments.

Actually (and Martin Fowler mentions it) MVP utilizes the Supervising Controller(Presenter) pattern. It is a trade-off between the Passive View approach and the observer view synchronization. And, as much of the trade-offs, Supervising Controller and MVP better fit real-life apps.

Joey Chömpff wrote:
If I were you I would say that the Model could also be an (Web-)(WCF-)Service, DTO, DataContract, etc.



I assumed business objects to be anything from DB table gateways to Service access points, though I did not express this assumption explicitly Smile

--
Oleg Zhukov

GeneralRe: Nice Article
degetya
6:12 31 Jul '08  
Hi Oleg,
I've been serching web and codeproject as well for MVC for a while, and I see i just got the real meaning of MVC and MVP after reading your article.

Thanks for the short and clear explanation, great work ! Smile


Last Updated 11 Feb 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010