AOP之拦截函数调用链实现

定义函数A,B,C,调用A->B->C,这样就形成了函数静态调用链,而AOP要做的是能动态的添加多个B,形成A->B1->B2->B3...->C这样的效果,在EntLib(MS的企业库)Unity中有这样的实现,不过要看明白里面的代码的确需要花不少脑子,3年前看过里面的代码并做了记录,但是这两天翻出来看时照样化了很大精力,并杀死杀伤大量脑细胞,于是痛下决心将整个过程整理并画出时序图。

测试代码:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
BLLObj bll = new BLLObj();
InterceptionUtil util = new InterceptionUtil();
util.Pipeline.InterceptionBehaviors.Add(new BehaviorA() { Id = "1" });
util.Pipeline.InterceptionBehaviors.Add(new BehaviorA() { Id = "2" });
util.Pipeline.InterceptionBehaviors.Add(new BehaviorA() { Id = "3" }); var r = util.Call(bll, "Add", new object[] { 3, 4 });
Console.WriteLine("结果:" + r); Console.WriteLine(new string('-', 40)); var r1 = util.Call(bll, "Concat", new object[]{ new object[] { "a",1, "b", 2 }});
Console.WriteLine("结果:" + r1); Console.WriteLine(new string('-', 40)); var r2 = util.Call(bll, "DoSome", new object[] { });
Console.WriteLine("结果:" + r2);
}
} #region 测试业务类 public class BLLObj
{
public int Add(int a, int b)
{
Console.WriteLine("Add");
return a + b;
}
public string Concat(object[] args)
{
Console.WriteLine("Concat");
return string.Concat(args);
}
public void DoSome()
{
Console.WriteLine("DoSome");
} }
#endregion

(图1)函数链调用的效果图:

从图1可以看到,在目标对象的目标方法被调用前包裹了3层拦截类的函数调用,正是这样嵌套结构允许我们实现数据库事务控制,错误记录,权限控制,性能监测等一系列AOP应用.

实现代码

