Click here to Skip to main content
13,257,667 members (67,933 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads


65 bookmarked
Posted 17 Mar 2009

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:
        /// </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;
            TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - dt1);
            HttpContext.Current.Trace.Write("PageControllerModule", "End BuildController " + ts.TotalSeconds);

            HttpContext.Current.Items["__time_PageControllerModule_BuildController"] = ts.TotalSeconds;


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


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.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Yiyi Sun
Canada Canada
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 26 Apr 2009
Article Copyright 2009 by Yiyi Sun
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid