Richie

Sometimes at night when I look up at the stars, and see the whole sky just laid out there, don't you think I ain't remembering it all. I still got dreams like anybody else, and ever so often, I am thinking about how things might of been. And then, all of a sudden, I'm forty, fifty, sixty years old, you know?

AOP - AspectSharp 2.1.1.0

AspectSharp项目地址:AspectSharp - .NET AOP Framework
这个项目从05年开始已经没有维护了,估计现在已经没落,现在AOP用的比较多的应该是PostSharp,PostSharp使用上应该非常方便,但前段时间已经商业化运作了,仍提供社区版,但功能上有一些限制

AspectSharp现在网上较完整的例子比较少,从sourceforge下载项目源代码,里面有一个example项目,可以使用这个项目大致看一下AspectSharp的使用方式
因为下载的example项目运行时会出错,而AspectSharp使用的还是Castle.DynamicProxy.dll文件,我将AspectSharp做了点修改
1. 改成.Net Framework 3.5版本,因为新的Castle DynamicProxy使用的是3.5版本了
2. 改成使用Castle.DynamicProxy2.dll。主要是老的DynamicProxy中不少创建代理的方法、代理的使用方法都发生变化了,老的AspectSharp代码无法工作
修改之后,这篇文章中的例子以及AspectSharp中的Example项目都能正常运行,从这里下载附件可以运行这篇文章中的示例程序。但是因为新的DynamicProxy中拦截器接口机制的改变,无法确保我对AspectSharp的修改一定是正确的,因此这个下载仅用于演示目的

codeproject上面有篇.NET下开源AOP框架的对比文章:Rating of Open Source AOP Frameworks in .Net。AspectSharp在里面的评价很低,最好的是PostSharp,其次是Unity跟Spring.NET

引用命名空间:
using AopAlliance.Intercept;
using AspectSharp.Builder;
//测试用的目标类
public class RequestProcessor
{
    //需要是virtual的方法才能拦截
    public virtual void Process()
    {
        Console.WriteLine("Hello AspectSharp");
    }
}
//记录日志用的拦截器
public class LoggerInterceptor : IMethodInterceptor
{
    public object Invoke(IMethodInvocation invocation)
    {
        Console.WriteLine("Before {0} on {1}", invocation.Method.Name, invocation.Method.DeclaringType);
        object returnVal = invocation.Proceed();
        Console.WriteLine("After {0} on {1}", invocation.Method.Name, invocation.Method.DeclaringType);
        return returnVal;
    }
}
测试代码如下:
static void Main(string[] args)
{
    StandardCall();
    AopCall();
    Console.ReadKey();
}
//正常的调用,不使用AOP拦截
private static void StandardCall()
{
    RequestProcessor rp = new RequestProcessor();
    rp.Process();
    Console.WriteLine();
}
//使用AOP拦截后的调用
private static void AopCall()
{
    //AOP的拦截配置,这些配置可以放入配置文件中
    //import: 导入命名空间,比如拦截器所在的命名空间
    //aspect: 定义一个拦截的aspect, for指令用于指示需要拦截的类所在的命名空间
    //pointcut: 定义一个拦截点,使用method指定需要拦截的方法,还可以使用property指定需要拦截的属性,
    //    或者使用propertyread、propertywrite等用于拦截属性的getter、setter操作,
    //    被拦截的方法需要是virtual类型的,否则无法实现拦截(跟Castle Dynamic Proxy机制相关)
    //advice: 定义拦截器
    string config = @"
import AspectSharp.Test
aspect processor for [AspectSharp.Test] 
    pointcut method(* Process())
        advice(LoggerInterceptor)
    end
end";
    AspectLanguageEngineBuilder engineBuilder = new AspectLanguageEngineBuilder(config);
    AspectEngine engine = engineBuilder.Build();
    //使用AspectSharp包装,AspectSharp将根据配置生成代理对象,在代理对象上使用拦截器实现AOP拦截处理
    RequestProcessor rp = engine.WrapClass(typeof(RequestProcessor)) as RequestProcessor;
    rp.Process();
    Console.WriteLine();
}
运行结果如下:
    

从上面可以看到,感觉AspectSharp AOP唯一有点新意的地方就是使用了antlr来定义AOP的配置语言,其实使用动态代理,或者Post Compilation的方式自己实现一下拦截也是比较容易的事情。AspectSharp中使用AOP的地方也还得采取措施对AspectEngine进行包装,整体来看这个AOP对代码不是完全透明的 上面只是简单演示了一下AspectSharp的基本用法,因为使用了Castle.DynamicProxy实现AOP,因此AspectSharp也支持mixins等

posted on 2010-03-31 14:58  riccc  阅读(2647)  评论(0编辑  收藏  举报

导航