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 ;
  }
  }
  }
  }

相关文章
|
17天前
|
数据可视化 网络协议 C#
C#/.NET/.NET Core优秀项目和框架2024年3月简报
公众号每月定期推广和分享的C#/.NET/.NET Core优秀项目和框架(每周至少会推荐两个优秀的项目和框架当然节假日除外),公众号推文中有项目和框架的介绍、功能特点、使用方式以及部分功能截图等(打不开或者打开GitHub很慢的同学可以优先查看公众号推文,文末一定会附带项目和框架源码地址)。注意:排名不分先后,都是十分优秀的开源项目和框架,每周定期更新分享(欢迎关注公众号:追逐时光者,第一时间获取每周精选分享资讯🔔)。
|
5月前
|
Java Spring
spring框架之AOP模块(面向切面),附带通知类型---超详细介绍
spring框架之AOP模块(面向切面),附带通知类型---超详细介绍
51 0
|
5月前
|
缓存 监控 Java
Spring框架之AOP(面向切面编程)
Spring框架之AOP(面向切面编程)
34 0
|
2月前
|
算法 BI API
C#/.NET/.NET Core优秀项目和框架2024年1月简报
C#/.NET/.NET Core优秀项目和框架2024年1月简报
|
3月前
|
XML 存储 Java
JAVAEE框架整合技术之Spring02-AOP面向切面编程技术
JAVAEE框架整合技术之Spring02-AOP面向切面编程技术
54 0
JAVAEE框架整合技术之Spring02-AOP面向切面编程技术
|
3月前
|
数据采集 开发框架 JavaScript
C#/.NET/.NET Core优秀项目和框架2023年12月简报
C#/.NET/.NET Core优秀项目和框架2023年12月简报
|
4月前
|
XML Java 数据格式
JAVAEE框架之Spring AOP
JAVAEE框架之Spring AOP
46 0
|
4月前
|
开发框架 数据可视化 C#
C# | 上位机开发新手指南(三)框架
在上位机开发中,Windows Forms是使用最广泛的C#框架之一。Windows Forms是.NET Framework中的一个GUI框架,提供了丰富的GUI控件和易于使用的编程模型,可以快速开发Windows桌面应用程序。Windows Forms已经存在多年,并且在.NET Framework中得到广泛的支持和优化,因此在上位机开发中得到了广泛应用。除此之外,随着.NET Core的不断发展,越来越多的开发人员开始使用跨平台的C#框架进行上位机开发,例如使用Electron或Avalonia等框架开发基于Web技术的桌面应用程序。
173 0
C# | 上位机开发新手指南(三)框架
|
4月前
|
安全 Java 数据库连接
Spring框架:IoC容器、AOP、事务管理等知识讲解梳理
Spring框架:IoC容器、AOP、事务管理等知识讲解梳理
56 1
|
4月前
|
Rust 算法 C#
C#/.NET/.NET Core优秀项目和框架2023年11月简报
C#/.NET/.NET Core优秀项目和框架2023年11月简报