Model View Presenter (MVP) – My first post!

Hello all and welcome to Mr. Rask on the web.

For this my first post i have composed a basic introduction to my favorite design pattern: Model View Presenter or MVP for short. The implementation presented in the article is pretty much exactly how i do it at the office office every day.

Model View Presenter is all about seperating user interface logic from graphical implementation. If you have ever build applications using a graphical library like Windows Form, you know how easily user interface code can get cluttered. MVP to the rescue.

Article (.PDF): Model View Presenter (MVP)
Source (C# 3.0 / VS2008): Crappy filehosting died, so if you want it just drop a comment.

Last but not least any feedback is much appreciated. So pretty please – with sugar on top – drop a comment if you find what you read here usefull (or even if you don’t – i just want to hear from you 🙂 )

All the best
Olav Rask

Tags: , , , , , , , ,

3 Responses to “Model View Presenter (MVP) – My first post!”

  1. Brendan Says:

    Thanks for the example.

    I am wondering how you would deal with a real world example using the MVP. The problem I have found with all samples so far is they show none of the basic complexities of a normal LOB application.

    For Example, what if both fields in the example are required pieces of data to be input. If they are not inputted I want to notify the UI of this with an appropriate error, but to show this error on the UI I don’t want to use an intrusive Message box or a generic label on top or bottom displaying the error. Instead I want to use something like an ErrorProvider. However now I face the problem in that my presenter knows nothing about this provider and should not as its the responsibility of the UI, but the UI has no way of telling which field produced the error so that it can attach the Error Provider to the referenced property.

    Another example I would like to see is how the UI handles the state of it’s own controls. For example. maybe I don’t want the button enabled until both fields have been entered. Think of a username/password scenario. How would this work with respect to the MVP pattern. Would it be up to the UI to check the state of this or would the presenter export a setter for a property like IsValid and then this reflects the enabled state of a button.

    These are just some of the basic but required things I’d like to see presented by the MVP pattern instead of a the simple textbox presentation scenario.

  2. OlavRask Says:

    I don’t have much experience with ErrorProviders, but i will explain what i would do in the case of the two textboxes and the button:

    Handling state is exactly the kind of functionality that should be moved away from the concrete view and into the presenter, where it can be properly (unit) tested and reused.

    Expose the TextChanged events of the two textboxes and the enabled property of the button in the view inteface.

    Presenter code for eather eventhandler would look something like this:

    private void view_UsernameTextChanged(object sender, EventArgs e)
    if ( this.view.Username != “” && this.view.Password != “” )
    this.view.OkEnabled = true;
    this.view.OkEnabled = false;

    You will now be able to test this functionality programatically by supplying the presenter with a mock view that fires the events and reports the resulting value of OkEnabled.

    Regards Olav

  3. Quan Says:

    Thanks a lot for the awesome MVP article.

    I am just very curious about your Final Thought in the article. How can you avoid wiring model, presenter, view with injection? And what are your helpful classes? I just cant wait for your next post about MVP ^_^

    Very appreciate if you drop me a mail with your source code.

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: