在前面的系列文章中
AOP的最基本功能就是實現特定的預處理和後處理
先來看看構成此微型框架的
/************************************* CommonDef
using System;
using System
namespace EnterpriseServerBase
{
/// <summary>
/// IAopOperator AOP操作符接口
///
/// </summary>
public interface IAopOperator
{
void PreProcess(IMessage requestMsg ) ;
void PostProcess(IMessage requestMsg
}
/// <summary>
/// IAopProxyFactory 用於創建特定的Aop代理的實例
/// </summary>
public interface IAopProxyFactory
{
AopProxyBase CreateAopProxyInstance(MarshalByRefObject obj
}
}
using System;
using System
using System
using System
using System
using System
namespace EnterpriseServerBase
{
/// <summary>
/// AopProxyBase 所有自定義AOP代理類都從此類派生
///
/// </summary>
public abstract class AopProxyBase : RealProxy
{
private readonly MarshalByRefObject target ; //默認透明代理
public AopProxyBase(MarshalByRefObject obj
{
this
}
#region Invoke
public override IMessage Invoke(IMessage msg)
{
bool useAspect = false ;
IMethodCallMessage call = (IMethodCallMessage)msg ;
//查詢目標方法是否使用了啟用AOP的MethodAopSwitcherAttribute
foreach(Attribute attr in call
{
MethodAopSwitcherAttribute mehodAopAttr = attr as MethodAopSwitcherAttribute ;
if(mehodAopAttr != null)
{
if(mehodAopAttr
{
useAspect = true ;
break ;
}
}
}
if(useAspect)
{
this
}
//如果觸發的是構造函數
IConstructionCallMessage ctor = call as IConstructionCallMessage ;
if(ctor != null)
{
//獲取最底層的默認真實代理
RealProxy default_proxy = RemotingServices
default_proxy
MarshalByRefObject tp = (MarshalByRefObject)this
return EnterpriseServicesHelper
}
IMethodReturnMessage result_msg = RemotingServices
if(useAspect)
{
this
}
return result_msg ;
}
#endregion
#region IAopOperator 成員
public abstract void PreProcess(IMessage requestMsg) ;
public abstract void PostProcess(IMessage requestMsg
#endregion
}
}
/****************************** AopProxyAttribute ************************************
using System;
using System
using System
namespace EnterpriseServerBase
{
/// <summary>
/// AopProxyAttribute
/// AOP代理特性
///
/// </summary>
[AttributeUsage(AttributeTargets
public class AopProxyAttribute : ProxyAttribute
{
private IAopProxyFactory proxyFactory = null ;
public AopProxyAttribute(Type factoryType)
{
this
}
#region CreateInstance
/// <summary>
/// 獲得目標對象的自定義透明代理
/// </summary>
public override MarshalByRefObject CreateInstance(Type serverType)//serverType是被AopProxyAttribute修飾的類
{
//未初始化的實例的默認透明代理
MarshalByRefObject target = base
object[] args = {target
//AopProxyBase rp = (AopProxyBase)Activator
//得到自定義的真實代理
AopProxyBase rp = this
return (MarshalByRefObject)rp
}
#endregion
}
}
/**************************** MethodAopSwitcherAttribute
using System;
namespace EnterpriseServerBase
{
/// <summary>
/// MethodAopSwitcherAttribute 用於決定一個被AopProxyAttribute修飾的class的某個特定方法是否啟用截獲
/// 創建原因
/// 使用方法
/// 都不會對其進行截獲
///
/// </summary>
[AttributeUsage(AttributeTargets
public class MethodAopSwitcherAttribute : Attribute
{
private bool useAspect = false ;
public MethodAopSwitcherAttribute(bool useAop)
{
this
}
public bool UseAspect
{
get
{
return this
}
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11374.html