Wednesday 17 September 2008

Navigation within MVP

I've been doing a lot of research into various frameworks so I can make an informed decision on my new application I'm developing. In evaluating MVP, ASP.NET MVC and the traditional three tiered approach I came across a question that I found it difficult to find informaton on. How should navigation be handled in a MVP ASP.NET application and who should handle it? Well the answer to the first question was easy, any way you like. You could use Spring.NET libraries, PageMethods, write your own, Response.Redirect, it doesn't matter and MVP doesn't prescribe any of the above. But the question that was more important is who should handle it? I found a great article on CodeProject on Model View Presenter implementation that was very useful. The article points out that a navigation raised through the presenter should be raised in the View via an event. So in a simple example if you have a login form your presenter might look like this:
public class LoginController
{
    // The view interface for manipulating the user interface.
    ILoginView view;

    public LoginController(ILoginView view)
    {
        this.view = view;
        SubscriveToEvents();
    }

    private void SubscriveToEvents()
    {
        view.OnLogin += OnLogin;
    }

    public void OnLogin(object sender, EventArgs e)
    {
        if (Login(view.userName, view.password))
        {
            view.LoginSuccessful();
        }
    }
}
And in your view you will simply implement the login successful method (don't forget to add it to the interface). This means that the business rules for navigation are actully sitting in your view layer, however if you use a navigation framework they will be well encapsulated and simple to read and understand. I have not done any investigation into which framework would be appropriate for my project, if we choose to go with MVP then this will have to be done and I'll blog that too, but in the mean time have a look at the Spring.NET libraries and PageMethods.

No comments: