AOP(面向方面)设计概念流行已经不少日子了,一直以来对这方面进行了不少了解,今天终于有幸可以小试一下,下面把具体实现过程供大家参考
1、定义代理类,代理类从RealProxy继承实现,关键的方法是Invoke,对对象的请求的附加功能都在这个方法里实现代码如下:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Services;
using System.Security.Permissions;
namespace ProxyTest
{
/// <summary>
/// AOP代理类
/// </summary>
public class AspectProxy : RealProxy
{
private MarshalByRefObject _Target = null;
public AspectProxy() : base()
{
}
public AspectProxy(Type serverType) : base(serverType)
{
}
public AspectProxy(Type serverType,MarshalByRefObject target) : base(serverType)
{
this._Target = target;
}
public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage msg)
{
IMessage retMsg = null;;
if(msg is IConstructionCallMessage)
{
IConstructionCallMessage ccm = (IConstructionCallMessage)msg;
//用结构调用请求, 初始化当前代理的真实代理所表示的远程对象的对象类型的实例
RemotingServices.GetRealProxy(this._Target).InitializeServerObject(ccm);
//将远程对象转换为本地带URL和Type的ObjRef对象
ObjRef oRef = RemotingServices.Marshal(this._Target);
//从oRef创建一个代理对象
RemotingServices.Unmarshal(oRef);
retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage(ccm,
(MarshalByRefObject)this.GetTransparentProxy());
}
else
{
IMethodCallMessage mcm = (IMethodCallMessage)msg;
retMsg = RemotingServices.ExecuteMessage(this._Target,mcm);
//对方法调用记录日志,在这里加入其他处理
MethodCallLog.AddLog(mcm.MethodBase.ReflectedType.ToString() + "."
+ mcm.MethodName + "\t" + DateTime.Now.ToString());
}
return retMsg;
}
}
}
下面是日志的实现类,比较简单:
using System;
using System.IO;
namespace ProxyTest
{
/// <summary>
/// MethodCallLog 的摘要说明。
/// </summary>
public class MethodCallLog
{
public static void AddLog(string message)
{
StreamWriter w =
new StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "MethodCall.log",true);
w.WriteLine(message);
w.Close();
}
}
}
2、定义一个AOP对象处理特性,AspectAttribute从ProxyAtrribute继承实现,用来标识对象类型需要自定义代理实现,代码发下:
using System;
using System.Security.Permissions;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Proxies;
namespace ProxyTest
{
/// <summary>
/// 标识类需要进行AOP处理
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
[SecurityPermission(SecurityAction.Demand,Flags=SecurityPermissionFlag.Infrastructure)]
public class AspectAttribute : ProxyAttribute
{
public override MarshalByRefObject CreateInstance(Type serverType)
{
MarshalByRefObject mobj = base.CreateInstance (serverType);
RealProxy proxy = new AspectProxy(serverType,mobj);
return proxy.GetTransparentProxy() as MarshalByRefObject;
}
}
}
3、紧接着,定义了一个类,标识为需要做AOP处理
using System;
namespace ProxyTest
{
/// <summary>
/// AOP处理对象
/// </summary>
[Aspect()]
public class AspectObject : ContextBoundObject
{
private int _value;
public AspectObject()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public AspectObject(int value)
{
this._value = value;
}
public void Add(int value)
{
this._value += value;
}
}
}
编译运行,可以在Bin\Debug下看到 MethodCall.log内容如下:
ProxyTest.AspectObject.Add 2006-3-10 15:31:48 ProxyTest.AspectObject.Add 2006-3-10 15:31:53 ProxyTest.AspectObject.Add 2006-3-10 15:31:54 ProxyTest.AspectObject.Add 2006-3-10 15:31:54 ProxyTest.AspectObject.Add 2006-3-10 15:31:54 ProxyTest.AspectObject.Add 2006-3-10 15:31:55 ProxyTest.AspectObject.Add 2006-3-10 15:31:55 ProxyTest.AspectObject.Add 2006-3-10 15:31:55 ProxyTest.AspectObject.Add 2006-3-10 15:31:55 ProxyTest.AspectObject.Add 2006-3-10 15:31:56 ProxyTest.AspectObject.Add 2006-3-10 15:31:56
到此一个简单的AOP实现已经做完,当然实际应用比这要复杂得多,不过基本过程一样,有不对或者不合理的地方,大家多多指点
文章来源:http://blog.csdn.net/samen168/archive/2006/03/10/621008.aspx