應用Autofac使ASP.NET頁面擷取服務(Componets)

來源:互聯網
上載者:User
 

  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 { get; set; } 
protected void Page_Load(object sender, EventArgs e)
{
    lblEmp.Text = this.SomeDependency.selectOneEmp();
}

 

 

  以上講解的方式僅僅粗糙的實現了ASP.NET頁面與服務/中介層的連通,有很多的細節,如服務/中介層的組織方式、連通處是否可以引入Singleton模式等,均需進一步的研究。本人在IoC領域初來乍到,不足與錯誤之處望各位斧正!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.