文章出處
View Code
文章列表
之前說過有關攔截器的文章,第二回 緩存攔截器,事實上,在那講里說的最多是AOP和緩存組件,對于攔截的概念并沒有詳細的說明,這一講,不說AOP,主要說一下攔截器,攔截器Interception,主要是在方法執行前或者執行后,動態添加一些行為,而這個行為主要包含緩存,日志,異常處理及你可以想到的所有的一切,呵呵。
這一講是異常攔截器,它的主要意義在于,當你的一個方法被執行時,你可以通過配置文件去管理這個方法執行前與執行后是否附加統一異常處理行為。
攔截器組件我們還是用Unity.InterceptionExtension,它依附于Unity,當你沒有安裝Unity時,Unity.InterceptionExtension在安裝時會自己幫你添加上,這是正確的,呵呵。
對于我們所開發的攔截器,必須要實現IInterceptionBehavior這個接口才可以,這是接口的內容

// 摘要: // Interception behaviors implement this interface and are called for each invocation // of the pipelines that they're included in. public interface IInterceptionBehavior { // 摘要: // Returns a flag indicating if this behavior will actually do anything when // invoked. // // 備注: // This is used to optimize interception. If the behaviors won't actually do // anything (for example, PIAB where no policies match) then the interception // mechanism can be skipped completely. bool WillExecute { get; } // 摘要: // Returns the interfaces required by the behavior for the objects it intercepts. // // 返回結果: // The required interfaces. IEnumerable<Type> GetRequiredInterfaces(); // // 摘要: // Implement this method to execute your behavior processing. // // 參數: // input: // Inputs to the current call to the target. // // getNext: // Delegate to execute to get the next delegate in the behavior chain. // // 返回結果: // Return value from the target. IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext); }
其中Invoke方法就是攔截方法之前要執行的方法,當進行目標方法時,首先會檢測是否在此方法的信息,如果有,就進行攔截行為,本例為異常攔截行為。
異常攔截器代碼:
/// <summary> /// 表示用于異常日志記錄的攔截行為。 /// </summary> public class ExceptionLoggingBehavior : IInterceptionBehavior { #region IInterceptionBehavior Members /// <summary> /// 獲取當前行為需要攔截的對象類型接口。 /// </summary> /// <returns>所有需要攔截的對象類型接口。</returns> public IEnumerable<Type> GetRequiredInterfaces() { return Type.EmptyTypes; } /// <summary> /// 通過實現此方法來攔截調用并執行所需的攔截行為。 /// </summary> /// <param name="input">調用攔截目標時的輸入信息。</param> /// <param name="getNext">通過行為鏈來獲取下一個攔截行為的委托。</param> /// <returns>從攔截目標獲得的返回信息。</returns> public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) { var methodReturn = getNext().Invoke(input, getNext); if (methodReturn.Exception != null) { Utils.Log(methodReturn.Exception); } return methodReturn; } /// <summary> /// 獲取一個<see cref="Boolean"/>值,該值表示當前攔截行為被調用時,是否真的需要執行 /// 某些操作。 /// </summary> public bool WillExecute { get { return true; } } #endregion }
而要想使攔截器起作用,我們可以在配置文件中進行配置,下面是緩存攔截與異常攔截的配置代碼,它們隸屬于Unity節點
<!--BEGIN: Unity--> <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" /> <container> <extension type="Interception" /> <register type="Infrastructure.Caching.ICacheProvider, DDD_AOP_WCF.Infrastructure" mapTo="Infrastructure.Caching.EntLibCacheProvider, DDD_AOP_WCF.Infrastructure" /> <!--對數據上下文的注入--> <register type="DDD_AOP_WCF.Domain.Repository.IProductRepository, DDD_AOP_WCF.Domain" mapTo="DDD_AOP_WCF.Infrastructure.Repository.ProductRepository, DDD_AOP_WCF.Infrastructure" /> <!--對WCF的訪問進行的注入與緩存和異常的攔截--> <register type="DDD_AOP_WCF.ServiceContracts.IProductService, DDD_AOP_WCF.ServiceContracts" mapTo="DDD_AOP_WCF.Service.Implements.ProductServiceImpl, DDD_AOP_WCF.Service"> <!-- <interceptor type="VirtualMethodInterceptor" />--> <interceptor type="InterfaceInterceptor" /> <interceptionBehavior type="Infrastructure.InterceptionBehaviors.CachingBehavior, DDD_AOP_WCF.Infrastructure" /> <interceptionBehavior type="Infrastructure.InterceptionBehaviors.ExceptionLoggingBehavior, DDD_AOP_WCF.Infrastructure" /> </register> </container> </unity> <!--END: Unity-->
怎么樣,大家是否對攔截器有一個很清楚的了解和認識了呢,呵呵!
文章列表
全站熱搜