Channels ▼


The Model-View-Presenter Pattern in ASP.NET

Writing the user interface of a Windows system is way too easy. Every new version of Visual Studio makes the task simpler and simpler through wizards and tools of every sort. Code-behind files in both ASP.NET and Windows Forms, but also to some extent in Windows Presentation Foundation (WPF), seem to suggest developers not to waste their time with design and stratification of software. A plain old ADO.NET query right in the code-behind class of a page attached to the handler of a Click event does the job. And it's effective, and it's quick. And it just works.

More generally, we're not far from truth if we say that user interface facilities encourage developers to put in the presentation layer more stuff and code than it reasonably should. As a result, the presentation layer operates as a catch-all for logic of other layers, mostly the middle tier, but often also the data access layer. Is this really bad? And, if so, why is it all that bad?

I'm the first in line who just doesn't bother to place an ADO.NET query in the Button1_Click event handler of a code-behind class — if that page belongs to a temporary site or is destined to stay up and running only for short time; for example, an ASP.NET page that lives the three months around a user-group meeting. But even copycatting a Web site project that does the same few and well-known things, an approach that provides simplicity and time-to-market, isn't a bad thing per se. So feel free to make use of SqlDataSource controls, Linq-to-SQL, and plain ADO.NET stuff. All rigorously done right in the code-behind.

But for large, durable, enterprise-class systems, it's all another story.

Here, stuffing code in the presentation layer that logically belongs to other tier is a mortal sin. Presentation and model are to be neatly separated and classes with some embedded user interface are really hard to test efficiently and safely. Not to mention the risk of code duplication and the inherent difficulty to evolve and enhance the system providing different views of same model. The Model-View-Presenter (MVP) pattern can be a significant help to rationalize the presentation layer.

MVP assumes a data model, a view class and some logic to coordinate the display of information to the user. The data model represents the entities in play. The view class knows about the user interface elements and is ultimately responsible for providing a user-consumable view for the data model. Pushing data into the view is the main task of the presenter. The presenter typically holds a reference to both the view and the model. The view receives input from the user and fires events to the presenter. The view doesn't know about the model. It receives data from the presenter according to the details of a contract in place between view and presenter. That's it.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.