在前面的系列文章中我介绍了消息代理与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) ;
}
}
AopProxyBaseAOP代理的基类所有自定义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
}
}
AopProxyAttributeAOP代理特性
/****************************** 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 ;
}
}
}
}