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面向切面编程的更多相关文章

  1. Java实战之03Spring-03Spring的核心之AOP(Aspect Oriented Programming 面向切面编程)

    三.Spring的核心之AOP(Aspect Oriented Programming 面向切面编程) 1.AOP概念及原理 1.1.什么是AOP OOP:Object Oriented Progra ...

  2. Aspect Oriented Programming

    AOP(Aspect Oriented Programming),面向切面编程(也叫面向方面)是目前软件开发中的一个热点.利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度 ...

  3. Java笔记——面向切面编程(AOP模式)

    原文:http://www.cnblogs.com/yanbincn/archive/2012/06/01/2530377.html Aspect Oriented Programming  面向切面 ...

  4. Spring(三)--AOP【面向切面编程】、通知类型及使用、切入点表达式

    1.概念:Aspect Oriented Programming 面向切面编程 在方法的前后添加方法   2.作用:本质上来说是一种简化代码的方式      继承机制      封装方法      动 ...

  5. Spring AOP(面向切面编程)

    一.AOP简介 1.AOP概念:Aspect Oriented Programming 面向切面编程 2.作用:本质上来说是一种简化代码的方式 继承机制 封装方法 动态代理 …… 3.情景举例 ①数学 ...

  6. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  7. Spring详解篇之 AOP面向切面编程

    一.概述 Aop(aspect oriented programming面向切面编程),是spring框架的另一个特征.AOP包括切面.连接点.通知(advice).切入点(pointCut) . 1 ...

  8. AOP面向切面编程笔记

    1.AOP概念:Aspect Oriented Programming 面向切面编程 2.作用:本质上来说是一种简化代码的方式 继承机制 封装方法 动态代理 …… 3.情景举例 ①数学计算器接口[Ma ...

  9. java aop面向切面编程

    最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思.看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果.http://www.cnblog ...

随机推荐

  1. 新版Sublime text3注册码被移除的解决办法

    Sublime Text是风靡世界的文本编辑器,支持多种编程语言,启动时间短,打开文件速度快,插件丰富,让很多程序员爱不释手.但是,对于未注册的Sublime Text, 经常在保存的时候会弹出一个烦 ...

  2. Android XMPP服务器, BOSH(Http-Binding)和WEB客户端搭建

    目标: 搭建一个XMPP服务器, 实现在web page上用javascript与自己XMPP服务器通信, 匿名登录并与任何一个XMPP(Jabber)帐户通信. (Gtalk目前尚有问题) XMPP ...

  3. centos / Linux 服务环境下安装 Redis 5.0.3

    原文:centos / Linux 服务环境下安装 Redis 5.0.3 1.首先进入你要安装的目录 cd /usr/local 2.下载目前最新稳定版本 Redis 5.0.3 wget http ...

  4. 【codeforces 760C】Pavel and barbecue

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. leveldb学习:Versionedit和Versionset

    VersionEdit: compact过程中会有一系列改变当前Version的操作(FileNumber添加.删除input的sstable,添加输出的sstable).为了缩小version切换的 ...

  6. 深入理解AngularJs-scope(二)

    深入理解AngularJs-scope(一)中,我们对AngularJs的脏检测及其触发.异步任务队列进行了学习.紧接上一篇文章 深入理解AngularJs-scope(一),我们来看看scope对以 ...

  7. 学习web开发遇到几个细节问题

    1.在jsp中使用jsp表达式在input标签中时,避免直接和结束"/"相连 2.提取input select 标签内的内容,使用...value提取其值 3.form中含有一个o ...

  8. zookeeper 客户端操作

    代码 /** * 创建zk客户端 * 实现循环监听的两个必要条件:1.程序不能结束2.递归调用监听器 * @author tele * */ public class Demo { ; //多个节点用 ...

  9. git merge与rebase

    参考这篇文章 Git 之 merge 与 rebase 的区别  文章2 另外,使 rebase出现冲突后,先修改冲突,然后git add 某文件(我使用add  .经常有问题),然后git reba ...

  10. VS2017十五项新功能体验

    Visual Studio 2017十五项新功能体验 Visual Studio 2017正式已经于2017.3.7号正式发布,选在这一天发布也是为了纪念Visual Studio 二十周年.MVP ...