2012-07-18 104 views
2

接口攔截和構造函數有什麼區別注入 asp.net 統一? 我缺乏與統一 我的老項目和unity.config工作,瞭解DI的一些類似這樣的,Microsoft Unity接口攔截和構造函數的區別注入

<register type="ICRIC2012TrialUserService" mapTo="CRIC2012TrialUserService"> 
     <lifetime type="perthread"/> 
     <interceptor type="InterfaceInterceptor"/> 
     <policyInjection/> 
</register> 

但一些不具備

<interceptor type="InterfaceInterceptor"/> 

但在這些項目以同樣的方法類和接口的工作,這樣

private readonly IIncomingEstimateService _incomingEstimateService; 

     public ContractService(IIncomingEstimateService incomingEstimateService) 
     { 
      _incomingEstimateService = incomingEstimateService; 
     } 

回答

4

攔截和注射是兩回事。

攔截通常用於將交叉關注(如日誌記錄)添加到類,而無需一次又一次地實施它們。在Unity中的截取通過生成接收傳入方法調用並通過所謂的攔截器管道傳遞它們的代理來工作,直到調用最終到達原始目標對象。

這兩篇文章介紹瞭如何使用Unity進行截取。

Interceptors in Unity

Using Interception with Unity

構造方法注入是實現依賴注入模式的一種方式。這是一種模式,可以告訴您如何構建應用程序以分離組件。

網上有很多文章。

Inversion of Control Containers and the Dependency Injection pattern

The Dependency Injection Pattern – What is it and why do I care?

攔截和注射是完全不同的東西。但是一些依賴注入容器(如Unity)允許您同時執行這兩個操作。您可以將依賴關係注入到類中,並使用容器基礎結構攔截對這些類的調用。

+0

謝謝,這很有幫助。 – Bubble 2012-07-19 03:22:28