Christopher Bird

Professional and Personal Blog

Month
Aug
Day
31

NConfigure

Over the past few years i have become increasingly irritated by the availability of a configuration tool that would allow me to maintain all aspects of my .NET configuration files across development, integration, staging and multiple live environments.

So as a side project i have started work on creating a tool that will accomplish this. At the moment it will be driven from XML source files, have a command line tool, (hopefully a NANT task) and a GUI. The idea being that changes can be committed to any source control system and as part of an automated build process, configuration can be generated for all or just a target environment.

I’m attempting to release a prototype of this tool in the next few weeks so i would be interested to hear if anyone has found such a tool or if they have any ideas on how such a tool should work.


Month
Apr
Day
10

Thoughts on MVP in ASP.NET.

Recently i have been working with colleagues on implementing model view presenter into the ASP.NET platform. This is some of my thoughts separate from the constraints of the company that i work for. The major influence for this article is martin fowler’s articles on model view presenter where he talks about a pattern he has coined called Passive View. One of the main obstacles faced by any web application developers is the task of testing their interface. There are many tools out there such as WatIN and Selenium which can do UI testing but i would like to be able to unit test as much as possible and leave those other tools for simple UI checks. I will concede now there are many MVC frameworks available for .NET that can accomplish this, such as Microsoft MVC, Castle, etc. But i have found this pattern quite handy to use with existing applications where MVC is just to much of a radical change.

The passive view pattern attempts to move as much of the logic you would normally find in your aspx code behinds into a separate testable locations. The only problem that myself and many of my colleagues have found is coming to a common agreement on how it should be implemented, so here is my idea. Based on the pattern what i am going to do is provide a view that has only two concerns, one is to pass any user events onto the presenter, but also to provide an interface by which the view can be inspected and read. The presenter will deal with accepting those user events, performing some presentation logic and finally update the view. The model will encapsulate the data and business rules (See the Domain Model) which the presenter will inspect.

I’ve tried to come up with an example for demonstrating this implementation that includes enough interaction to demonstrate it working in ASP.NET. The example i have chosen is that a user is able to select a band from a list of bands and then add it to there favourites. To make it a little bit more complicated i have included a business rule that the user cannot have more than five favourite bands.

In this sequence diagram i have tried to show the users actions being passed onto the presenter and the subsequent model/view inspection and update that is triggered by that event.

In the following class diagram I’ve started to represent the implementation of the above sequence diagram. You may notice at this point that i have created interfaces for both the view and the presenter. I have created the interface for the view for a few reason, firstly it represents a set of actions that a view must implement to work with the presenter. This also means that we should be able to create multiple views for one presenter and the presenter is not dependant on the views implementation (this is quite useful for representing the same data in multiple ways e.g. different types of xml feed on a blog). I have created the presenters interface for a similar reasons, to the view it provides an selection of events that presenter can deal with and also means that we should be able to replace the presenter independent of the view.

Although its quite common to use inversion of control with this pattern i wont cover this at the moment, perhaps in a later post. When i came to start relating this into ASP.NET my first instinct was to say that Page = View. I’m not so sure that is the case, I’m now more inclined to say that View = UserControl, e.g. a collection of functionality grouped in a reusable way. So for my example i am going to create a user control that provides my example functionality. This does have the benefit of reusability, i am now able to use this UI component on multiple pages with ease. Now lets finally move onto the code.

View ASCX

< %@ Control Language="C#" AutoEventWireup="true" CodeFile="FavouriteBandControl.ascx.cs" Inherits="FavouriteBandControl" %>
<asp :Repeater ID="Repeater1" runat="server"></asp>
<asp :DropDownList ID="DropDownList1" runat="server"></asp>
<asp :Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

View Code Behind

public partial class FavouriteBandControl : System.Web.UI.UserControl, IFavouriteBandView
{
    private IFavouriteBandPresenter _presnter;
 
    protected override void OnInit(EventArgs e)
    {
        _presnter = new FavouriteBandPresenter(this);
        base.OnInit(e);
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        _presnter.Load();
    }
 
    public System.Collections.Generic.List<string> Bands
    {
        set
        {
            DropDownList1.DataSource = value;
            DropDownList1.DataBind();
        }
    }
 
    public System.Collections.Generic.List</string><string> FavouriteBands
    {
        set
        {
            Repeater1.DataSource = value;
            Repeater1.DataBind();
        }
    }
 
    public string SelectedBand
    {
        get
        {
            return DropDownList1.SelectedValue;
        }
        set
        {
            DropDownList1.SelectedValue = value;
        }
    }
 
    public bool FavouriteBandSelectionEnabled
    {
        set
        {
            DropDownList1.Visible = false;
            Button1.Visible = false;
        }
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        _presnter.AddFavouriteBand();
    }
}
</string>

View Interface.
you’ll notice here i have also added IsPostBack to the interface. I have had to do this so that i was able to deal with user event easily.

public interface IFavouriteBandView
{
    List<string> Bands { set; }
    List</string><string> FavouriteBands { set; }
    string SelectedBand { get; set; }
    bool FavouriteBandSelectionEnabled { set; }
    bool IsPostBack { get; }
}
</string>

Presentation

public class FavouriteBandPresenter: IFavouriteBandPresenter
{
    IFavouriteBandView _view;
    BandModel _band;
 
	public FavouriteBandPresenter(IFavouriteBandView view)
	{
        _view = view;
        _band = new BandModel();
	}
 
    public void Load()
    {
        if(_view.IsPostBack)
        {
                loadView();
        }
    }
 
    public void AddFavouriteBand()
    {
        _band.FavouriteBands.Add(_view.SelectedBand);
        loadView();
    }
 
    private void loadView()
    {
        _band.FavouriteBands.Add(_view.SelectedBand);
        _view.FavouriteBands = _band.FavouriteBands;
    }
}

Presentation Interface

public interface IFavouriteBandPresenter
{
    void Load();
    void AddFavouriteBand();
}

I hope this goes some way to showing how to implement model view presenter in ASP.NET.


Month
Mar
Day
27

Under Construction!

Welcome to my blog, i’m currently in the process of deploying, tweaking and fixing. The site should be up soon.