翻译文章链接http://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit


标题:利用拦截器(Interceptors)实现面向切面编程(AOP)

内容:

  1 介绍

    1.1 什么是AOP和方法拦截器

      1.1.1 手工实现(不采用AOP)

      1.1.2 AOP实现

    1.2 关于例程

  2 创建拦截器

  3 注册拦截器

-----------------------------------------------------------------

1 介绍

这篇文章会告诉你如何创建拦截器来实现AOP。例程使用了ABP作为应用框架,Castle Windsor作为拦截器库。本文中介绍的大部分方法适用于使用Castle Windsor在其他应用中。

-----------------------------------------------------------------

1.1 什么是AOP和方法拦截器

维基百科:面向侧面的程序设计(aspect-oriented programming,AOP,又译作面向方面的程序设计、观点导向编程、剖面导向程序设计)是计算机科学中的一个术语,指一种程序设计范型。该范型以一种称为侧面(aspect,又译作方面)的语言构造为基础,侧面是一种新的模块化机制,用来描述分散在对象、类或函数中的横切关注点(crosscutting concern)。

在应用程序中,我们会有重复或者相似的代码,比如日志、权限、验证、异常处理等......

-----------------------------------------------------------------

1.1.1 手工实现(不采用AOP)

采用手工实现的例程:

public class TaskAppService : ApplicationService
{
private readonly IRepository<Task> _taskRepository;
private readonly IPermissionChecker _permissionChecker;
private readonly ILogger _logger; public TaskAppService(IRepository<Task> taskRepository, IPermissionChecker permissionChecker, ILogger logger)
{
_taskRepository = taskRepository;
_permissionChecker = permissionChecker;
_logger = logger;
} public void CreateTask(CreateTaskInput input)
{
_logger.Debug("Running CreateTask method: " + input.ToJsonString()); try
{
if (input == null)
{
throw new ArgumentNullException("input");
} if (!_permissionChecker.IsGranted("TaskCreationPermission"))
{
throw new Exception("No permission for this operation!");
} _taskRepository.Insert(new Task(input.Title, input.Description, input.AssignedUserId));
}
catch (Exception ex)
{
_logger.Error(ex.Message, ex);
throw;
} _logger.Debug("CreateTask method is successfully completed!");
}
}

在CreateTask()方法中,核心代码是_taskRepository.Insert(...)方法的调用。其他都是重复代码,并且在TaskAppService类的其他方法中也会一样或类似。实际应用中,会有很多应用服务需要同样的功能。比如数据库连接打开、关闭,日志......

-----------------------------------------------------------------

1.1.2 AOP实现

如果使用了AOP和拦截器方式,TaskAppService类重写如下:

public class TaskAppService : ApplicationService
{
private readonly IRepository<Task> _taskRepository; public TaskAppService(IRepository<Task> taskRepository)
{
_taskRepository = taskRepository;
} [AbpAuthorize("TaskCreationPermission")]
public void CreateTask(CreateTaskInput input)
{
_taskRepository.Insert(new Task(input.Title, input.Description, input.AssignedUserId));
}
}

现在CreateTask()方法中不会有其他方法中重复的代码了。异常处理、验证、日志代码等对于所有方法都相似的代码都移除并可以集中实现。权限代码用AbpAuthorize属性代替。

幸好,这些都由ABP框架自动完成。但是你还需要根据自己的需求创建定制的拦截器逻辑。这也是这篇文章的宗旨。

-----------------------------------------------------------------

1.2 关于例程

例程来自ABP startup模板,可以在Github上找到。

-----------------------------------------------------------------

2 创建拦截器

创建一个简单的拦截器记录方法的执行时间:

using System.Diagnostics;
using Castle.Core.Logging;
using Castle.DynamicProxy; namespace InterceptionDemo.Interceptors
{
public class MeasureDurationInterceptor : IInterceptor
{
private readonly ILogger _logger; public MeasureDurationInterceptor(ILogger logger)
{
_logger = logger;
} public void Intercept(IInvocation invocation)
{
//Before method execution
var stopwatch = Stopwatch.StartNew(); //Executing the actual method
invocation.Proceed(); //After method execution
stopwatch.Stop();
_logger.InfoFormat(
"{0} executed in {1} milliseconds.",
invocation.MethodInvocationTarget.Name,
stopwatch.Elapsed.TotalMilliseconds.ToString("0.000")
);
}
}
}

拦截器是实现Castle Windsor中IInterceptor接口的类。定义了Intercept(IInvocation invocation)方法。用invocation参数可以调用方法、方法参数、返回值、方法类、程序及等。Intercept方法在注册方法调用时执行。Proceed()方法执行实际拦截的方法。可以像例程中那样在实际执行方法的前面、后面写代码。

拦截器类也可以依赖注入。例程中使用了构造器注入ILogger。

