C#實(shí)現(xiàn)AOP微型框架深入剖析
在向大家詳細(xì)介紹C#實(shí)現(xiàn)AOP微型框架之前,首先讓大家了解下微型框架的.cs文件,然后全面介紹C#實(shí)現(xiàn)AOP微型框架。
在前面的系列文章中,我介紹了消息、代理與AOP的關(guān)系,這次將我自己用C#實(shí)現(xiàn)AOP微型框架拿出來(lái)和大家交流一下。
AOP的最基本功能就是實(shí)現(xiàn)特定的預(yù)處理和后處理,我通過(guò)代理讓C#實(shí)現(xiàn)AOP微型框架。先來(lái)看看構(gòu)成此微型框架的.cs文件。
1.CommonDef.cs 用于定義最基本的AOP接口
- 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 用于創(chuàng)建特定的Aop代理的實(shí)例,
IAopProxyFactory的作用是使AopProxyAttribute獨(dú)立于具體的AOP代理類(lèi)。- /// </summary>
- public interface IAopProxyFactory
- {
- AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj ,Type type) ;
- }
- }
2. AopProxyBase AOP代理的基類(lèi)
所有自定義AOP代理類(lèi)都從此類(lèi)派生,覆寫(xiě)IAopOperator接口,實(shí)現(xiàn)具體的前/后處理 。
- 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代理類(lèi)都從此類(lèi)派生,
覆寫(xiě)IAopOperator接口,實(shí)現(xiàn)具體的前/后處理 。- /// 2005.04.12
- /// </summary>
- public abstract class AopProxyBase : RealProxy ,IAopOperator
- {
- private readonly MarshalByRefObject target ; //默認(rèn)透明代理
- 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 ;
- //查詢目標(biāo)方法是否使用了啟用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) ;
- }
- //如果觸發(fā)的是構(gòu)造函數(shù),此時(shí)target的構(gòu)建還未開(kāi)始
- IConstructionCallMessage ctor = call as IConstructionCallMessage ;
- if(ctor != null)
- {
- //獲取***層的默認(rèn)真實(shí)代理
- 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) ;
- //將消息轉(zhuǎn)化為堆棧,并執(zhí)行目標(biāo)方法,方法完成后,再將堆棧轉(zhuǎn)化為消息
- 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
- }
- }
【編輯推薦】