• 应用Autofac使ASP.NET页面获取服务(Componets)


     

      Autofac是应用于.Net平台的依赖注入(DI,Dependency Injection)容器,具有贴近、契合C#语言的特点。随着应用系统的日益庞大与复杂,使用Autofac容器来管理组件之间的关系可以“扁平化”错综复杂的类依赖,具有很好的适应性和便捷度。

      在该篇博文中,我们将应用Autofac,以依赖注入的方式建立传统ASP.NET页面与服务/中间层之间的联系,建立“呈现”与“控制”的纽带。

      那么,如何将依赖注入(Dependency Injection)植入ASP.NET中呢?

      ASP.NET页面生命周期的整个过程均被ASP.NET工作者进程把持,这也就基本上切断了我们想在这个过程中通过“构造函数注入(Constructor injection)”的方式植入服务/中间层的念头,并且要求我们必须在进入页面生命周期之前完成这个植入的过程。基于这种考虑,我们可以采用IhttpModule基础之上“属性注入(Property injection)”方式。以下为实现细节:

    1、 引用相应dll

    ASP.NET应用程序添加以下引用:

    Autofac.dll

    Autofac.Integration.Web.dll

    2、 构造随Application一同启动的ContainerProvider属性

    在进入页面生命周期之前植入,我们选择在Global类中实现。使Global类实现Autofac.Integration.Web命名空间下的IcontainerProviderAccessor接口。该接口公开了一名为ContainerProvider的属性,其为服务的消费者(在此处即为ASP.NET页面)提供根容器。该属性应在应用程序启动的时候构建完成,也就是在Application_Start事件中完成。具体构建代码如下:

    代码
    // 构建应用容器并注册依赖
    var builder = new ContainerBuilder();
    builder.Register
    <EmpMng>().As<IEmpMng>().HttpRequestScoped();
    // ... 注册其他依赖 ... 
    // 注册完成后,便可设置ContainerProvider属性
    _containerProvider = new ContainerProvider(builder.Build());

    3、 设置web.config

    基于IhttpModule的原理,便需在web.config中设置相关的内容。

     

    代码
    <configuration> 
      
    <system.web> 
        
    <httpModules> 
          
    <!--IIS6下设置 --> 
          
    <add 
            
    name="ContainerDisposal" 
            type
    ="Autofac.Integration.Web.ContainerDisposalModule,
                                                   Autofac.Integration.Web"
    /> 
          
    <add 
            
    name="PropertyInjection" 
            type
    ="Autofac.Integration.Web.Forms.PropertyInjectionModule,               
                                                   Autofac.Integration.Web"
    /> 
        
    </httpModules> 
      
    </system.web> 
      
    <system.webServer> 
        
    <!--IIS7 下设置--> 
        
    <modules> 
          
    <add 
            
    name="ContainerDisposal" 
            type
    ="Autofac.Integration.Web.ContainerDisposalModule,  
                                                   Autofac.Integration.Web"
     
            preCondition
    ="managedHandler"/> 
          
    <add 
            
    name="PropertyInjection" 
            type
    ="Autofac.Integration.Web.Forms.PropertyInjectionModule,  
                                                   Autofac.Integration.Web"
     
            preCondition
    ="managedHandler"/> 
        
    </modules> 
      
    </system.webServer> 
    </configuration>

    说明:

    ContainerDisposalModule,页面请求结束,Autofac释放之前创建的组件/服务。

    PropertyInjectionModule,在页面生命周期之前,向页面注入依赖项。

    4、 页面属性获取服务/中间层

    .aspx页面后台放置一公开的属性,用来接收服务/中间层。

    // 用来接收服务/中间层的属性
    public IEmpMng SomeDependency { getset; } 
    protected void Page_Load(object sender, EventArgs e)
    {
        lblEmp.Text 
    = this.SomeDependency.selectOneEmp();
    }

      以上讲解的方式仅仅粗糙的实现了ASP.NET页面与服务/中间层的连通,有很多的细节,如服务/中间层的组织方式、连通处是否可以引入Singleton模式等,均需进一步的研究。本人在IoC领域初来乍到,不足与错误之处望各位斧正!

  • 相关阅读:
    PHP:判断客户端是否使用代理服务器及其匿名级别
    Ruby:Mechanize的使用教程
    Ruby:多线程队列(Queue)下载博客文章到本地
    Ruby:线程实现经典的生产者消费者问题
    Ruby:Open-uri和Net::HTTP的不同
    Ruby:Nokogiri
    Ruby:字符串处理函数
    Ruby:Net::HTTP
    10分钟打造强大的gvim
    命令行批量合并视频脚本
  • 原文地址:https://www.cnblogs.com/isun/p/1827420.html
Copyright © 2020-2023  润新知