熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C# AOP微型框架實現

2022-06-13   來源: .NET編程 

  在前面的系列文章中我介紹了消息代理與AOP的關系這次將我自己實現的一個AOP微型框架拿出來和大家交流一下
  
  AOP的最基本功能就是實現特定的預處理和後處理我通過代理實現了此微型框架
  
  先來看看構成此微型框架的cs文件
  
  CommonDefcs 用於定義最基本的AOP接口
  
  /************************************* CommonDefcs **************************
  
  using System;
  using SystemRuntimeRemotingMessaging ;
  
  namespace EnterpriseServerBaseAop
  {
  /// <summary>
  /// IAopOperator AOP操作符接口包括前處理和後處理
  ///
  /// </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) ;
  }
  
  }
  
   AopProxyBase AOP代理的基類所有自定義AOP代理類都從此類派生覆寫IAopOperator接口實現具體的前/後處理
  
  using System;
  using SystemRuntimeRemoting ;
  using SystemRuntimeRemotingProxies ;
  using SystemRuntimeRemotingMessaging ;
  using SystemRuntimeRemotingServices ;
  using SystemRuntimeRemotingActivation ;
  
  namespace EnterpriseServerBaseAop
  {
  /// <summary>
  /// AopProxyBase 所有自定義AOP代理類都從此類派生覆寫IAopOperator接口實現具體的前/後處理
  ///
  /// </summary>
  public abstract class AopProxyBase : RealProxy IAopOperator
  {
  private readonly MarshalByRefObject target ; //默認透明代理
  
  public AopProxyBase(MarshalByRefObject obj Type type) :base(type)
  {
  thistarget = obj ;
  }
  
  #region Invoke
  public override IMessage Invoke(IMessage msg)
  {
  bool useAspect = false ;
  IMethodCallMessage call = (IMethodCallMessage)msg ;
  
  //查詢目標方法是否使用了啟用AOP的MethodAopSwitcherAttribute
  foreach(Attribute attr in callMethodBaseGetCustomAttributes(false))
  {
  MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute ;
  if(mehodAopAttr != null)
  {
  if(mehodAopAttrUseAspect)
  {
  useAspect = true ;
  break ;
  }
  }
  }
  
  if(useAspect)
  {
  thisPreProcess(msg) ;
  }
  
  //如果觸發的是構造函數此時target的構建還未開始
  IConstructionCallMessage ctor = call as IConstructionCallMessage ;
  if(ctor != null)
  {
  //獲取最底層的默認真實代理
  RealProxy default_proxy = RemotingServicesGetRealProxy(thistarget) ;
  
  default_proxyInitializeServerObject(ctor) ;
  MarshalByRefObject tp = (MarshalByRefObject)thisGetTransparentProxy() ; //自定義的透明代理 this
  
  return EnterpriseServicesHelperCreateConstructionReturnMessage(ctortp);
  }
  
  IMethodReturnMessage result_msg = RemotingServicesExecuteMessage(thistarget call) ; //將消息轉化為堆棧並執行目標方法方法完成後再將堆棧轉化為消息
  
  if(useAspect)
  {
  thisPostProcess(msg result_msg) ;
  }
  
  return result_msg ;
  
  }
  #endregion
  
  #region IAopOperator 成員
  
  public abstract void PreProcess(IMessage requestMsg) ;
  public abstract void PostProcess(IMessage requestMsg IMessage Respond) ;
  #endregion
  
  }
  
  }
  
   AopProxyAttribute AOP代理特性
  
  /****************************** AopProxyAttribute  ************************************
  
  using System;
  using SystemRuntimeRemoting ;
  using SystemRuntimeRemotingProxies ;
  
  
  namespace EnterpriseServerBaseAop
  {
  /// <summary>
  /// AopProxyAttribute
  /// AOP代理特性如果一個類想實現具體的AOP只要實現AopProxyBase和IAopProxyFactory然後加上該特性即可
  ///
  /// </summary>
  
  [AttributeUsage(AttributeTargetsClass AllowMultiple = false)]
  public class AopProxyAttribute : ProxyAttribute
  {
  private IAopProxyFactory proxyFactory = null ;
  
  public AopProxyAttribute(Type factoryType)
  {
  thisproxyFactory = (IAopProxyFactory)ActivatorCreateInstance(factoryType) ;
  }
  
  #region CreateInstance
  /// <summary>
  /// 獲得目標對象的自定義透明代理
  /// </summary>
  public override MarshalByRefObject CreateInstance(Type serverType)//serverType是被AopProxyAttribute修飾的類
  {
  //未初始化的實例的默認透明代理
  MarshalByRefObject target = baseCreateInstance (serverType); //得到位初始化的實例(ctor未執行)
  object[] args = {target serverType} ;
  //AopProxyBase rp = (AopProxyBase)ActivatorCreateInstance(thisrealProxyType args) ; //ActivatorCreateInstance在調用ctor時通過了代理所以此處將會失敗
  
  //得到自定義的真實代理
  AopProxyBase rp = thisproxyFactoryCreateAopProxyInstance(target serverType) ;//new AopControlProxy(target serverType) ;
  return (MarshalByRefObject)rpGetTransparentProxy() ;
  }
  #endregion
  }
  }
  
   MethodAopSwitcherAttributecs
  
  /**************************** MethodAopSwitcherAttributecs *************************
  
  using System;
  
  namespace EnterpriseServerBaseAop
  {
  /// <summary>
  /// MethodAopSwitcherAttribute 用於決定一個被AopProxyAttribute修飾的class的某個特定方法是否啟用截獲
  /// 創建原因絕大多數時候我們只希望對某個類的一部分Method而不是所有Method使用截獲
  /// 使用方法如果一個方法沒有使用MethodAopSwitcherAttribute特性或使用MethodAopSwitcherAttribute(false)修飾
  ///    都不會對其進行截獲只對使用了MethodAopSwitcherAttribute(true)啟用截獲
  ///
  /// </summary>
  [AttributeUsage(AttributeTargetsMethod AllowMultiple = false )]
  public class MethodAopSwitcherAttribute : Attribute
  {
  private bool useAspect = false ;
  
  public MethodAopSwitcherAttribute(bool useAop)
  {
  thisuseAspect = useAop ;
  }
  
  public bool UseAspect
  {
  get
  {
  return thisuseAspect ;
  }
  }
  }
  }
From:http://tw.wingwit.com/Article/program/net/201311/11374.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.