AOP 和 OOP,在我看来是两种相辅相成的技术,作为OOP的补充,AOP 有着自己特殊的应用场景。
假设,我们需要在Service层实现以下几项基本功能:
/// <para>1、自动管理数据库连接[可选]</para>
/// <para>2、自动管理数据库事务,当接收到异常后(无论什么异常)事务将自动回滚[可选]</para>
/// <para>3、服务级加锁[必选]</para>
/// <para>4、以统一方式处理 服务异常 及 错误, 包括数据库异常 和 主动抛出的异常[必选]</para>
解释:
1、在 执行Service方法前 打开数据库连接, 在 执行Service方法后 关闭数据库连接
2、在 执行Service方法前 Begin数据库事务, 在 执行Service方法后 Commit数据库事务, Catch异常后 RollBack数据库事务
3、将 整个Service方法 lock 进去,lock Service 的私有静态对象,以达到服务级方法的 线程安全及同步工作
4、捕获Service方法中所有未捕获的异常,捕获异常后,如果需要将自动关闭连接和回滚事务。并记录异常信息。
即、主动报告错误时,只需要抛出异常即可。
为了 实现如上的功能,并能简单方便实现,而且不打破现有的C#编码规范。
所以,引入AOP、 使用 Attribute 为方法 指定增强对象,
以便在调用Service方法前,执行方法的前置增强(包括打开连接、开启事务等)
在调用Service方法后,执行方法的后置增强(包括关闭连接、提交事务等)
及 对整个调用方法实现 Try...Catch异常捕获 和 Lock 加锁。
C# 引入了 Proxy (代理)的概念,即 System.Runtime.Remoting.Proxies.RealProxy 提供了 代理的基本功能。利用该对象可以自己实现AOP编程。
RealProxy 可以可以为任何 “直接或间接继承于 System.MarshalByRefObject” 的类型 提供代理。
RealProxy 可以为指定类型创建一个代理对象, 被创建的代理对象的类型 可以看做是 指定类型的 子类(但 被指定的类型可以是密封类)。
【PS: 看做子类,更容易理解,本质上为被创建的代理对象的类型 和 指定类型直接为 组合关系,并不是继承关系 】
RealProxy 的工作原理:
假设:
T 为 需要被代理的类型, t 为对象
ProxyT 为 被创建的代理类型, proxyT 为对象
T 类型中存在 成员方法 Test();
ProxyT 继承于 T【实际上不为继承关系,应该为组合,为方便理解看做继承关系】, ProxyT 同样也存在方法 Test
当执行如下代码时:
proxyT.Test();
.NET runtime 会自动调用 System.Runtime.Remoting.Proxies.RealProxy.Invoke(...)方法。
而该方法为抽象方法,自己重写该方法,在方法内部调用 t.Test()。
在调用之前、执行前置增强;在调用之后、执行后置增强; 及 其他处理操作。
由此可实现 AOP 编程,织入增强。
自定义的RealProxy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
namespace AOPDemo.Common
{
public class DelayProxy<T> : RealProxy
{
private static object objLock = new object();
/// <summary>
/// 被代理的对象
/// </summary>
private T target;
public DelayProxy(T target)
: base(typeof(T))
{
this.target = target;
}
public override IMessage Invoke(IMessage msg)
{
IMethodCallMessage callMessage = (IMethodCallMessage)msg;
Console.WriteLine("方法被调用前");
Console.WriteLine("调用方法名:" + callMessage.MethodName);
IMessage message = DelayProxyUtil.InvokeBeProxy(this.target, callMessage);
Console.WriteLine("方法被调用后");
return message;
}
}
}
RealProxy
辅助工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Reflection;
namespace AOPDemo.Common
{
/// <summary>
/// 延迟初始化代理工具类
/// </summary>
public static class DelayProxyUtil
{
/// <summary>
/// 调用被代理对象中方法,返回 被代理对象的 方法返回值
/// <para>支持 out ref 参数</para>
/// </summary>
/// <param name="target"></param>
/// <param name="callMessage"></param>
/// <returns></returns>
public static IMessage InvokeBeProxy(object target, IMethodCallMessage callMessage)
{
var args = callMessage.Args;
object returnValue = callMessage.MethodBase.Invoke(target, args);
return new ReturnMessage(returnValue, args, args.Length, callMessage.LogicalCallContext, callMessage);
}
/// <summary>
/// 向上层抛出异常
/// </summary>
/// <param name="ex"></param>
/// <param name="callMessage"></param>
/// <returns></returns>
public static IMessage ReturnExecption(Exception ex, IMethodCallMessage callMessage)
{
return new ReturnMessage(ex, callMessage);
}
/// <summary>
/// 获取对象的代理
/// </summary>
/// <param name="type"></param>
/// <param name="instance"></param>
/// <param name="delay"></param>
/// <returns></returns>
public static object GetTransparentProxy(Type type, object instance)
{
Type tmpType = typeof(DelayProxy<>);
tmpType = tmpType.MakeGenericType(type);
RealProxy proxy = Activator.CreateInstance(tmpType, new object[] { instance }) as RealProxy;
return proxy.GetTransparentProxy();
}
}
}
简单的Demo

public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
Service service = new Service();
Service proxy = Common.DelayProxyUtil.GetTransparentProxy(typeof(Service), service) as Service;
proxy.Test();
return View();
}
}
public class Service : MarshalByRefObject
{
public void Test()
{
Console.WriteLine("调用Test方法");
}
}
View Code
由于例子很简单,就不上传源码了。
未完待续...
返回导读目录,阅读更多随笔
分割线,以下为博客签名:
软件臭虫情未了
- 编码一分钟
- 测试十年功
随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。