
























由于我最近参加雅思培训,一天到晚都在上课,所以一直没时间出来写blog。今天星期六我把aop资料整理了,做了例子,现在放上来,希望堆大家有帮助。
AOP是基于动态代理技术的。在学习AOP之前必须明白几个概念,这几个概念我将由配置AspectSharp(以下简称A#)的配置文件中一一引出,包括:Advice、Pointcut、Advister、Mixin 。
A#有自己独特的配置语言,当然也支持XML,但是新的配置语言我觉得比XML更加明了直观,而且也是非常容易使用的。 详细的官方文档在这里:http://www.castleproject.org/index.php/AspectSharp_Language_Documentation
1)配置必须按照以下顺序:
[Imports]
[Global Interceptor map]
[Global Mixin map]
Aspects definitions
2)[Imports]:引入命名空间,在下面的配置中用到的拦截器、混淆器所需要的。
Import Namespace.Name [in AssemblyName]
3)[Global Interceptor map]:如果你想在程序共享同一个拦截器而不想重复声明可以将Interceptor声明为全局,在同一配置文件中重用,而不用再次打长长的名称,用声明的别名就可以了。
interceptors [
"key" : InterceptorType ;
"key2" : InterceptorType2
]
4)[Global Mixin map] : 同样混淆器也可以声明为全局。
mixins [
"key" : MixinType ;
"key2" : MixinType2
]
5)Aspects definitions : 具体定义一个“切面(需要拦截的地方)”
aspect Name for Type
[include]
[pointcuts]
end
6)[include] :定义混淆器(mixin)组合的类
aspect MyAspect for Customer
include DigitalGravity.Mixins.Security in DigitalGravity.XProject
include System.Collections.ArrayList in System
end
7)[pointcuts] :拦截的具体名称,这里先指定拦截的类型并可以用通配符匹配名称。类型如下:
8)Advices :指定由哪个拦截器拦截
AOP-面向方面编程,定义这里我不想介绍太多,如果有兴趣的话,你可以来:http://www.wjshome.com/bbs/board.aspx?boardid=14看看aop的资料。那么AOP主要应用在哪些方面呢?具体来说,有:
Authentication--权限、Caching--缓存、Context passing--内容传递、Error handling--错误处理、Lazy loading--延迟加载、Debugging--调试、logging tracing profiling and monitoring--记录跟踪 优化 校准、Performance optimization--性能优化、Persistence--持久化、Resource pooling--资源池、Synchronization--同步、Transactions--事务……。
A#是CastleProject在.Net上实现AOP的一个框架,另外Spring.Net也有AOP的咚咚。 其实在Java里面,aop已经是很久以前的话题了,只是。net方面资料不如java多,用的人也相对少了。我下面将以一个简单的例子(参照官方改写),阐述A#在.net下面实现简单的“日志”和“权限检查”的AOP编程。
1)配置文件:
import AopDemo.Interceptors
import AopDemo.Mixins
aspect
log for [AopDemo]aspect Security
for [AopDemo]最上面两个import是引入命名空间,是LoggerInterceptor、SecurityCheckInterceptor和SecurityMixin的。我发现如果在同一个程序集中,不使用import也行,但是如果你的interceptor是单独的dll的话,应该必须import,我没有尝试。把interceptor独立出来的好处就是可以很方便的替换他。
接着是两个“切面”定义,第一个是定义所有的方法都会被LoggerInterceptor拦截,用于记录日志。上面讲到可以利用通配符来筛选拦截的方法名称,所以“*”就是代表所有的方法,又比如说:pointcut void Create(*)就是拦截返回类型为void,参数不限制的Create方法,void create(int id), void create(string name)这两个方法都会被拦截。第二个pointcut是加入了一个权限混淆器SecurityMixin,他的所有方法调用会被SecurityCheckInterceptor拦截。SecurityMixin的代码如下:
public class SecurityMixin : ISecurity, IProxyAware
{
private object proxy;public SecurityMixin()
{}#region ISecurity 成员public bool Access
{
get
{
// use proxy to do sth.
// eg: if (proxy is PersonDao)
return true;
}
}#endregion#region IProxyAware 成员public void SetProxy(object proxy)
{
this.proxy = proxy;
}#endregion
}
他实现了两个接口:ISecurity和IProxyAware。
ISecurity有一个Access属性,而实现IProxyAware是为了在mixin中使用代理对象。这样定义之后,在SecurityCheckInterceptor拦截的代理对象就是实现了ISecurity接口的对象,可以调用ISecurity接口中定义的方法了,而原来的对象是没有实现这个接口的。
使用的时候先加载配置初始化AspectEngine:
AspectLanguageEngineBuilder builder = new AspectLanguageEngineBuilder(File.OpenText(@"../../aspectsharp.cfg"));
AspectEngine engine = builder.Build();
然后通过这样来使用:
PersonDao dao = engine.WrapClass(typeof(PersonDao)) as PersonDao;
dao.Create("CCC");
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。