Click here to Skip to main content
15,892,537 members
Articles / Web Development / ASP.NET

Advancing the Model-View-Presenter Pattern - Fixing the Common Problems

Rate me:
Please Sign up or sign in to vote.
4.53/5 (28 votes)
6 Sep 2007CPOL7 min read 187.3K   611   105  
Addressing the common issues related to the MVP pattern using ASP.NET and WinForms clients.
using System;

namespace Sample.Presentation
{
    /// <summary>
    /// Part part facade, part factory - this class is used to return 
    /// a presenter based on the type of view
    /// </summary>
    public static class PresentationManager
    {
        /// <summary>
        /// Creates and return a presenter based on the type of interface the calling view 
        /// implements. 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="presenterType">Type of the presenter.</param>
        /// <param name="view">The view.</param>
        /// <returns></returns>
        public static T RegisterView<T>(Type presenterType, IView view) where T : Presenter
        {
            return RegisterView<T>(presenterType, view, null);
        }
        /// <summary>
        /// Creates and return a presenter based on the type of interface the calling view 
        /// implements.  
        /// </summary>
        /// <param name="presenterType">Type of the presenter.</param>
        /// <param name="view">Instance of the view</param>
        /// <param name="session">Object implementing ISessionProvider, or null if no state is required</param>
        public static T RegisterView<T>(Type presenterType, IView view, ISessionProvider session) where T : Presenter
        {
            return LoadPresenter(presenterType, view, session) as T;
        }
        /// <summary>
        /// Allows for registration where the presenter type isn't known at compile time.  This method pair also
        /// does not return a presenter to the caller
        /// </summary>
        /// <param name="presenterType">Type of presenter</param>
        /// <param name="view">Instance of the view</param>
        public static void RegisterView(Type presenterType, IView view)
        {
            RegisterView(presenterType, view, null);
        }
        /// <summary>
        /// Allows for registration where the presenter type isn't known at compile time.  This method pair also
        /// does not return a presenter to the caller
        /// </summary>
        /// <param name="presenterType">Type of presenter</param>
        /// <param name="view">Instance of the view</param>
        /// <param name="session">An object implementing ISessionStateProvicer</param>
        public static void RegisterView(Type presenterType, IView view, ISessionProvider session)
        {
            LoadPresenter(presenterType, view, session);
        }
        /// <summary>
        /// Loads the presenter.
        /// </summary>
        /// <param name="presenterType">Type of the presenter.</param>
        /// <param name="view">The view.</param>
        /// <param name="session">The session.</param>
        /// <returns></returns>
        private static object LoadPresenter(Type presenterType, IView view, ISessionProvider session)
        {
            int arraySize = session == null ? 1 : 2;

            object[] constructerParams = new object[arraySize];
            constructerParams[0] = view;

            // Add the session as a parameter if it's not null
            if (arraySize.Equals(2))
            {
                constructerParams[1] = session;
            }

            return Activator.CreateInstance(presenterType, constructerParams); ;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!

http://www.pagelabs.com

Comments and Discussions