Model View Presenter: Control Model

So now we have defined our view, let’s go and work with it. Let’s start with examining the interface of a control, for example the ITextBox:

At the end of this article you can download the sample project and see all the action for yourself
public interface ITextBox : IControl
{
    string Text { get; set; }
    bool IsPassword { get; set; }
    bool Visible { get; set; } // Defined in IControl
    string ID { get; set; }    // Defined in IControl
}

Here you can see what a TextBox needs to have to be an ITextBox. For easy reading I have included the properties from IControl in this snippet. All this properties can be accessed in the presenter. We can set in the presenter which Textbox is a password and which Texbox is visible. We do not have to define a rule in the interface of the view for this.

Now let’s take a look at the implementation of the ITextBox for the ASP.NET TextBox.

public class MVPTextBox : TextBox, ITextBox
{
    public bool IsPassword
    {
        get { return this.TextMode == TextBoxMode.Password; }
        set { this.TextMode = value ? TextBoxMode.Password : TextBoxMode.SingleLine; }
    }
}

This is all the code we need. We create our own control called MVPTextBox which inherits from the normal ASP.NET TextBox. Because the TextBox already has the Id, Text and Visible properties we don’t have to define them here. The IsPassword property is not standard and has to be defined.

This is how it is used in your application in the markup:

<%@ Register Assembly="ControlModel.Framework" Namespace="JYAG.ControlModel.Framework.Controls.Web" TagPrefix="jyag" %>

and here is the codebehind:

public ITextBox UsernameTextBox
{
    get { return usernameTextBox; }
}

That’s all there is to it.

This whole concept stands and falls with the implementation of a good Framework to provide the controls. Think thoroughly what you need when you create the framework. If you have a good framework with all the controls you need you can enjoy the MVP model without ever having to worry about the interface and how to hookup the implementation to the contract. The provided codesample is not even close to complete. There are just a few controls and I don’t even use custom events and custom eventhandlers. But I think this will give you a nice first impression of my thoughts on this pattern.

So to understand exactly what’s going on download the sample project and examine the code.

If you have any questions just contact me.

  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.