利用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 ...
随机推荐
- BRep Shapes Based on Tessellated Geometry
BRep Shapes Based on Tessellated Geometry eryar@163.com Key Words. BRep Shape, Tessellated Geometry, ...
- jQuery源码06-jQuery = function(){};给JQ对象,添加一些方法和属性,extend : JQ的继承方法,jQuery.extend()
/*! * Includes Sizzle.js 选择器,独立的库 * http://sizzlejs.com/ */ (function( window, undefined ) { //" ...
- elasticsearch index 之 create index(二)
创建索引需要创建索引并且更新集群index matedata,这一过程在MetaDataCreateIndexService的createIndex方法中完成.这里会提交一个高优先级,AckedClu ...
- 圆形头像CircleImageView和Cardview使用
效果: 圆形头像在我们的日常使用的app中很常见,因为圆形的头像比较美观. 使用圆形图片的方法可能有我们直接将图片裁剪成圆形再在app中使用, 还有就是使用自定义View对我们设置的任何图片自动裁剪成 ...
- Android java.lang.NoSuchFieldError: No static field xxx of type I in class Lcom/XX/R$id; or its superclasses
项目开发快到尾声,突然发现之前一个模块莫名其妙的奔溃了,我的内心也是奔溃的.以前一直都是好好的,也没去动过它,为啥会出现这样的问题呢? 下面我会根据自己的理解来看待问题 android是怎么根据id查 ...
- Atcoder AGC 019 A,B
A - Ice Tea Store Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You' ...
- marquee
marquee: 页面的自动滚动效果,可由javascript来实现,但是今天无意中发现了一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需 ...
- Interrupt distribution scheme for a computer bus
A method of handling processor to processor interrupt requests in a multiprocessing computer bus env ...
- C++里面virtual函数及虚表大小
实验了下面的函数: #include <vector> #include <iostream> using namespace std; class A { public: v ...
- excel表如何实现多if选择结构多分支判断
excel表如何实现多if选择结构多分支判断 一.总结 一句话总结:把多if分支转换成单if分支相加. 也可以if分支,也可以lookup函数. 1.CHOICE: +2 if band A; +1 ...