JointCode.Aspects,一个基于装饰模式的 Aop 框架

实现 Aop 的方法有不少,有人给总结了一下,至少有如下几种:网络

Approach框架

方法ide

Advantages性能

优势flex

Disadvantagesui

缺点spa

Remoting Proxies设计

远程代理代理

Easy to implement, because of the .Net framework supportcode

容易实现,由于有.NET框架的支持。

Somewhat heavyweight
Can only be used on interfaces or MarshalByRefObjects

微显重量级

仅在接口或MarshalByRefObjects 上使用

Derivingfrom ContextBoundObject

从ContextBoundObject 派生

Easiest to implement
Native support for call interception

很容易实现

原生支持调用拦截

Very costly in terms of performance

很是昂贵的性能代价

Compile-time subclassing
( Rhino Proxy )

编译时子类化

Easiest to understand

很容易理解

Interfaces or virtual methods only

仅用于接口或虚方法

Runtime subclassing
( Castle Dynamic Proxy )

运行时子类化

Easiest to understand
Very flexible

很容易理解

很是灵活

Complex implementation (but alreadyexists)
Interfaces or virtual methods only

复杂的实现(已经实现)

仅用于接口或虚方法

Hooking into the profiler API
( Type Mock )

分析 API钩子

Extremely powerful

极端强大

Performance?
Complex implementation (COM API, require separate runner, etc)

性能未知

复杂实现(COM API,须要单独运行等)

Compile time IL-weaving
( Post Sharp / Cecil )

编译时 IL织入

Very powerful
Good performance

很是强大

良好的性能

Very hard to implement

实现很是困难

Runtime IL-weaving
( Post Sharp / Cecil )

运行时 IL织入

Very powerful
Good performance

很是强大

朗好的性能

Very hard to implement

实现很是困难

而 JointCode.Aspects 只是一个基于装饰模式来实现方法拦截的 Aop 半成品。

什么是装饰模式,怎么实现方法拦截呢?

用代码来简单解释一下。

 1 public class Base
 2     {
 3         public void Print()
 4         {
 5             DoPrint();
 6         }
 7         protected virtual void DoPrint()
 8         {
 9             Console.WriteLine("Base.DoPrint...." + (City ?? string.Empty)); 
10         }
11     }
12     public class Derived : Base
13     {
14         protected sealed override void DoPrint()
15         {
16             Console.WriteLine("Derived.DoPrint");
17             base.Print();
18         }
19     }
View Code

如上所示,假设咱们有一个要拦截的类 Base,咱们要拦截其虚方法 Print。那么咱们只要从这个 Base 类派生出一个 Derived 类,而后在 Derived 类中重写这个方法,并在重写的方法中调用原来的方法,这样就实现了简单的方法拦截。

为何说是半成品呢?

开发这个框架时,做者的本意是想把这个框架用到本身的 IoC 框架(My.IoC)中,让 IoC 负责对象建立,而 JointCode.Aspects 则负责方法注入,这样能够经过配置实现很是灵活的对象生成。所以 JointCode.Aspects 只是在运行时动态生成被装饰类的派生类从而实现方法注入,但它并不负责为咱们建立该派生类的对象,因此我说它是一个半成品。

使用场景设想

好比说,咱们能够设想一下这样的场景:咱们须要执行某些须要验证身份的操做(例如删除文件、访问网络等), 咱们为这些操做分别设计了一个服务,并将这些服务注册到 IoC 中。因为身份验证这个动做是共同的,咱们能够将这个动做从业务代码中抽取出来,写到一个公共方法中。而后,当咱们在 IoC 中注册这些服务时,咱们能够指定将身份验证动做植入这些操做中,从而在运行时动态改变业务流程。

一点说明

目前来讲,这个框架只是我我的本身在用,并未开源。这篇文章也只是简单介绍一下这个框架,远远没有涉及到这个框架的设计思路,若是您对这个框架感兴趣的话,欢迎您在下面的评论区中提出问题共同讨论。

使用方法

我为这个框架写了一个简单的示例,您能够单击 此处 下载示例源码。至于这个框架的具体使用方法,且容我在之后有必要时再慢慢写出吧。

相关文章
相关标签/搜索