Model View Presenter: Control Model

The loginform

Consider a straightforward form to let a user log in to an application by providing his username and password. Let me sketch it with balsamiq for you:

Note that I have sketched the form as an webpage. This could have been a WinForm just as well. That’s why we use MVP, so we do not bind ourselves to a specific interface. Call it force of habit for me as a WebDeveloper to sketch webpages.

The form has a Textbox for the username as well as for the password. It also gives the user the ability to let the system remember the credentials by checking a ‘Remember me’ checkbox. There is a button to start the process and a label to show messages and errors.

In classic MVP the view would look like this:

public interface ILoginView : IView
{
    string Username { get; }
    string Password { get; }
    bool RememberUser { get; set; }
    string ErrorMessage { set; }
    bool ErrorMessageVisible { set; }
}

In this interface you can see that we need to get the given username and password. We can set and get the Boolean stating to remember the credentials. If the process fails we could set an error message.  If the process completed successfully whe shoud hide the error message.
Here you can see another downfall of this approach. Although the error message is just a simple string we have some properties we would like to set as well in our presenter. In this case it is the visibility but for a checkbox it could be for example the enabled property as well. Each property needs to be defined in the interface of the view for the Presenter to be able to set or get its value.

Let’s take a look at the views interface when we use the Passive View Control Model:

public interface ILoginView : IView
{
    ITextBox UsernameTextBox { get; }
    ITextBox PasswordTextBox { get; }
    ICheckBox RememberUserCheckBox { get; }
    ILabel ErrorMessageLabel { get; }
    IButton LogInButton { get; }
}

This interface specifies the controls we want on the form. We specify the contract of the controls so we do not bind ourselves to a WinForm or ASP.NET controls.
The contract of an ILabel specifies that it needs a Text property as well as a Visible flag. We do not have to make a different rule in the interface of the view for each property.

On the next page I will show you how to use this code.

  1. Thank you for sharing your thoughts!

    I am definitely looking forward to the next posts about MVP and how you plan to solve the issues concerning internationalization of labels and error messages. I also would like to see a sample of how you plan to use a grid, or am I too greedy now (pun intended) ;-)

    Greetings.

  1. No trackbacks yet.