下面的代码涉及2个接口与5个类,话说人脑在同时考虑多个对象时会很受伤,所以俺在代码里加了必要的注释并在下面付上了调用序列图,请配合起来看。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection; namespace FuncLinkT2
{ //两个互相引用的接口,从定义的这一刻开始
//就注定了他们需要互相依靠,相伴一身.......
//当然哥们的纠结也从此开始
public interface IInterceptionBehavior
{
MethodReturn Invoke(MethodInvocation input, IGetNextInterceptionBehavior nb);
}
public interface IGetNextInterceptionBehavior
{
IInterceptionBehavior GetNext();
} /// <summary>
/// 作为IInterceptionBehavior.Invoke参数,
/// 便于拦截行为类获取当前拦截方法的信息
/// 包括被拦截对象与被拦截方法
/// </summary>
public class MethodInvocation
{
public MethodBase MethodBase { get; set; }
public object Target { get; set; } }
/// <summary>
/// 被拦截方法的返回结果包装
/// 同样便于拦截行为类获取方法调用后接返回结果
/// </summary>
public class MethodReturn
{
public object ReturnValue { get; set; }
public Exception Err { get; set; } } #region 一个拦截行为类的实现
public class BehaviorA : IInterceptionBehavior
{
public string Id { get; set; } public MethodReturn Invoke(MethodInvocation input, IGetNextInterceptionBehavior nb)
{ Console.WriteLine("Behavior:" + Id + "->Invoke Befor!"); IInterceptionBehavior behavior = nb.GetNext(); var r = behavior.Invoke(input, nb); Console.WriteLine("Behavior:" + Id + "-> invoke After!"); return r; }
}
#endregion /// <summary>
/// 实际对象调用代理类
/// 包装成IInterceptionBehavior以便加到BehaviorPipeline末尾
/// </summary>
public sealed class InterceptionInvocationProxy : IInterceptionBehavior
{
public object[] Args;
public MethodInvocation Invocation;
public object Target; public MethodReturn Invoke(MethodInvocation input, IGetNextInterceptionBehavior nb)
{
Console.WriteLine("InterceptionInvocationProxy->实际对象将被调用!");
var ret= new MethodReturn();
try
{
ret.ReturnValue = Invocation.MethodBase.Invoke(Target, Args);
}
catch (Exception ex)
{
ret.Err = ex; }
return ret; }
} public sealed class NextBehaviorsImp : IGetNextInterceptionBehavior
{
internal InterceptionBehaviorPipeline Pipeline;
internal int InterceptorIndex;
internal IInterceptionBehavior Target; public IInterceptionBehavior GetNext()
{
InterceptorIndex++;
if (InterceptorIndex < Pipeline.InterceptionBehaviors.Count)
{
IInterceptionBehavior local1 = Pipeline.InterceptionBehaviors[InterceptorIndex];
return local1;
}
return Target;
}
}
/// <summary>
/// 拦截行为链条
/// </summary>
public class InterceptionBehaviorPipeline
{
internal readonly List<IInterceptionBehavior> InterceptionBehaviors;
// Properties
public int Count
{
get
{
return InterceptionBehaviors.Count;
}
}
// Methods
public InterceptionBehaviorPipeline()
{
InterceptionBehaviors = new List<IInterceptionBehavior>();
}
public MethodReturn Call(MethodInvocation input, IInterceptionBehavior target)
{ //无任何拦截行为
if (InterceptionBehaviors.Count == 0)
{
return target.Invoke(input, null);
}
//至少有一个IInterceptionBehavior
NextBehaviorsImp nb = new NextBehaviorsImp();
nb.Target = target;
nb.Pipeline = this;
nb.InterceptorIndex = 0;
return InterceptionBehaviors[0].Invoke(input, nb); }
} public class InterceptionUtil
{
internal InterceptionBehaviorPipeline Pipeline = new InterceptionBehaviorPipeline(); public object Call(BLLObj targetObj, string methodName, object[] args)
{ InterceptionInvocationProxy proxy = new InterceptionInvocationProxy();
proxy.Target = targetObj;
proxy.Args = args;
proxy.Invocation = new MethodInvocation();
proxy.Invocation.MethodBase = proxy.Target.GetType().GetMethod(methodName);
proxy.Invocation.Target = proxy.Target; //将目标类的方法封装代理提供给调用连,开始进行拦截调用
MethodReturn ret= Pipeline.Call(proxy.Invocation, proxy);
return ret.ReturnValue; }
} }

(图2)调用序列--务必鼠标右键找到图片url后看大图

参考

1.当然实现函数调用链不一定要搞这么麻烦,可以通过嵌套代理类来实现上面的调用效果.
地址:http://www.cnblogs.com/wdfrog/p/3162524.html

2.如果哥们觉得上面的看起来很轻松,那么你可以直接看MS使用委托代替上面两个接口的实现代码.
地址:http://www.cnblogs.com/wdfrog/archive/2010/10/23/1859021.html

至于为什么微软要用委托(而且用了大量的匿名委托)来实现,估计是觉得用接口是java干的事情,为了跟其保持距离所以某个MS的程序员在领导的要求下写出了这样变态的代码。

 
 
 
标签: AOP函数调用链

