I recently had the pleasure of attending a talk (twice) by Scott Cate about the Model View Presenter design pattern as implemented in .NET. I had never worked with this design pattern before and must say it was a joy to listen to Scott give this talk. He style is very natural, code oriented, and he stays very connected to the audience.

The concept of this pattern is one of seperation of UI from Implementation. Most N-Tier implementations believe they do this well, but in fact still fill their code-behind (for ASP.NET) or form class (for WinForms) with logic which retrieves data, validates, etc. The Model View Presenter (MVP) suggests a different way, in which you do not place this in your application, but rather in a presenter object. But before we go further, let's define some of the terms.

Model - The model is your business objects, or data access layer, which provides the data which needs to be presented.

View - The view is an Interface which defines how information is both retrieved and set.

Presenter - The presenter is a class which requires a "view" object to be constructed and can then present methods which will act upon the view.

As a simple example, let's take the concept of a simple blogging software which stores all of the posts in an XML file. The XML file will look like this :

<Posts><Post id=1><Title>Model View Presenter Rocks!</Title><Body>Lots of cool text goes here</Body></Post><Post id=2><Title>BizTalk Rocks!</Title><Body>Lots of cool text goes here</Body></Post><Post id=3><Title>Dot Net Rocks!</Title><Body>Will this get me in trouble with Cory?</Body></Post></Posts>

Given this, which we keep in a "posts.xml" file, we will implement a super simple Model object.


public class Post{private Post() { }// Private Constructorinternal string _Title;public string Title{get{return _Title;}}internal string _Body;public string Body{get{return _Body;}}public static Post GetPostById(int id){XmlDocument posts = new XmlDocument();posts.Load("posts.xml");string strXpath = "/Posts/Post[@id={0}]/{1}";XmlNode titleNode = posts.SelectSingleNode
"Title"));XmlNode bodyNode = posts.SelectSingleNode
(String.Format(strXpath, id,
"Body"));Post retPost = new Post();retPost._Title = titleNode.InnerText;retPost._Body = bodyNode.InnerText;return retPost;}}


Now that we can get data from the file, we need to create our view. Our view is an Interface which supports "get"ing the post id, and "set"ing the title and body.

public interface IPostView{int PostId { get; }string Title { set; }string Body { set; }}

Finally, we have our presenter. Our presenter requires an implementation of our interface to be constructed and offers a method which allows retrieval.


public class PostPresenter{private readonly IPostView view;public PostPresenter(IPostView view){this.view = view;}public void RetrievePost(){Post myPost = Post.GetPostById(view.PostId);view.Title = myPost.Title;view.Body = myPost.Body;}}

If you want to see all of this implemented as a Windows Form application, then grab the download here.