Aspect Oriented Programming面向切面编程
I简介
Aspect Oriented Programming(AOP),面向切面编程,是一个比较热门的话题。AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。比如我们最常见的就是权限验证,日志记录。
举个例子,我们现在提供例了获取数据的一个方法,但是我们希望并不是所有人都有权限调用。如果按照传统的OOP的实现的话,我们实现了获取数据获取数据,同时为了要进行权限验证的的话,那我们在实现该方法中要添加验证权限的代码。这样的话,假如我们要实现的功能有多个呢?那就要在每个实现的类都添加这些验证权限的代码。这样做的话就会有点繁琐,而且每个实现类都与验证权限的行为紧耦合,违反了面向对象的规则。那么怎样才能把验证权限的行为与业务处理过程中分离出来呢?看起来好像就是获取数据的方法自己在进行,但却是背后权限验证为这些行为进行验证,并且方法调用者都不知道存在这些权限验证过程,这就是我们要讨论AOP的目的所在。AOP的编程,好像就是把我们在某个方面的功能提出来与一批对象进行隔离,这样与一批对象之间降低了耦合性,可以就某个功能进行编程。
II代码实现:
1. 类标签声明
/// <summary>
/// 支持权限验证标签
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class PermissionCheckAttribute :ContextAttribute
{
public PermissionCheckAttribute()
: base("PermissionCheck")
{ }
public override voidGetPropertiesForNewContext(IConstructionCallMessage ccm)
{
ccm.ContextProperties.Add(newPermissionCheckProperty());
}
}
2. 验证属性声明
public class PermissionCheckProperty :IContextProperty,
IContributeObjectSink
{
#region IContributeObjectSinkimplementation
public IMessageSinkGetObjectSink(MarshalByRefObject o,
IMessageSink next)
{
return new SecurityAspect(next);
}
#endregion // IContributeObjectSinkimplementation
#region IContextProperty implementation
// Implement Name, Freeze,IsNewContextOK
public string Name
{
get { return"PermissionCheckProperty"; }
}
public void Freeze(Context newContext)
{
}
public bool IsNewContextOK(ContextnewCtx)
{
return true;
}
#endregion //IContextPropertyimplementation
}
3. 验证属性上下文声明
public class PermissionCheckProperty :IContextProperty,
IContributeObjectSink
{
#region IContributeObjectSinkimplementation
public IMessageSinkGetObjectSink(MarshalByRefObject o,
IMessageSink next)
{
return new SecurityAspect(next);
}
#endregion // IContributeObjectSinkimplementation
#region IContextProperty implementation
// Implement Name, Freeze,IsNewContextOK
public string Name
{
get { return"PermissionCheckProperty"; }
}
public void Freeze(Context newContext)
{
}
public bool IsNewContextOK(ContextnewCtx)
{
return true;
}
#endregion //IContextProperty implementation
}
4. 函数标签声明
/// <summary>
/// 功能描述标签类
/// </summary>
[AttributeUsage(AttributeTargets.All,AllowMultiple = false, Inherited = true)]
public class FeatureDescriptionAttribute :Attribute
{
/// <summary>
/// 功能编号
/// </summary>
public string Guid { get; set; }
/// <summary>
/// 功能描述
/// </summary>
public string Description { get; set; }
public FeatureDescriptionAttribute() {}
public FeatureDescriptionAttribute(stringname, string description)
{
this.Guid = name;
this.Description = description;
}
}
5.验证实现具体帮助类
/// <summary>
/// 权限验证帮助类
/// </summary>
public class PowerHelper
{
/// <summary>
/// 权限验证方法实现
/// </summary>
/// <param name="guid">功能编号</param>
/// <paramname="description">功能描述</param>
public static voidPermissionCheck(string guid, string description)
{
//TODO:此处查询数据库,做权限验证
if (guid =="04C4DFC7-9EDD-4A5D-9029-3EDCD5977163")
{
//拥有权限,正常
MessageBox.Show("权限检测通过");
}
else
{
//没有权限
throw newUnauthorizedAccessException("访问被拒绝,当前用户不具有操作此功能的权限!");
}
}
}
III具体使用例子
1. 功能函数声明
[PermissionCheck]
public class ControlDemoApi :ContextBoundObject
{
[FeatureDescription("04C4DFC7-9EDD-4A5D-9029-3EDCD5977163","功能1")]
public void Function1()
{
MessageBox.Show("成功的执行了功能1!");
}
[FeatureDescription("2FCFA71B-D492-4F88-8A75-985AC70BA161","功能2")]
public void Function2()
{
MessageBox.Show("成功的执行了功能2!");
}
}
2.调用功能函数
ControlDemoApi apiDemo =new ControlDemoApi(); apiDemo.Function1(); apiDemo. Function2();
3.说明
如2中描述,在执行apiDemo. Function1();之前会自动调用验证实现具体帮助类中的PermissionCheck(string guid, string description)方法。执行结果为 首先弹出消息权限检测通过,然后弹出消息 成功的执行了功能1!
如2中描述,在执行apiDemo. Function2 ();之前会自动调用验证实现具体帮助类中的PermissionCheck(stringguid, string description)方法。执行结果为 访问被拒绝,抛出异常 当前用户不具有操作此功能的权限!
Aspect Oriented Programming面向切面编程的更多相关文章
- Java实战之03Spring-03Spring的核心之AOP(Aspect Oriented Programming 面向切面编程)
三.Spring的核心之AOP(Aspect Oriented Programming 面向切面编程) 1.AOP概念及原理 1.1.什么是AOP OOP:Object Oriented Progra ...
- Aspect Oriented Programming
AOP(Aspect Oriented Programming),面向切面编程(也叫面向方面)是目前软件开发中的一个热点.利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度 ...
- Java笔记——面向切面编程(AOP模式)
原文:http://www.cnblogs.com/yanbincn/archive/2012/06/01/2530377.html Aspect Oriented Programming 面向切面 ...
- Spring(三)--AOP【面向切面编程】、通知类型及使用、切入点表达式
1.概念:Aspect Oriented Programming 面向切面编程 在方法的前后添加方法 2.作用:本质上来说是一种简化代码的方式 继承机制 封装方法 动 ...
- Spring AOP(面向切面编程)
一.AOP简介 1.AOP概念:Aspect Oriented Programming 面向切面编程 2.作用:本质上来说是一种简化代码的方式 继承机制 封装方法 动态代理 …… 3.情景举例 ①数学 ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
- Spring详解篇之 AOP面向切面编程
一.概述 Aop(aspect oriented programming面向切面编程),是spring框架的另一个特征.AOP包括切面.连接点.通知(advice).切入点(pointCut) . 1 ...
- AOP面向切面编程笔记
1.AOP概念:Aspect Oriented Programming 面向切面编程 2.作用:本质上来说是一种简化代码的方式 继承机制 封装方法 动态代理 …… 3.情景举例 ①数学计算器接口[Ma ...
- java aop面向切面编程
最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...
随机推荐
- .NET-架构优化实战-梳理篇
原文:.NET-架构优化实战-梳理篇 前言 程序员输出是他敲写的代码,那么输入就是他思考好的设计.因此不做设计是不存在,设计只分优秀的设计和糟糕的设计.为了避免过度设计浪费成本,需要针对现有业务与问题 ...
- javax.servlet.WriteListener
http://www.programcreek.com/java-api-examples/index.php?api=javax.servlet.WriteListener
- 最新国内外可用SVN托管仓库有哪些
最新国内外可用SVN托管仓库哪些 一.总结 一句话总结:用SVNBucket和SourceForge 二.最新国内外可用SVN托管仓库推荐 这几年很多SVN托管平台都基本不维护或者直接关闭了,我翻遍了 ...
- jQuery Mobile手机网站案例
jQuery Mobile手机网站案例 一.总结 一句话总结:jQuery Mobile是纯手机框架,和amazeui和bootstrap都可以做手机网站. 1.另一款文本编辑器? jd编辑器 二.j ...
- QWidget标题栏双击事件(QWidget::event里拦截NonClientAreaMouseButtonDblClick)
widget.h 1 virtual bool event(QEvent *event); widget.cpp bool Widget::event(QEvent *event) { if (eve ...
- push的时候隐藏底部的tabbar
push的时候隐藏底部的tabbar #import "mainNavigationControllers.h" @interface mainNavigationControll ...
- 【p093】细胞分裂
Time Limit: 1 second Memory Limit: 128 MB [问题描述] Hanks博士是BT(Bio-Tech,生物技术)领域的知名专家.现在,他正在为一个细胞实验做准备工作 ...
- SCM文章9类:外部中断示例程序
JP3遇见P0口,JP5遇见P3口,P1接受该发光二极管,什么时候P1所有的都是高时,,全亮度发光二极管.因为外部中断0和1用同样的方法.这里只是外部中断0计划. #include<reg51. ...
- 探究Promise的实现
最终答案在一个类库里,地址 https://github.com/yahoo/ypromise 这个类库也有问题,就是下面这道面试题在IE9里实现不一致,类库里还是用了setTimeout.去年尝试用 ...
- Linq知识小总结
一.投影操作符 Select Select操作符对单个序列或集合中的值进行投影. 返回 IEnumerable<类名> //查询语法 var query = from e in db.Em ...