C# AOP微型框架实现

简介: 来源:中国自学编程网 发布日期:1211261269  在前面的系列文章中,我介绍了消息、代理与AOP的关系,这次将我自己实现的一个AOP微型框架拿出来和大家交流一下。

来源:中国自学编程网 发布日期:1211261269


  在前面的系列文章中,我介绍了消息、代理与AOP的关系,这次将我自己实现的一个AOP微型框架拿出来和大家交流一下。
  
  AOP的最基本功能就是实现特定的预处理和后处理,我通过代理实现了此微型框架。
  
  先来看看构成此微型框架的4个.cs文件。
  
  1.CommonDef.cs 用于定义最基本的AOP接口
  
  /************************************* CommonDef.cs **************************
  
  using System;
  using System.Runtime.Remoting.Messaging ;
  
  namespace EnterpriseServerBase.Aop
  {
  /// <summary>
  /// IAopOperator AOP操作符接口,包括前处理和后处理
  /// 2005.04.12
  /// </summary>
  public interface IAopOperator
  {
  void PreProcess(IMessage requestMsg ) ;
  void PostProcess(IMessage requestMsg ,IMessage Respond) ;
  }
  
  /// <summary>
  /// IAopProxyFactory 用于创建特定的Aop代理的实例,IAopProxyFactory的作用是使AopProxyAttribute独立于具体的AOP代理类。
  /// </summary>
  public interface IAopProxyFactory
  {
  AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;
  }
  
  }
  
  2. AopProxyBase AOP代理的基类,所有自定义AOP代理类都从此类派生,覆写IAopOperator接口,实现具体的前/后处理 。
  
  using System;
  using System.Runtime.Remoting ;
  using System.Runtime.Remoting.Proxies ;
  using System.Runtime.Remoting.Messaging ;
  using System.Runtime.Remoting.Services ;
  using System.Runtime.Remoting.Activation ;
  
  namespace EnterpriseServerBase.Aop
  {
  /// <summary>
  /// AopProxyBase 所有自定义AOP代理类都从此类派生,覆写IAopOperator接口,实现具体的前/后处理 。
  /// 2005.04.12
  /// </summary>
  public abstract class AopProxyBase : RealProxy ,IAopOperator
  {
  private readonly MarshalByRefObject target ; //默认透明代理
  
  public AopProxyBase(MarshalByRefObject obj ,Type type) :base(type)
  {
  this.target = obj ;
  }
  
  #region Invoke
  public override IMessage Invoke(IMessage msg)
  {
  bool useAspect = false ;
  IMethodCallMessage call = (IMethodCallMessage)msg ;
  
  //查询目标方法是否使用了启用AOP的MethodAopSwitcherAttribute
  foreach(Attribute attr in call.MethodBase.GetCustomAttributes(false))
  {
  MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute ;
  if(mehodAopAttr != null)
  {
  if(mehodAopAttr.UseAspect)
  {
  useAspect = true ;
  break ;
  }
  }
  }
  
  if(useAspect)
  {
  this.PreProcess(msg) ;
  }
  
  //如果触发的是构造函数,此时target的构建还未开始
  IConstructionCallMessage ctor = call as IConstructionCallMessage ;
  if(ctor != null)
  {
  //获取最底层的默认真实代理
  RealProxy default_proxy = RemotingServices.GetRealProxy(this.target) ;
  
  default_proxy.InitializeServerObject(ctor) ;
  MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy() ; //自定义的透明代理 this
  
  return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor,tp);
  }
  
  IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ; //将消息转化为堆栈,并执行目标方法,方法完成后,再将堆栈转化为消息
  
  if(useAspect)
  {
  this.PostProcess(msg ,result_msg) ;
  }
  
  return result_msg ;
  
  }
  #endregion
  
  #region IAopOperator 成员
  
  public abstract void PreProcess(IMessage requestMsg) ;
  public abstract void PostProcess(IMessage requestMsg, IMessage Respond) ;
  #endregion
  
  }
  
  }
  
  3. AopProxyAttribute AOP代理特性
  
  /****************************** AopProxyAttribute  ************************************
  
  using System;
  using System.Runtime.Remoting ;
  using System.Runtime.Remoting.Proxies ;
  
  
  namespace EnterpriseServerBase.Aop
  {
  /// <summary>
  /// AopProxyAttribute
  /// AOP代理特性,如果一个类想实现具体的AOP,只要实现AopProxyBase和IAopProxyFactory,然后加上该特性即可。
  /// 2005.04.11
  /// </summary>
  
  [AttributeUsage(AttributeTargets.Class ,AllowMultiple = false)]
  public class AopProxyAttribute : ProxyAttribute
  {
  private IAopProxyFactory proxyFactory = null ;
  
  public AopProxyAttribute(Type factoryType)
  {
  this.proxyFactory = (IAopProxyFactory)Activator.CreateInstance(factoryType) ;
  }
  
  #region CreateInstance
  /// <summary>
  /// 获得目标对象的自定义透明代理
  /// </summary>
  public override MarshalByRefObject CreateInstance(Type serverType)//serverType是被AopProxyAttribute修饰的类
  {
  //未初始化的实例的默认透明代理
  MarshalByRefObject target = base.CreateInstance (serverType); //得到位初始化的实例(ctor未执行)
  object[] args = {target ,serverType} ;
  //AopProxyBase rp = (AopProxyBase)Activator.CreateInstance(this.realProxyType ,args) ; //Activator.CreateInstance在调用ctor时通过了代理,所以此处将会失败
  
  //得到自定义的真实代理
  AopProxyBase rp = this.proxyFactory.CreateAopProxyInstance(target ,serverType) ;//new AopControlProxy(target ,serverType) ;
  return (MarshalByRefObject)rp.GetTransparentProxy() ;
  }
  #endregion
  }
  }
  
  4 .MethodAopSwitcherAttribute.cs
  
  /**************************** MethodAopSwitcherAttribute.cs *************************
  
  using System;
  
  namespace EnterpriseServerBase.Aop
  {
  /// <summary>
  /// MethodAopSwitcherAttribute 用于决定一个被AopProxyAttribute修饰的class的某个特定方法是否启用截获 。
  /// 创建原因:绝大多数时候我们只希望对某个类的一部分Method而不是所有Method使用截获。
  /// 使用方法:如果一个方法没有使用MethodAopSwitcherAttribute特性或使用MethodAopSwitcherAttribute(false)修饰,
  ///    都不会对其进行截获。只对使用了MethodAopSwitcherAttribute(true)启用截获。
  /// 2005.05.11
  /// </summary>
  [AttributeUsage(AttributeTargets.Method ,AllowMultiple = false )]
  public class MethodAopSwitcherAttribute : Attribute
  {
  private bool useAspect = false ;
  
  public MethodAopSwitcherAttribute(bool useAop)
  {
  this.useAspect = useAop ;
  }
  
  public bool UseAspect
  {
  get
  {
  return this.useAspect ;
  }
  }
  }
  }

相关文章
|
7月前
|
测试技术 C# 数据库
C# 单元测试框架 NUnit 一分钟浅谈
【10月更文挑战第17天】单元测试是软件开发中重要的质量保证手段,NUnit 是一个广泛使用的 .NET 单元测试框架。本文从基础到进阶介绍了 NUnit 的使用方法,包括安装、基本用法、参数化测试、异步测试等,并探讨了常见问题和易错点,旨在帮助开发者有效利用单元测试提高代码质量和开发效率。
303 64
|
5月前
|
Linux C# iOS开发
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
110 12
|
6月前
|
开发框架 C# iOS开发
基于C#开源、功能强大、灵活的跨平台开发框架 - Uno Platform
基于C#开源、功能强大、灵活的跨平台开发框架 - Uno Platform
231 3
|
6月前
|
开发框架 网络协议 .NET
C#/.NET/.NET Core优秀项目和框架2024年10月简报
C#/.NET/.NET Core优秀项目和框架2024年10月简报
245 3
|
7月前
|
开发框架 前端开发 API
C#/.NET/.NET Core优秀项目和框架2024年9月简报
C#/.NET/.NET Core优秀项目和框架2024年9月简报
129 1
|
6月前
|
网络协议 Unix Linux
精选2款C#/.NET开源且功能强大的网络通信框架
精选2款C#/.NET开源且功能强大的网络通信框架
206 0
|
8月前
|
编译器 C# Android开发
Uno Platform 是一个用于构建跨平台应用程序的强大框架,它允许开发者使用 C# 和 XAML 来创建适用于多个平台的应用
Uno Platform 是一个用于构建跨平台应用程序的强大框架,它允许开发者使用 C# 和 XAML 来创建适用于多个平台的应用
561 8
|
7月前
|
边缘计算 开发框架 人工智能
C#/.NET/.NET Core优秀项目和框架2024年8月简报
C#/.NET/.NET Core优秀项目和框架2024年8月简报
131 0
|
8月前
|
JSON 测试技术 C#
C#/.NET/.NET Core优秀项目框架推荐榜单
C#/.NET/.NET Core优秀项目框架推荐榜单
498 0
|
9月前
|
开发者 C# Android开发
Xamarin 与 .NET:解锁现代化移动应用开发的超级武器——深入探讨C#与.NET框架如何赋能跨平台应用,实现高效编码与卓越性能
【8月更文挑战第31天】Xamarin 与 .NET 的结合为开发者提供了强大的平台,用于构建现代化移动应用。通过 C# 和 .NET 框架,Xamarin 可以实现一次编写、多平台运行,覆盖 iOS、Android 和 Windows。这种方式不仅节省了开发时间和成本,还保证了应用的一致性和高质量。Xamarin 是一个开源框架,专为跨平台移动应用开发设计,允许使用 C# 语言和 .NET 核心库构建原生应用,并访问各平台特定功能。微软维护的 Xamarin 是 Visual Studio 生态系统的一部分,极大地提高了开发效率。
141 0