AOP之拦截函数调用链实现的更多相关文章

  1. spring---aop(3)---Spring AOP的拦截器链

    写在前面 时间断断续续,这次写一点关于spring aop拦截器链的记载.至于如何获取spring的拦截器,前一篇博客已经写的很清楚(spring---aop(2)---Spring AOP的JDK动 ...

  2. Spring AOP 源码分析 - 拦截器链的执行过程

    1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...

  3. 33、[源码]-AOP原理-获取拦截器链-MethodInterceptor

    33.[源码]-AOP原理-获取拦截器链-MethodInterceptor

  4. Spring异步调用原理及SpringAop拦截器链原理

    一.Spring异步调用底层原理 开启异步调用只需一个注解@EnableAsync @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTI ...

  5. JdkDynamicAopProxy 拦截器链的获得与递归执行

    JdkDynamicAopProxy类的invoke方法 1.获得拦截器链 List<Object> chain = this.advised.getInterceptorsAndDyna ...

  6. 手撸了一个HTTP框架:支持Sprng MVC、IOC、AOP,拦截器,配置文件读取...

    https://github.com/Snailclimb/jsoncat :仿 Spring Boot 但不同于 Spring Boot 的一个轻量级的 HTTP 框架 距离上一次给小伙伴们汇报简易 ...

  7. java 动态代理—— Mybaties 拦截器链基本原理实现

    1.摘要 Mybaties 中有个分页插件,之前有特意的去了解了一下原理 :https://www.cnblogs.com/jonrain0625/p/11168247.html,从了解中得知分页插件 ...

  8. AOP实现拦截对象以及获取切入目标方法和注解

    AOP实现拦截对象以及获取切入目标方法和注解 一.JoinPoint是什么? AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用 o ...

  9. SIGSEGV异常时打印函数调用链

    C语言写的程序跑飞了,怎样打印出函数调用链呢? linux_dev_framework软件包中的trace_exception_test.c就是一个实现演示样例. 该程序有益产生一个内存訪问异常,然后 ...

随机推荐

  1. 小结php中几种网页跳转

    1.使用网页中<a href=.....></a>实现跳转: 2.<form action="php_request2.php" method=&qu ...

  2. 且看三星刚发布的Smart TV如何窃听你的枕边细语

    三星最新的SmartTV有一个很酷的新的声控功能,网络连接设备可以通过它来录下你说过的所有内容并把它上传到一个第三方的地方进行存储. 该公司的语音识别软件允许用户跟他们的电视通过声音来进行沟通.一旦电 ...

  3. curl_redir_exec()函数

    function curl_redir_exec($ch,$debug="") { static $curl_loops = 0; static $curl_max_loops = ...

  4. A在SP.NET跨页多选

    在ASP.NET跨页多选 本文介绍怎样在ASP.NET中实现多页面选择的问题.其详细思路非常easy:用隐藏的INPUT记住每次选择的项目,在进行数据绑定时.检查保存的值,再在DataGrid中进行选 ...

  5. 快速构建Windows 8风格应用25-数据绑定

    原文:快速构建Windows 8风格应用25-数据绑定 本篇博文主要介绍如何将UI元素与数据进行绑定.数据绑定的方向.数据更改通知.数据转换.数据绑定支持的绑定方案. 数据绑定是一种简单方式来显示数据 ...

  6. C# 根据年、月、周、星期获得日期等

    原文:C# 根据年.月.周.星期获得日期等 /// 取得某月的第一天 /// </summary> /// <param name="datetime">要 ...

  7. VS2015 Apache Cordova

    VS2015 Apache Cordova第一个Android和IOS应用   前言 本人个人博客原文链接地址为http://aehyok.com/Blog/Detail/75.html. http: ...

  8. js获取最近几天的日期(转载)

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  9. 【蜗牛—漫漫IT路之大学篇(九) 】

    再来一篇叨叨的博客 近期,状态还是那个状态,人还是那个人. 前两天,感冒了,可能是宿舍阴面的事吧.然后,中午睡觉的时候穿着短袖披了一件外套,然后鼻子就不通气了.只是,前天晚上,我骑着崔国强的车子跑了不 ...

  10. asp.net MVC中的AppendTrailingSlash以及LowercaseUrls

    asp.net MVC中的AppendTrailingSlash以及LowercaseUrls asp.net MVC是一个具有极大扩展性的框架,可以在从Url请求开始直到最终的html的渲染之间进行 ...