C#当中利用Attribute实现简易AOP
来源:互联网 发布:直销管理平台系统源码 编辑:程序博客网 时间:2024/05/02 06:11
首先看一段简单的代码:
public partial class Form1 : Form{ public Form1() { InitializeComponent(); } //来自UI层的调用 private void button1_Click(object sender, EventArgs e) { BusinessHandler handler = new BusinessHandler(); handler.DoSomething(); }}//业务层的类和方法public class BusinessHandler{ public void DoSomething() { MessageBox.Show("执行了方法本身!"); }}
那么假设这是一个web程序,我们想在按钮点击时,先校验session,然后检验权限、起事务、写日志,然后再执行操作本身,最后再写日志。显然不能把对上面这些操作的方法调用都写在DoSomething()里,这样代码会变成一大坨屎,那我们该怎么办呢?
所谓AOP(面向切面编程)这个唬人的名词就是干这件事用的,其实现方式有很多种,比如利用Spring等框架,但是在实际项目中并不是想引一个框架进来就能随便引的,很多时候都需要我们自己手写一些机制。
这里想到了MVC当中的Filter,只要在Controller或者Action上打一个特性标签(Attribute),就能在方法执行前后做一些其他事情了。那么我们就来简单模拟一个Filter的实现吧。
首先给原先的方法改造一下,改成特性标签这种优雅的方式:
//业务层的类和方法,让它继承自上下文绑定类的基类[MyInterceptor]public class BusinessHandler : ContextBoundObject{ [MyInterceptorMethod] public void DoSomething() { MessageBox.Show("执行了方法本身!"); }}
有了特性标签自然就要有特性标签对应的类,以及AOP的实现方法,这些东西可以单独独立到一个文件或程序集里。
首先是贴在方法上的标签,实现为空:
//贴在方法上的标签[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]public sealed class MyInterceptorMethodAttribute : Attribute { }
之后定义贴在类上的标签:
//贴在类上的标签[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]public sealed class MyInterceptorAttribute : ContextAttribute, IContributeObjectSink{ public MyInterceptorAttribute() : base("MyInterceptor") { } //实现IContributeObjectSink接口当中的消息接收器接口 public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink next) { return new MyAopHandler(next); }}
这里定义了一个MyAopHandler类,实现如下:
//AOP方法处理类,实现了IMessageSink接口,以便返回给IContributeObjectSink接口的GetObjectSink方法public sealed class MyAopHandler : IMessageSink{ //下一个接收器 private IMessageSink nextSink; public IMessageSink NextSink { get { return nextSink; } } public MyAopHandler(IMessageSink nextSink) { this.nextSink = nextSink; } //同步处理方法 public IMessage SyncProcessMessage(IMessage msg) { IMessage retMsg = null; //方法调用消息接口 IMethodCallMessage call = msg as IMethodCallMessage; //如果被调用的方法没打MyInterceptorMethodAttribute标签 if (call == null || (Attribute.GetCustomAttribute(call.MethodBase, typeof(MyInterceptorMethodAttribute))) == null) { retMsg = nextSink.SyncProcessMessage(msg); } //如果打了MyInterceptorMethodAttribute标签 else { MessageBox.Show("执行之前"); retMsg = nextSink.SyncProcessMessage(msg); MessageBox.Show("执行之后"); } return retMsg; } //异步处理方法(不需要) public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink) { return null; }}
注意到上面的执行前后具体操作部分代码,相当于一个“代理类”,它实质上是改变了方法执行的上下文。可以用委托等面向对象程序结构把具体实现暴露给外部进行二次开发。
- C#当中利用Attribute实现简易AOP
- .net利用Attribute简单实现AOP
- C# 用Attribute实现AOP事务
- C# 用Attribute实现AOP事务
- C# 用Attribute实现AOP事务 [C# | AOP | Attribute | ContextAttribute | IContributeObjectSink | IMessageSink
- .NET里简易实现AOP
- .NET里简易实现AOP
- \t\tC# 用Attribute实现AOP事务
- AOP学习-基于Emit和Attribute的简单AOP实现
- C#实现简易计算器
- C# AOP实现
- C# AOP手动实现
- C#实现AOP框架
- 实现简易的AOP框架(一)
- 实现简易的AOP框架(二)
- 简易AOP
- spring当中的AOP
- C#简易线性表实现。
- 《观察者模式》
- 黑马程序员--10.网络编程--08.【C_S常见的“双卡”现象和解决--TCP复制文本文件示例II】【阻塞式循环的分析过程 ---总结】
- 注释:不是直接忽略
- Boost.Regex库 正则表达式
- windows xp下如何安装memcached
- C#当中利用Attribute实现简易AOP
- php学习记录 ajax 状态码
- ssi 引入文件找不到时,源码中的体现
- eclipse 使用server-Runtime Environment管理server
- cocos2d_x+lua【9】 ------tolua++使用(将2d_x自定义类导入lua)
- [iis7]请求筛选模块被配置为拒绝包含双重转义序列的请求。HTTP 错误 404.11 - Not Found
- Oracle中分区表的使用
- spring中自定义属性编辑器CustomEditorConfigurer
- Linux内核编译过程详解