On.. MVP Implementation and Project Template

I’ve been implementing Model View Presenter as a pattern for the best part of the last year in a range of projects. I think I’ve got a good handle on working with it, at least all my implementations work and are easy to maintain. The way I’ve implemented it has evolved over the past year through reading more, putting it into practice and discussions with collegues.

A Quick Intro to MVP
Model View Presenter (MVP) is an attractive way of working because of the level of layer abstraction it exhibits and the testability of the layers, particularly useful if you are an ASP.NET developer. I like the clear separation of each layer and the fact that one need not know how the other layer is implementing the interface. More to the point, it doesn’t care.

The best diagram I’ve found of MVP was this one on Flickr. It also compares it with the Model View Controller pattern. I didn’t like the circular nature of communication with MVC, one of the advantages of MVP to me is that as you scale the system you can completely rewrite any one section of it without breaking it. For example you can move everything from the Model backwards (towards the database) into a web service, or into WCF. Alternatively your designers now want the front end to be Silverlight, so you modify your View implementation to be Silverlight, without touching another part of your code. More to the point if you did both of these things the only part of your application that would have knowledge of that would be the layer adjacent to it.

On to my implementation.

So here it is, The view of my solution explorer when I open up my new VB.NET MVP Web Application Template (download Here and follow this entry through).

MVP Solution once opened

Like all these templates its probably better to go through and rename everything as you wish, it takes a little longer in an MVP projects. On which note I’ll just add that working with interfaces and carrying function names all the way down through the layers can result in some slightly longer refactoring times, but its not that bad.

So the first problems I came across when implementing MVP was how to hook everything together. The Template does all that. I now work on the basis that the View only knows what Presenter it is using, the Presenter knows the View (because it gets passed to it) and the Model. The Model will know the Data Access Layer and other Helper classes.

Right lets create a Hello World web app with this MVP template. I’m going to get the String from the Model so you can see it passing through the system.

My interface for the View will have a property of Message As String, the interface for the Model will contain Function GetMessage() As String.

I drop a label (Label1) onto the Deafult.aspx which will be implementing IView1, I then add the following code to the Default.aspx.vb. :

    Public Property Message() As String Implements IView1.Message
        Get
            Return Label1.Text
        End Get
        Set(ByVal value As String)
            Label1.Text = value
        End Set
    End Property

One day I’ll work out how to get binding oriented programming working and then I’ll make Paul Stovell happy.

Next I’ll do Model1, to which I add

    Public Function GetMessage() As String Implements IModel1.GetMessage
        Return "Hello World"
    End Function

Now the next bit is where the magic happens baby. In the Presenter I add the following:

    Public Sub GetMessage()
        m_View.Message = m_Model.GetMessage
    End Sub

Now all that remains is to go back into our View/Default.aspx and tell it to use the presenter to get the message. I’ll do this in the page load event, but we could hook it up to a button click or whatever if we wished.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        m_Presenter.GetMessage()
    End Sub

Lets hope thats working for you, because it doesn’t get simpler than that. If you put a breakpoint on the m_Presenter.GetMessage() line you can step through the code and see how the layers are separated. In full applications I like to keep the Presenter layer for abstract and business logic, whilst the Model should be dealing with data and how its getting it. The Presenter should have no knowledge of where the actual data is coming from or how it is being transformed before it gets to it, it knows it wants a String, or whatever it is your function requires be returned, as long as it gets it, it is happy.

Lets do something silly. Lets add a Helper class that will return our string instead. Functionally this is the same as moving your datalayer from being SQL to Oracle or Access or Excel or a Web Service. I’m going to add a class called MessageFetcher.vb to the Helper folder. The entire class looks like this:

Public Class MessageFetcher
    Public Function GetsMeAMessage() As String
        Return "Hello World, from further away"
    End Function
End Class

I now change Model1 GetMessage() Function to read thusly :

    Public Function GetMessage() As String Implements IModel1.GetMessage
        Dim myFetcher As New MessageFetcher
        Return myFetcher.GetsMeAMessage
    End Function

Now it may seem trival, but that is a whole massive change when you are talking about large applications, a massive change that will only affect the Model layer. I could go further, I could fetch the message from a database. I could start passing down through the subs and functions values to tell it which message I wanted from the database. I could ship the whole model layer out to be a web service that fetched messages depending on which ID was sent to it and the View and most of the Presenter could would never have to be altered. would never have to be altered.

You can download the Template Here to get started with this stuff. Download it and follow through the entry and see it working, even if you don’t use it in the future at least you get a handle on what people are talking about.

If people want I can do a WinForms one and a C# one too. If they think they will find it useful. Just drop me a comment. Also any questions drop them in the comments.

About these ads

3 Responses to “On.. MVP Implementation and Project Template”


  1. 1 johnmazarr April 6, 2008 at 2:30 am

    A Realistic $250K First Year Income Potential

    Less than four years ago I was driving this beat-up ’94 VW with a rusty muffler. Within two years of creating this system I was making more than my doctor, accountant, and attorney COMBINED… while working less than 40 hours a week FROM HOME! Together with my group of leaders we are now seeking qualified entrepeneurs sharein the incredible results of our system.

    for more information check this site out….
    make money online scams

  2. 2 Earle March 10, 2014 at 2:08 am

    Hey there! I’m at work surfing around your blog from my new iphone 3gs!

    Just wanted to say I love reading your blog and look forward to all your posts!
    Carry on the excellent work!


  1. 1 On.. Model View Presenter « BuzzSort Trackback on November 19, 2007 at 5:24 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





Follow

Get every new post delivered to your Inbox.

%d bloggers like this: