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. Unity3d优化包的大小

    http://wenku.baidu.com/link?url=MEUtNP6k1W7gXK2LcHdKXGqwoTD4HZDsBpsu9iFYjuL3WCIXgl2-rBHhBWP_zo5Xm4Yx ...

  2. 文本处理之可视化wordcloud

    什么是词云 词云又叫文字云,是对文本数据中出现频率较高的“关键词”在视觉上的突出呈现,形成关键词的渲染形成类似云一样的彩色图片,从而一眼就可以领略文本数据的主要表达意思. 准备工作: python开发 ...

  3. Apache DataFu: LinkedIn开源的Pig UDF库

    介绍 Apache DataFu分两部分,本文介绍的是其Pig UDF的部分.代码在Github上开源(除了代码外.也有一些slides介绍链接). DataFu里面是一些Pig的UDF.主要包含这些 ...

  4. tensorflow 的使用流程

    1. optimizer.minimize 与 global_step optimizer = tf.train.**(learning_rate) global_step = tf.Variable ...

  5. 【9005】最短网络agrinet

    Time Limit: 1 second Memory Limit: 256 MB 问题描述 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的 ...

  6. Thermal management in a gaming machine

    BACKGROUND OF THE INVENTION 1. Field of the Invention The present invention relates to wager gaming ...

  7. Crystal Report - 水晶报表导出文件的格式设置

    水晶报表中自带的导出和打印功能用起来确实很方便,只不过有时候需要导出的文件并不需要那么多种类型,在网上找到一些朋友的代码总结了一下,可以通过代码实现自定义导出文件类型 首先需要定义一个枚举: publ ...

  8. An HTTP & HTTP/2 client for Android and Java applications OkHttp

    HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP effic ...

  9. .net remoting 使用事件

    原文:.net remoting 使用事件 在RPC如果需要使用事件,相对是比较难的.本文告诉大家如何在 .net remoting 使用事件. 目录 使用 Channel 序列化 开发建议 修复异常 ...

  10. jquery 源码学习(四)构造jQuery对象-工具函数

    jQuery源码分析-03构造jQuery对象-工具函数,需要的朋友可以参考下.   作者:nuysoft/高云 QQ:47214707 EMail:nuysoft@gmail.com 声明:本文为原 ...