之前说过有关拦截器的文章,第二回 缓存拦截器,事实上,在那讲里说的最多是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); }