微软企业库5.0学习笔记(10)ASP.NET模块依赖注入_转载

您可以使用HTTP模块,一个到ASP.NET HttpApplicationState类的扩展,在Global.asax编写代码强制ASP.NET在每一个页面请求时自动注入依赖的对象,就像在ASP.NET Web窗体应用程序中讨论的一样.
 
    下列方法显示了一个合适的方法能够获取PreRequestHandlerExecute事件将它自己注入到ASP.NET的执行流水线,在每个页面请求中通过容器的BuildUp方法运行Http模块,并获取OnPageInitComplete事件。当OnPageInitComplete执行时模块代码按照所有的控件树运行,并通过容器的BuildUp方法处理每个控件。
 
    BuildUp方法获取已经存在的对象实例,处理并填充类的依赖,返回实例。如果没有依赖则返回最初的实例。
      view plaincopy to clipboardprint?01.using System;  02.using System.Collections.Generic;  03.using System.Web;  04.using System.Web.UI;  05.using Microsoft.Practices.Unity;  06.  07.namespace Unity.Web  08.{  09.  public class UnityHttpModule : IHttpModule  10.  {  11.    public void Init(HttpApplication context)  12.    {  13.      context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;  14.    }  15.  16.    public void Dispose() { }  17.  18.    private void OnPreRequestHandlerExecute(object sender, EventArgs e)  19.    {  20.      IHttpHandler currentHandler = HttpContext.Current.Handler;  21.      HttpContext.Current.Application.GetContainer().BuildUp(  22.                          currentHandler.GetType(), currentHandler);  23.  24.      // User Controls are ready to be built up after page initialization is complete  25.      var currentPage = HttpContext.Current.Handler as Page;  26.      if (currentPage != null)  27.      {  28.        currentPage.InitComplete += OnPageInitComplete;  29.      }  30.    }  31.  32.    // Build up each control in the page's control tree  33.    private void OnPageInitComplete(object sender, EventArgs e)  34.    {  35.      var currentPage = (Page)sender;  36.      IUnityContainer container = HttpContext.Current.Application.GetContainer();  37.      foreach (Control c in GetControlTree(currentPage))  38.      {  39.        container.BuildUp(c.GetType(), c);  40.      }  41.      context.PreRequestHandlerExecute -= OnPreRequestHandlerExecute;  42.    }  43.  44.    // Get the controls in the page's control tree excluding the page itself  45.    private IEnumerable<Control> GetControlTree(Control root)  46.    {  47.      foreach (Control child in root.Controls)  48.      {  49.        yield return child;  50.        foreach (Control c in GetControlTree(child))  51.        {  52.          yield return c;  53.        }  54.      }  55.    }  56.  }  57.}        下面显示了一个应用程序状态的实现,并暴露一个静态的 GetContainer方法,这个方法能够在Application状态中创建一个新的统一容器,如果不存在的话,或者返回一个存在的实例的引用。   view plaincopy to clipboardprint?01.using System.Web;  02.using Microsoft.Practices.Unity;  03.  04.namespace Unity.Web  05.{  06.  public static class HttpApplicationStateExtensions  07.  {  08.    private const string GlobalContainerKey = "EntLibContainer";  09.  10.    public static IUnityContainer GetContainer(this HttpApplicationState appState)  11.    {  12.      appState.Lock();  13.      try  14.      {  15.        var myContainer = appState[GlobalContainerKey] as IUnityContainer;  16.        if (myContainer == null)  17.        {  18.          myContainer = new UnityContainer();  19.          appState[GlobalContainerKey] = myContainer;  20.        }  21.        return myContainer;  22.      }  23.      finally  24.      {  25.          appState.UnLock();  26.      }  27.    }  28.  }  29.}  

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sfbirp/archive/2010/05/24/5621272.aspx

转载于:https://www.cnblogs.com/ZhangHuaning/archive/2011/03/17/1986769.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值