10分钟入门AOP:用PostSharp普及一下AOP

PostSharp是一个比较强调易学易用的AOP框架,在这之前我接触过AspectJ,Spring.当然花的时间也不多,不过一直感觉不好掌握,似乎AOP是比较难的东西。今天看到这篇英文论文里面的说明,似乎并不那么难以掌握。参考下图,对比一下两组概念。


PS用了我们熟知的东西,而没有引入新名词,这是它聪明的地方。看表格右边的一组概念,事件(还不太理解为什么说是meta),事件处理器,aspect=定制的attribute, 唯一新鲜的是multicasting,实际上可以理解为一个通配表达式。

下面再用一个简单例子:

public class LogAttribute : OnMethodBoundaryAspect
{
public override void OnEntry (MethodExecutionEventArgs eventArgs )
{  Console . WriteLine (" Entering the method {0}." , eventArgs . Method );
}

public override void OnExit (MethodExecutionEventArgs eventArgs )
{
  Console . WriteLine (
" Leaving the method {0}." , eventArgs . Method );
}

}


class Program
{
[Log]
static void Main ()
{
    Console . WriteLine (" Hello , world .");
}
}

//multicasting 说明对
MyApp . BusinessObjects命名空间使用Log

[ assembly : Log(
AttributeTargetTypes =
" MyApp . BusinessObjects .*" ,
AttributeTargetMemberAttributes =
MulticastAttributes . Public )]

从这例子看非常简明清晰,的确易学易用。


分享:practice makes perfect.
Tag标签: AOP,PostSharp
posted @ 2008-03-22 16:18 分享 阅读(865) 评论(3)  编辑 收藏

  回复  引用  查看    
#1楼 2008-03-22 16:57 | Justin      
不错!不错!
  回复  引用  查看    
#2楼 2008-03-23 11:29 | 代码乱了      
好文,简单明了
  回复  引用    
#3楼 2008-07-07 09:33 | dreamwinter [未注册用户]
PostSharp和Castle的Windsor之间有什么关系?请赐教!

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-03-22 16:45 编辑过


相关链接: