C#实现AOP微型框架深入剖析

开发 后端
这里介绍C#实现AOP微型框架,AOP的最基本功能就是实现特定的预处理和后处理,我通过代理让C#实现AOP微型框架。先来看看构成此微型框架的.cs文件。

在向大家详细介绍C#实现AOP微型框架之前,首先让大家了解下微型框架的.cs文件,然后全面介绍C#实现AOP微型框架。

在前面的系列文章中,我介绍了消息、代理与AOP的关系,这次将我自己用C#实现AOP微型框架拿出来和大家交流一下。

AOP的最基本功能就是实现特定的预处理和后处理,我通过代理让C#实现AOP微型框架。先来看看构成此微型框架的.cs文件。

1.CommonDef.cs 用于定义最基本的AOP接口

  1. using System;  
  2. using System.Runtime.Remoting.Messaging ;  
  3.  
  4. namespace EnterpriseServerBase.Aop  
  5. {  
  6. /// <summary> 
  7. /// IAopOperator AOP操作符接口,包括前处理和后处理  
  8. /// 2005.04.12  
  9. /// </summary> 
  10. public interface IAopOperator  
  11. {  
  12. void PreProcess(IMessage requestMsg ) ;  
  13. void PostProcess(IMessage requestMsg ,IMessage Respond) ;  
  14. }  
  15.  
  16. /// <summary> 
  17. /// IAopProxyFactory 用于创建特定的Aop代理的实例,
    IAopProxyFactory的作用是使AopProxyAttribute独立于具体的AOP代理类。  
  18. /// </summary> 
  19. public interface IAopProxyFactory  
  20. {  
  21. AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;  
  22. }  
  23.  

2. AopProxyBase AOP代理的基类

所有自定义AOP代理类都从此类派生,覆写IAopOperator接口,实现具体的前/后处理 。

  1. using System;  
  2. using System.Runtime.Remoting ;  
  3. using System.Runtime.Remoting.Proxies ;  
  4. using System.Runtime.Remoting.Messaging ;  
  5. using System.Runtime.Remoting.Services ;  
  6. using System.Runtime.Remoting.Activation ;  
  7.  
  8. namespace EnterpriseServerBase.Aop  
  9. {  
  10. /// <summary> 
  11. /// AopProxyBase 所有自定义AOP代理类都从此类派生,
    覆写IAopOperator接口,实现具体的前/后处理 。  
  12. /// 2005.04.12  
  13. /// </summary> 
  14. public abstract class AopProxyBase : RealProxy ,IAopOperator  
  15. {  
  16. private readonly MarshalByRefObject target ; //默认透明代理  
  17.  
  18. public AopProxyBase(MarshalByRefObject obj ,Type type) :base(type)  
  19. {  
  20. this.target = obj ;  
  21. }  
  22.  
  23. #region Invoke  
  24. public override IMessage Invoke(IMessage msg)  
  25. {  
  26. bool useAspect = false ;  
  27. IMethodCallMessage call = (IMethodCallMessage)msg ;  
  28.  
  29. //查询目标方法是否使用了启用AOP的MethodAopSwitcherAttribute  
  30. foreach(Attribute attr in call.MethodBase.GetCustomAttributes(false))  
  31. {  
  32. MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute ;  
  33. if(mehodAopAttr != null)  
  34. {  
  35. if(mehodAopAttr.UseAspect)  
  36. {  
  37. useAspect = true ;  
  38. break ;  
  39. }  
  40. }  
  41. }  
  42.  
  43. if(useAspect)  
  44. {  
  45. this.PreProcess(msg) ;  
  46. }  
  47.  
  48. //如果触发的是构造函数,此时target的构建还未开始  
  49. IConstructionCallMessage ctor = call as IConstructionCallMessage ;  
  50. if(ctor != null)  
  51. {  
  52. //获取***层的默认真实代理  
  53. RealProxy default_proxy = RemotingServices.GetRealProxy(this.target) ;  
  54.  
  55. default_proxy.InitializeServerObject(ctor) ;  
  56. MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy() ; 
  57. //自定义的透明代理 this  
  58.  
  59. return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor,tp);  
  60. }  
  61.  
  62. IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ; 
  63. //将消息转化为堆栈,并执行目标方法,方法完成后,再将堆栈转化为消息  
  64.  
  65. if(useAspect)  
  66. {  
  67. this.PostProcess(msg ,result_msg) ;  
  68. }  
  69.  
  70. return result_msg ;  
  71.  
  72. }  
  73. #endregion  
  74.  
  75. #region IAopOperator 成员  
  76.  
  77. public abstract void PreProcess(IMessage requestMsg) ;  
  78. public abstract void PostProcess(IMessage requestMsg, IMessage Respond) ;  
  79. #endregion  
  80. }  

【编辑推荐】

  1. C#创建表单简单介绍
  2. C#修改DataReader默认行为
  3. C#设置CooperativeLevel概述
  4. C#表单增加控件简单描述
  5. C# EmployeePlug类概述
责任编辑:佚名 来源: 博客园
相关推荐

2009-09-03 15:38:54

C#实现AOP微型框架

2009-09-02 13:36:58

C#实现多个接口

2009-09-11 11:09:36

C#引用类型

2009-09-02 18:14:33

C# WebClien

2009-09-04 17:56:22

C#删除数据

2009-09-29 10:00:40

Spring AOP框

2009-09-03 17:42:07

C#开发CF蓝牙模块

2009-09-04 17:49:34

C#连接数据库

2015-07-28 10:06:03

C#内部实现剖析

2009-09-10 17:37:01

C# get post

2009-08-28 15:38:49

C#实现断点续传

2009-08-27 17:14:36

C# Socket

2009-09-07 14:29:52

C# ServiceC

2009-09-01 16:29:03

QuickSort C

2010-02-06 16:05:51

C++ Vector

2009-09-01 11:04:59

C#调用扩展方法

2009-09-11 11:17:04

C#引用类型

2009-08-27 16:29:18

C#动态编译

2009-08-27 17:51:34

C#匿名方法

2009-08-31 17:26:32

C#异常处理
点赞
收藏

51CTO技术栈公众号