叶子的家

~●    ~●  ~●          ~●   ~●~●                           ○
    离成功还很远,距离长着叻,Fighting!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Castle实践8-AspectSharp

Posted on 2005-08-21 00:30  绿叶  阅读(6518)  评论(9编辑  收藏  举报
        由于我最近参加雅思培训,一天到晚都在上课,所以一直没时间出来写blog。今天星期六我把aop资料整理了,做了例子,现在放上来,希望堆大家有帮助。
        AOP是基于动态代理技术的。在学习AOP之前必须明白几个概念,这几个概念我将由配置AspectSharp(以下简称A#)的配置文件中一一引出,包括:Advice、Pointcut、Advister、Mixin  。
        A#有自己独特的配置语言,当然也支持XML,但是新的配置语言我觉得比XML更加明了直观,而且也是非常容易使用的。 详细的官方文档在这里:http://www.castleproject.org/index.php/AspectSharp_Language_Documentation

1)配置必须按照以下顺序:
[Imports]

[Global Interceptor map]

[Global Mixin map]

Aspects definitions

       
2)[Imports]:引入命名空间,在下面的配置中用到的拦截器、混淆器所需要的。
Import Namespace.Name [in AssemblyName]


3)[Global Interceptor map]:如果你想在程序共享同一个拦截器而不想重复声明可以将Interceptor声明为全局,在同一配置文件中重用,而不用再次打长长的名称,用声明的别名就可以了。
interceptors [ 
  
"key" : InterceptorType ; 
  
"key2" : InterceptorType2 
]


4)[Global Mixin map] : 同样混淆器也可以声明为全局。
mixins [ 
  
"key" : MixinType ; 
  
"key2" : MixinType2 
]



5)Aspects definitions : 具体定义一个“切面(需要拦截的地方)”

aspect Name for Type 
  [include]
  [pointcuts]
end


6)[include] :定义混淆器(mixin)组合的类
aspect MyAspect for Customer
  include DigitalGravity.Mixins.Security in DigitalGravity.XProject
  include System.Collections.ArrayList in System
end


7)[pointcuts] :拦截的具体名称,这里先指定拦截的类型并可以用通配符匹配名称。类型如下:
  • method :拦截方法的名称
  • property :拦截的属性名称
  • propertyread :拦截的读属性的名称
  • propertywrite:拦截的写属性的名称
    pointcut method|property(*)
    end

    pointcut method|propertyread(
    *)
    end

    pointcut propertywrite(
    *)
    end


    8)Advices :指定由哪个拦截器拦截

    aspect MyAspect for Customer
      pointcut method(
    *)
        advice(DigitalGravity.Interceptors.LogInvocationInterceptor in DigitalGravity.XProject)
      
    end
    end

            上面基本是官方的配置例子,那么这几个咚咚有什么关系,引用一句话“advice是你想向别的程序内部不同的地方注入的代码。pointcut定义了需要注入advice的位置,通常是某个特定的类的一个public方法。advisor是pointcut和advice的装配器,是将advice注入主程序中预定义位置的代码。”这句话是从java关于spring文章中截取出来的,原文在这里:http://www.wjshome.com/bbs/topic.aspx?topicid=209&page=65535,上面所讲的advice和pointcut的概念和这里是一样的,对于advisor我想是相对于上面讲的Aspects definitions 。这样大家就非常容易理解了吧。

    详细了解动态代理请看:http://iaxes.cnblogs.com/archive/2005/04/07/132868.html
    详细了解Maxin请看:http://iaxes.cnblogs.com/archive/2005/04/07/133407.html
    AOP基本资料请看:http://www.wjshome.com/bbs/topic.aspx?topicid=203


            AOP-面向方面编程,定义这里我不想介绍太多,如果有兴趣的话,你可以来:http://www.wjshome.com/bbs/board.aspx?boardid=14看看aop的资料。那么AOP主要应用在哪些方面呢?具体来说,有:
    Authentication--权限、Caching--缓存、Context passing--内容传递、Error handling--错误处理、Lazy loading--延迟加载、Debugging--调试、logging tracing profiling and monitoring--记录跟踪 优化 校准、Performance optimization--性能优化、Persistence--持久化、Resource pooling--资源池、Synchronization--同步、Transactions--事务……。
            A#是CastleProject在.Net上实现AOP的一个框架,另外Spring.Net也有AOP的咚咚。 其实在Java里面,aop已经是很久以前的话题了,只是。net方面资料不如java多,用的人也相对少了。我下面将以一个简单的例子(参照官方改写),阐述A#在.net下面实现简单的“日志”和“权限检查”的AOP编程。


    1)配置文件:

    import AopDemo.Interceptors
    import AopDemo.Mixins

    aspect 
    log for [AopDemo]
        pointcut method(
    *)
            advice(LoggerInterceptor)
        
    end
    end

    aspect Security 
    for [AopDemo]
        include SecurityMixin
        pointcut method(
    *)
            advice(SecurityCheckInterceptor)
        
    end
    end

    最上面两个import是引入命名空间,是LoggerInterceptor、SecurityCheckInterceptor和SecurityMixin的。我发现如果在同一个程序集中,不使用import也行,但是如果你的interceptor是单独的dll的话,应该必须import,我没有尝试。把interceptor独立出来的好处就是可以很方便的替换他。
    接着是两个“切面”定义,第一个是定义所有的方法都会被LoggerInterceptor拦截,用于记录日志。上面讲到可以利用通配符来筛选拦截的方法名称,所以“*”就是代表所有的方法,又比如说:pointcut void Create(*)就是拦截返回类型为void,参数不限制的Create方法,void create(int id), void create(string name)这两个方法都会被拦截。第二个pointcut是加入了一个权限混淆器SecurityMixin,他的所有方法调用会被SecurityCheckInterceptor拦截。SecurityMixin的代码如下:

    public class SecurityMixin : ISecurity, IProxyAware
    {
        
    private object proxy;

        
    public SecurityMixin()
        {
    }

        
    #region ISecurity 成员

        
    public bool Access
        {
            
    get
            {
                
    // use proxy to do sth.
                
    // eg: if (proxy is PersonDao)

                
    return true;
            }
        }

        
    #endregion

        
    #region IProxyAware 成员

        
    public void SetProxy(object proxy)
        {
            
    this.proxy = proxy;
        }

        
    #endregion
    }

    他实现了两个接口:ISecurity和IProxyAware。
    ISecurity有一个Access属性,而实现IProxyAware是为了在mixin中使用代理对象。这样定义之后,在SecurityCheckInterceptor拦截的代理对象就是实现了ISecurity接口的对象,可以调用ISecurity接口中定义的方法了,而原来的对象是没有实现这个接口的。

    使用的时候先加载配置初始化AspectEngine:

    AspectLanguageEngineBuilder builder = new AspectLanguageEngineBuilder(File.OpenText(@"../../aspectsharp.cfg"));
    AspectEngine engine 
    = builder.Build();

    然后通过这样来使用:

    PersonDao dao = engine.WrapClass(typeof(PersonDao)) as PersonDao;
    dao.Create(
    "CCC");

    Create方法将被拦截咯,作日志,安全检查,随便你~

    好了,就说到这吧,下载例子来调试调试咯:
    http://www.wjshome.com/Income/Others/Castle.AopDemo.rar
    注意例子中的PersonStore和PersonDao是模拟数据库的操作的,方便大家调试。bye~