Aop“面向切面编程”,与OOP“面向对象编程”一样是一种编程思路。个人理解:在不改变原有逻辑的基础上,注入其他行为。

基础代码(仿MVC拦截器实现)

  1. namespace HGL.Toolkit.Aop
  2. {
  3. [AttributeUsage(AttributeTargets.Class,AllowMultiple=true)]
  4. publicsealedclassAopAttribute:ContextAttribute,IContributeObjectSink
  5. {
  6. publicAopAttribute():base("Aop"){}
  7.  
  8. //实现IContributeObjectSink接口当中的消息接收器接口
  9. publicIMessageSinkGetObjectSink(MarshalByRefObject obj,IMessageSinknext)
  10. {
  11. returnnewAopProxy(next);
  12. }
  13. }
  14.  
  15. [AttributeUsage(AttributeTargets.Method,AllowMultiple=true)]
  16. publicclassAopMethodAttribute:Attribute
  17. {
  18. /// <summary>
  19. /// 方法执行之前,执行代码
  20. /// </summary>
  21. publicvirtualboolOnMethodExecuting(System.Web.HttpContext context)
  22. {
  23. returntrue;
  24. }
  25.  
  26. /// <summary>
  27. /// 方法执行之后,执行代码
  28. /// </summary>
  29. publicvirtualboolOnMethodExecuted(System.Web.HttpContext context)
  30. {
  31. returntrue;
  32. }
  33. }
  34. }
  35.  
  36. namespace HGL.Toolkit.Aop
  37. {
  38. publicsealedclassAopProxy:IMessageSink
  39. {
  40. //下一个接收器
  41. privateIMessageSink nextSink;
  42.  
  43. publicIMessageSinkNextSink
  44. {
  45. get{return nextSink;}
  46. }
  47.  
  48. publicAopProxy(IMessageSink nextSink)
  49. {
  50. this.nextSink = nextSink;
  51. }
  52.  
  53. //同步处理方法
  54. publicIMessageSyncProcessMessage(IMessage msg)
  55. {
  56. IMessage retMsg =null;
  57.  
  58. //方法调用消息接口
  59. IMethodCallMessage call = msg asIMethodCallMessage;
  60.  
  61. var attributes =Attribute.GetCustomAttributes(call.MethodBase,typeof(AopMethodAttribute));
  62.  
  63. //如果被调用的方法没打AopMethodAttribute标签
  64. if(call ==null|| attributes.Count()==0)
  65. {
  66. retMsg = nextSink.SyncProcessMessage(msg);
  67. }
  68. //如果打了AopMethodAttribute标签
  69. else
  70. {
  71. foreach(var attribute in attributes)
  72. {
  73. var obj = attribute.GetType();
  74.  
  75. var executing = obj.GetMethod("OnMethodExecuting");
  76. if(executing !=null)
  77. {
  78. var context=System.Web.HttpContext.Current;
  79. executing.Invoke(attribute,newobject[]{ context });
  80. }
  81. }
  82.  
  83. retMsg = nextSink.SyncProcessMessage(msg);
  84.  
  85. foreach(var attribute in attributes)
  86. {
  87. var obj = attribute.GetType();
  88.  
  89. var executed = obj.GetMethod("OnMethodExecuted");
  90. if(executed !=null)
  91. {
  92. var context =System.Web.HttpContext.Current;
  93. executed.Invoke(attribute,newobject[]{ context });
  94. }
  95. }
  96. }
  97.  
  98. return retMsg;
  99. }
  100.  
  101. //异步处理方法(不需要)
  102. publicIMessageCtrlAsyncProcessMessage(IMessage msg,IMessageSink replySink)
  103. {
  104. returnnull;
  105. }
  106. }
  107. }

使用注意:

  1. 1.使用到Aop的类必须继承ContextBoundObject
  2. 2.使用到Aop的方法存在的类必须添加[Aop]特性
  3. 3.哪个方法需要实现Aop,就给那个方法添加[AopMethod]特性
  4.  
  5. AopMethod未实现任何业务逻辑,使用者需要重写该attribute
  6.  
  7. /// <summary>
  8. /// 方法执行之前,执行代码
  9. /// </summary>
  10. publicvirtualboolOnMethodExecuting()
  11. {
  12. returntrue;
  13. }
  14.  
  15. /// <summary>
  16. /// 方法执行之后,执行代码
  17. /// </summary>
  18. publicvirtualboolOnMethodExecuted()
  19. {
  20. returntrue;
  21. }

待续......(将执行前和执行后的方法使用代理委托机制,实现二次开发)