-----------------------------------------------------------------

3 注册拦截器

创建好拦截器后,就可以注册需要被拦截的类。比如,为所有的应用服务类方法注册MeasureDurationInterceptor。在ABP框架中很容易找出应用服务类,因为其均实现了IApplication接口。

当然也有其他的方法注册拦截器。但在ABP中可以处理Castle Windsors中Kernel的ComponentRegistered事件。

public static class MeasureDurationInterceptorRegistrar
{
public static void Initialize(IKernel kernel)
{
kernel.ComponentRegistered += Kernel_ComponentRegistered;
} private static void Kernel_ComponentRegistered(string key, IHandler handler)
{
if (typeof (IApplicationService).IsAssignableFrom(handler.ComponentModel.Implementation))
{
handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(MeasureDurationInterceptor)));
}
}
}

这样,一个类无论合适注册到依赖注入系统时,都可以处理这个事件,检查这个类是不是要拦截的类,并在满足条件时添加拦截器。

创建了注册代码后,需要在其他位置初始化方法。最好在模块的PreInitialize事件中(类一般在Initialize阶段注册到依赖注册系统):

public class InterceptionDemoApplicationModule : AbpModule
{
public override void PreInitialize()
{
MeasureDurationInterceptorRegistrar.Initialize(IocManager.IocContainer.Kernel);
} //...
}

最后,执行并登入到应用。查看日志:

INFO -- ::, [ ] .Interceptors.MeasureDurationInterceptor -
GetCurrentLoginInformations executed in , milliseconds.

文章由ABP作者在2016-02-23第一次发表在codeproject上,后续作者还将增加其他的一些用例。

关键字:ABP,.NET,AOP,Castle Windsor

[翻译]AOP编程的更多相关文章

  1. 聊Javascript中的AOP编程

    Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...

  2. 聊聊Javascript中的AOP编程

    Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...

  3. 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入

    概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...

  4. 使用spring方式来实现aop编程

    1:什么是aop? Aspect Oriented Programming 面向切面编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...

  5. Spring学习笔记之四----基于Annotation的Spring AOP编程

    你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...

  6. 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--AOP编程

    AOP编程在目前来说好像是大家都比较喜欢的.ASP.NET MVC中的Filter就是使用AOP实现的配置器模式.AOP在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...

  7. struts2.1笔记03:AOP编程和拦截器概念的简介

    1.AOP编程 AOP编程,也叫面向切面编程(也叫面向方面):Aspect Oriented Programming(AOP),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用A ...

  8. Method Swizzling以及AOP编程:在运行时进行代码注入-备用

    概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...

  9. Aop编程--注解与xml的实现

    一.注解方式 1.首先引入spring对于aop编程的jar支持包,spring框架没有的包请自行在网上下载. aopalliance-alpha1.jar aspectjrt.jar aspectj ...

随机推荐

  1. UVa123 - Searching Quickly

    题目地址:点击打开链接 C++代码: #include <iostream> #include <set> #include <map> #include < ...

  2. support-v4不能查看源码

    在Android实际开发过程中往往会遇到使用v4,v7或v13兼容包中的一些类如ViewPager,Fargment等,但却无法关联源码 具体步骤: 1 右击Android项目中libs文件夹下的an ...

  3. 剑指OFFER之丑数(九度OJ1214)

    题目描述: 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7.习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 输入: 输 ...

  4. H5页面请求跨域问题

    1.  <meta http-equiv="Access-Control-Allow-Origin" content="*"> 说明一下什么情况下我 ...

  5. Android v4 包和v7包问题

    昨天新建了一个android项目,加入了一个bootstrap的外部依赖和一个底部导航栏的外部依赖.结果jj 了,老是提醒我v4包v7包冲突: 事实是这样的,首先我的底部导航依赖库里面有一个v4包,那 ...

  6. iOS 时间戳转换为时间

    NSString *str=";//时间戳 NSTimeInterval time=[str doubleValue]+;//因为时差问题要加8小时 == 28800 sec NSDate ...

  7. C# Redis Server分布式缓存编程(二)

    在Redis编程中, 实体和集合类型则更加有趣和实用 namespace Zeus.Cache.Redis.Demo { public class Person { public int Id { g ...

  8. cdoj 92 Journey tarjan/lca 树上点对距离

    Journey Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/92 Descri ...

  9. [每日一题] 11gOCP 1z0-053 :2013-10-12 RESULT_CACHE在哪个池?.............................44

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12657479 正确答案:B   Oracle 11g 新特性:Result Cache , ...

  10. 写给新入IT的新人们

    IT=挨踢,这是IT人的自嘲,进入IT行业是有四五年了,也算得上是一个"老人"了吧,见了不少新人,面试了不少新人,也带了一些新人,多多少少还是有点发言权的. 关于书本 新人们经常会 ...