[翻译]AOP编程
翻译文章链接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编程的更多相关文章
- 聊Javascript中的AOP编程
Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...
- 聊聊Javascript中的AOP编程
Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...
- 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
- 使用spring方式来实现aop编程
1:什么是aop? Aspect Oriented Programming 面向切面编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- Spring学习笔记之四----基于Annotation的Spring AOP编程
你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...
- 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--AOP编程
AOP编程在目前来说好像是大家都比较喜欢的.ASP.NET MVC中的Filter就是使用AOP实现的配置器模式.AOP在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...
- struts2.1笔记03:AOP编程和拦截器概念的简介
1.AOP编程 AOP编程,也叫面向切面编程(也叫面向方面):Aspect Oriented Programming(AOP),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用A ...
- Method Swizzling以及AOP编程:在运行时进行代码注入-备用
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
- Aop编程--注解与xml的实现
一.注解方式 1.首先引入spring对于aop编程的jar支持包,spring框架没有的包请自行在网上下载. aopalliance-alpha1.jar aspectjrt.jar aspectj ...
随机推荐
- flash Timer类使用
var timer:Timer = new Timer(1000); timer.addEventListener("timer",tracerTimer); function t ...
- cocos2d-x 获取图片的某像素点的RGBA颜色
转自:http://www.cnblogs.com/jaoye/archive/2013/02/19/2916501.html ccColor4B c = {, , , }; CCPoint pt = ...
- 继续推广我的新博客xysay:http://www.xysay.com/
RT 博客收拾了一下,准备以后就在那里记录论文笔记啦,求交流,求推荐,求友链~~~ http://www.xysay.com/
- 05---JSON学习(Java)
一.简介 JSON:JavaScript对象表示法(JavaScript object Notation) JSON是存储和文本交换信息的语法 JSON ...
- android学习日记21--消息提示之Toast和Notification
1.Toast Toast译为土司,类似切片面包,用于弹出比较快速的及时提示信息.当Toast被显示时,虽然它悬浮应用程序最上方,但是并未获得焦点.它的设计就是为了提示有用的信息,而不打扰用户其他操作 ...
- 同时使用ADO与Excel类库冲突的问题
客户需要一个Demo程序实现Access数据库表导出到Excel表格,并将表中存储的照片(OLE对象)以其中一个字段(编号)命名存储到本地.程序中引入了ADO操作Access数据库("C:\ ...
- 开源PaaS产品介绍
简介 本文主要介绍一下PaaS领域两个著名的开源软件: Cloud Foundry和OpenShift. [广告]如果你喜欢本博客,请点此查看本博客所有文章:http://www.cnblogs.co ...
- 九、Socket之TCP编程
TCP简介 TCP是Transmission Control Protocol(传输控制协议)的简称,是TCP/IP体系中面向连接的运输层协议,在网络中提供全双工的和可靠的服务. TCP最主要的特点: ...
- C#_dropdownlist_2
string deptId =Request.Form["depts"].Trim(); Html.DropDownList()赋默认值: 页面代码如下: <% ...
- 导入MyEclipse项目乱码
1.右键项目属性→text file encoding →修改为UTF-8,即可自动变成正常的.