Click here to Skip to main content
15,891,529 members
Articles / Desktop Programming / Windows Forms

Winforms – Model-View-Presenter – A tutorial

Rate me:
Please Sign up or sign in to vote.
3.59/5 (12 votes)
11 May 2009CPOL4 min read 73.2K   34   18
Winforms – Model-View-Presenter – A tutorial

Introduction

Coming from C/C++ and diving into .NET, I am constantly surrounded by strange phrases, three letter acronyms and concepts that take a long time to get used to. One of those concepts is the Model-View-Presenter (MVP). There are plenty of articles about MVP around the Web. Most are pointers to Martin Fowler (http://www.martinfowler.com/eaaDev/uiArchs.html) or the MSDN (http://msdn.microsoft.com/en-us/library/cc304760.aspx). There are also many articles on CodeProject (www.codeproject.com). The problem that I have had in the reading of these articles is that most use big $.25 words and unfamiliar phrases that mean nothing to the beginner, along with diagrams that are just plain confusing. Nothing I found that was in plain simple English. After a lot of trial and error and study, I think I finally have something that I can understand. I do not pretend that this is the “right way” or the only way but it works for me.

MVP

The goal of using the MVP design pattern is to separate the responsibilities of the application in such a manner as to make the application code:

  • Testable in an automated manner
  • Make the application more maintainable
  • Make the application more extensible

The first bullet means that there are many tools out there in the world that can be used to test code in an automated manner, called “unit testing”. These tools cannot test code that is in form code-behind modules. So the aim is to take that code out of the form code and place it in a DLL that can be tested.

I do not know about anyone else, but for me application code is way more maintainable when it is separated out into modules that have a specific purpose. If some unexpected feature does make it through the unit testing, then you will have a pretty good idea where to look just based on knowing what the various classes’ responsibilities are.

The extensibility part comes in when you have this separation of concerns, adding additional functionality is made easier because the code modules are not closely tied together also known as “loose coupling”.

What Does It All Mean?

The first thing is to understand the meaning of what the terms mean.

View: A view is any form or window that is shown to the user of the application.

Presenter: The presenter is an entity that presents the data to the view that is to be shown to the user.

Model: The model is the actual data that the Presenter will request and gets displayed in the View. The Model is responsible for obtaining the data. So the Model is the one that reads files, or connects to a database or if you are really ambitious, gets the data from a data service object.

The pattern is typically drawn like this.

uml1

Looks simple, doesn't it? Well what all those other articles, and examples, didn't tell me was how to turn that simple diagram into a working application.

Get On With It Already!

The first thing to do is to setup the solution. Open up Visual Studio and create a new Windows Forms Application.

newproject1

Once Visual Studio is through creating your new project, add a class library to the solution to hold all of the various classes. Right click your solution object in the Solution Explorer, select Add then Add new project.

addnew1

classlibrary

I named mine Common.Lib.dll. You can name yours whatever you want to. I have seen some people that create a class library to contain the business logic and another one to contain the data layer. I think it is overkill for this simple example. Go ahead and delete the Class1.cs file that Visual Studio created for you.

I like to compartmentalize things so I group like classes together. Right click on the class library project; select Add then Select New Folder. Name the new folder, Interfaces. Repeat twice more and name the other two new folders Models and Presenters. Your project should now look like this:

solutionex

This is the end of Part 1. After starting the article, it quickly grew to 14 pages and I was not done yet so I elected to post a series of pages.

This is the project skeleton so far. Change the extension from .doc to .zip before you open it. This is a workpress requirement.

This article was originally posted at http://wesaday.wordpress.com?p=89

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 1 Pin
boussad.yahia13-Sep-10 8:54
boussad.yahia13-Sep-10 8:54 
GeneralLoved it Pin
EliottA21-May-09 11:26
EliottA21-May-09 11:26 
GeneralRe: Loved it Pin
Wes Aday24-May-09 16:27
professionalWes Aday24-May-09 16:27 
GeneralMy vote of 1 Pin
Jay R. Wren20-May-09 7:12
Jay R. Wren20-May-09 7:12 
GeneralRe: My vote of 1 Pin
Wes Aday24-May-09 16:28
professionalWes Aday24-May-09 16:28 
GeneralGood start Pin
Pat Tormey20-May-09 0:34
Pat Tormey20-May-09 0:34 
I too have struggled trying to understand how to start implementing Model View Presenter. I kinda figure the "why" but without a clear understanding of the process I quickly revert back to my own cobbled version of MVP.
Please ignore the self important comments you've seen and continue the discussion. I'm sure I'm not the only one out here trying to understand the simple model so I can better understand the concept.

Pat Tormey New Hampshire USA
GeneralRe: Good start Pin
Wes Aday24-May-09 16:33
professionalWes Aday24-May-09 16:33 
Generalnext buzzword - MVP Pin
Thornik12-May-09 23:18
Thornik12-May-09 23:18 
GeneralRe: next buzzword - MVP Pin
Wes Aday13-May-09 3:04
professionalWes Aday13-May-09 3:04 
GeneralRe: next buzzword - MVP Pin
wallism19-May-09 14:11
wallism19-May-09 14:11 
GeneralRe: next buzzword - MVP Pin
Pat Tormey20-May-09 0:45
Pat Tormey20-May-09 0:45 
GeneralRe: next buzzword - MVP Pin
Wes Aday24-May-09 16:30
professionalWes Aday24-May-09 16:30 
GeneralRe: next buzzword - MVP Pin
Thornik17-Jun-09 23:54
Thornik17-Jun-09 23:54 
GeneralRe: next buzzword - MVP Pin
Wes Aday18-Jun-09 3:18
professionalWes Aday18-Jun-09 3:18 
GeneralRe: next buzzword - MVP Pin
Thornik17-Jun-09 23:43
Thornik17-Jun-09 23:43 
GeneralRe: next buzzword - MVP [modified] Pin
Wes Aday18-Jun-09 3:27
professionalWes Aday18-Jun-09 3:27 
Generalquestion Pin
Eanna M-annion11-May-09 23:15
Eanna M-annion11-May-09 23:15 
GeneralRe: question Pin
Wes Aday12-May-09 3:12
professionalWes Aday12-May-09 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.