Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
 
Add your own
alternative version

Presentation Model and Dependency Injection

, 26 Apr 2009
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)

About the Author

Yiyi Sun
Architect
Canada Canada
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 26 Apr 2009
Article Copyright 2009 by Yiyi Sun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid