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

Presentation Model and Dependency Injection

Rate me:
Please Sign up or sign in to vote.
4.81/5 (10 votes)
26 Apr 2009Ms-PL5 min read 46.6K   434   65  
ASP.NET MVVM provides a framework to implement the Presentation Model pattern, a.k.a. the Model-View-ViewModel pattern in ASP.NET projects. Developers can take advantages of Dependency Injection and Event Broker to write concise, elegant and business focused code.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo.Web.Mvvm
{
    public class PageControllerModule : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>

        #region IHttpModule Members

        public void Dispose()
        {
            //clean-up code here.
        }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            Page page = application.Context.CurrentHandler as Page;
            if (page != null)
            {
                page.PreLoad += (src, args) => BuildController(page);
                page.Unload += (src, args) => UnloadController(page);
            }
        }


        public static void BuildController(Page page)
        {
            HttpContext.Current.Trace.Write("PageControllerModule", "Begin BuildController");
            long dt1 = DateTime.Now.Ticks;
            PageControllerBuilder.BuildController(page);
            TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - dt1);
            HttpContext.Current.Trace.Write("PageControllerModule", "End BuildController " + ts.TotalSeconds);

#if(DEBUG)
            HttpContext.Current.Items["__time_PageControllerModule_BuildController"] = ts.TotalSeconds;
#endif

        }

        public static void UnloadController(Page page)
        {
            HttpContext.Current.Trace.Write("PageControllerModule", "Begin UnloadController");
            PageControllerBuilder.UnloadController(page);
            HttpContext.Current.Trace.Write("PageControllerModule", "End UnloadController");
        }

        #endregion
    }
}

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 Microsoft Public License (Ms-PL)


Written By
Architect
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions