好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

对websharp中aspect的改进(待续)

对websharp中aspect的改进(待续)

缘起
    为了在我的 《朗志轻量级项目管理解决方案》 项目中应用AOP,考虑了多种实现方式,首先想到的是postsharp,无奈它是基于2.0的,要不然它是首选了,可惜我的项目是基于1.1,只好舍弃了。接下来就是Castle DynamicProxy,可是它需要虚的非 密封方法 ,这个要求让我很为难,也只好放弃了。最后选择了Remoting的实现方式,虽然它有百般的不是,但是对于我的这个项目而言,最重要的是可以解决我的问题,而且项目对性能效率几乎可以说是没有要求,查看了一下园子里关于使用Remoting实现aop的文章,那可不是一般的多哈,而且都贴出了相应的实现代码(注意是个“贴”字)可对于我这样的懒人,连粘代码调试的心情都没有。几经波折,发现了孙亚民先生的WebSharp这个东东,里面包含了一个基于Remoting的Aspect模块,对其做了良好的包装,而且说明的文档详尽,还是中文的,于是就在项目中使用它了。

问题
在WebSharp中提供了拦截类方式,如下

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
     < configSections >
< section  name ="Websharp.Aspects"  type ="Websharp.Aspect.AspectConfigHandler,Websharp.Aspect"   />
     </ configSections >
    
     < Websharp .Aspects >
         < Aspect  type ="WeaveTest.FirstAspect,WeaveTest"  deploy-model ="None"  
 pointcut-type ="Method|Construction|Property"  action-position ="Both"   match ="*,Get*"   />
     </ Websharp.Aspects >
</ configuration >


注意黄色的部分,就是原先的匹配方式,我想对项目中的一类方法进行横切,但是其中有几个是特例,再是我的方法的命名又没有什么规则,万般无奈之下动了改造的念头,我设想的是能够达到如下的效果

     < Langzhi .Aspects >
         < Aspect  type ="Langzhi.Security.Aspect.PermissionAspect,Langzhi.Security"  deploy-model ="Singleton"  pointcut-type ="Method"  action-position ="Before" >
             < Rule  type ="include"  match ="Langzhi\.PM\.BLL\.Facade\..*,.*"   />
             < Rule  type ="exclude"  match ="Langzhi\.PM\.BLL\.Facade\.UserManager,ValidLogin"   />
         </ Aspect >
         < Aspect  type ="Langzhi.Security.Aspect.AuditLogAspect,Langzhi.Security"  deploy-model ="Singleton"     pointcut-type ="Method"  action-position ="After" >
             < Rule  type ="include"  match ="Langzhi\.PM\.BLL\.Facade\..*,.*"   />
             < Rule  type ="exclude"  match ="Langzhi\.PM\.BLL\.Facade\.UserManager,ValidLogin"   />
         </ Aspect >
         < Aspect  type ="Langzhi.Security.Aspect.LoginAuditLogAspect,Langzhi.Security"  deploy-model ="Singleton"  pointcut-type ="Method"  action-position ="Both" >
             < Rule  type ="include"  match ="Langzhi\.PM\.BLL\.Facade\.UserManager,ValidLogin"   />
         </ Aspect >
         < Aspect  type ="Langzhi.Security.Aspect.LogoutAuditLogAspect,Langzhi.Security"  deploy-model ="Singleton"  pointcut-type ="Method"  action-position ="Both" >
             < Rule  type ="include"  match ="Langzhi\.PM\.BLL\.Facade\.UserManager,Logout"   />
         </ Aspect >
     </ Langzhi.Aspects >


改造

     /**/ ///   <summary>
     ///  读取Aspect配置文件的类
     ///  Class to get the configuration of Aspects. 
     ///   </summary>
     public   class  AspectConfigHandler : IConfigurationSectionHandler
     {
         /**/ ///   <summary>
         ///  由所有配置节处理程序实现,以分析配置节的 XML
         ///   </summary>
         ///   <param name="parent"> 对应父配置节中的配置设置 </param>
         ///   <param name="context"> 在从 ASP.NET 配置系统中调用 Create 时为 HttpConfigurationContext。否则,该参数是保留参数,并且为空引用。 </param>
         ///   <param name="section"> 一个 XmlNode,它包含配置文件中的配置信息。提供对配置节 XML 内容的直接访问。 </param>
         ///   <returns> 配置对象 </returns>
         object  IConfigurationSectionHandler.Create (  object  parent,  object  context,    XmlNode section )        
         {
             if  ( object .Equals(section, null ))
             {
                 throw ( new  ArgumentNullException());
            }
            XmlReader reader = new  XmlNodeReader(section);
            XmlRootAttribute xra = new  XmlRootAttribute();
            xra.ElementName = " Langzhi.Aspects " ;
            xra.IsNullable = false ;
            xra.DataType = " string " ;
            xra.Namespace = "" ;
            XmlSerializer xs = new  XmlSerializer( typeof (AspectCollection),xra);
             return     xs.Deserialize(reader);
        } }

配置文件变了,当然,读取的方式也得变了,原先WebSharp Aspect中采用的是操作xml的方式来读取节点,这里我们使用了反串行化的方式,还原对象的状态。这或许是一点小小的进步吧,但不知道效率怎么样了,不故用起来倒是挺爽的。

__________________________________________
朗志工作室:承接北京地区网站类项目

查看更多关于对websharp中aspect的改进(待续)的详细内容...

  阅读:42次