利用Attribute实现Aop
Aop“面向切面编程”,与OOP“面向对象编程”一样是一种编程思路。个人理解:在不改变原有逻辑的基础上,注入其他行为。
基础代码(仿MVC拦截器实现)
namespace HGL.Toolkit.Aop
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class AopAttribute : ContextAttribute, IContributeObjectSink
{
public AopAttribute() : base("Aop") { } //实现IContributeObjectSink接口当中的消息接收器接口
public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink next)
{
return new AopProxy(next);
}
} [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class AopMethodAttribute : Attribute
{
/// <summary>
/// 方法执行之前,执行代码
/// </summary>
public virtual bool OnMethodExecuting(System.Web.HttpContext context)
{
return true;
} /// <summary>
/// 方法执行之后,执行代码
/// </summary>
public virtual bool OnMethodExecuted(System.Web.HttpContext context)
{
return true;
}
}
} namespace HGL.Toolkit.Aop
{
public sealed class AopProxy : IMessageSink
{
//下一个接收器
private IMessageSink nextSink; public IMessageSink NextSink
{
get { return nextSink; }
} public AopProxy(IMessageSink nextSink)
{
this.nextSink = nextSink;
} //同步处理方法
public IMessage SyncProcessMessage(IMessage msg)
{
IMessage retMsg = null; //方法调用消息接口
IMethodCallMessage call = msg as IMethodCallMessage; var attributes = Attribute.GetCustomAttributes(call.MethodBase, typeof(AopMethodAttribute)); //如果被调用的方法没打AopMethodAttribute标签
if (call == null || attributes.Count() == 0)
{
retMsg = nextSink.SyncProcessMessage(msg);
}
//如果打了AopMethodAttribute标签
else
{
foreach (var attribute in attributes)
{
var obj = attribute.GetType(); var executing = obj.GetMethod("OnMethodExecuting");
if (executing != null)
{
var context=System.Web.HttpContext.Current;
executing.Invoke(attribute, new object[] { context });
}
} retMsg = nextSink.SyncProcessMessage(msg); foreach (var attribute in attributes)
{
var obj = attribute.GetType(); var executed = obj.GetMethod("OnMethodExecuted");
if (executed != null)
{
var context = System.Web.HttpContext.Current;
executed.Invoke(attribute, new object[] { context });
}
}
} return retMsg;
} //异步处理方法(不需要)
public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
{
return null;
}
}
}
使用注意:
1.使用到Aop的类必须继承ContextBoundObject
2.使用到Aop的方法存在的类必须添加[Aop]特性
3.哪个方法需要实现Aop,就给那个方法添加[AopMethod]特性 AopMethod未实现任何业务逻辑,使用者需要重写该attribute /// <summary>
/// 方法执行之前,执行代码
/// </summary>
public virtual bool OnMethodExecuting()
{
return true;
} /// <summary>
/// 方法执行之后,执行代码
/// </summary>
public virtual bool OnMethodExecuted()
{
return true;
}
待续......(将执行前和执行后的方法使用代理委托机制,实现二次开发)
利用Attribute实现Aop的更多相关文章
- C# Unity依赖注入利用Attribute实现AOP功能
使用场景? 很多时候, 我们定义一个功能, 当我们要对这个功能进行扩展的时候, 按照常规的思路, 我们一般都是利用OOP的思想, 在原有的功能上进行扩展. 那么有没有一种东西, 可以实现当我们需要扩展 ...
- C#.NET利用ContextBoundObject和Attribute实现AOP技术--AOP事务实现例子
我前两天看见同事用写了用AOP技术实现缓存的方案,于是好奇看了一下这是怎么实现的.原来是用了.NET中的一个类ContextBoundObject和Attribute相关技术.其实个类在.NET Fr ...
- 利用C#实现AOP常见的几种方法详解
利用C#实现AOP常见的几种方法详解 AOP面向切面编程(Aspect Oriented Programming) 是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 下面这篇文章主要 ...
- .Net core 2.0 利用Attribute获取MVC Action来生成菜单
最近在学习.net core的同时将老师的MVC5项目中的模块搬过来用,其中有一块就是利用Attribute来生成菜单. 一·首先定义Action实体 /// <summary> /// ...
- C#利用Attribute实现简易AOP介绍
首先看一段简单的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } //来自UI层的调用 ...
- C#当中利用Attribute实现简易AOP
首先看一段简单的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } //来自UI层的调用 ...
- C#利用Attribute实现简易AOP介绍 (转载)
地址:http://dotnet.9sssd.com/csbase/art/638 http://wayfarer.blog.51cto.com/1300239/279913 http://devel ...
- ssm+redis 如何更简洁的利用自定义注解+AOP实现redis缓存
基于 ssm + maven + redis 使用自定义注解 利用aop基于AspectJ方式 实现redis缓存 如何能更简洁的利用aop实现redis缓存,话不多说,上demo 需求: 数据查询时 ...
- 从Unity中的Attribute到AOP(七)
本章我们将依然讲解Unity中的Attribute,继续命名空间在UnityEngine里的. PropertyAttribute,这个特性主要来控制变量或者类在Inspector里面的显示方式.和P ...
随机推荐
- 矩阵乘法2(codevs3147)
题目描写叙述 Description 给出两个n*n的矩阵.m次询问它们的积中给定子矩阵的数值和. 输入描写叙述 Input Description 第一行两个正整数n,m. 接下来n行,每行n个非负 ...
- vue ---- 实现手机端(左滑 删除。右划 正常)
touchstart: // 手指放到屏幕上的时候触发 touchmove: // 手指在屏幕上移动的时候触发 touchend: // 手指从屏幕上拿起的时候触发 touchcancel: // 系 ...
- 59.C++与正则表达式
regex_match 整个字符串是否匹配 (通过cmatch存储匹配的结果),match[0]代表整个匹配序列,match[1]代表第1个匹配后的子序列,match[2]代表第2个匹配后的子序列 代 ...
- Kinect 开发 —— 姿势识别
姿势和手势通常会混淆,但是他们是两个不同的概念.当一个人摆一个姿势时,他会保持身体的位置和样子一段时间.但是手势包含有动作,例如用户通过手势在触摸屏上,放大图片等操作. 通常,游戏者很容易模仿指定姿势 ...
- 【JEECG技术博文】JEECG 简单实例解说权限控制
JEECG简单实例解说权限控制 请大家点击这里为我们投票.2015博客之星.很多其他分享敬请期待 博文地址:http://blog.itpub.net/30066956/viewspace-18687 ...
- SUSE Linux Enterprise Server 11 64T 安装(带清晰视频)
SUSE Linux Enterprise Server 11 64T 安装实录 650) this.width=650;" onclick='window.open("http: ...
- 很好的资源 for android
//texttospeach http://examples.javacodegeeks.com/android/core/text-to-speech/android-text-to-speech- ...
- Code froces 831 A. Unimodal Array
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- JSON.parse和eval()的区别
eval方法不检查给的字符串是否符合json的格式,parse会检查json语法格式. 比如一个json字符串data: { "a": 1, "b": &quo ...
- Ubuntu系统简介
1.ubuntu 远程连接 需要开启ssh 服务 sudo apt-get install openssh-server service ssh start|stop|restart 2.查看Linu ...