【IT168技术文档】
使用Castle实现AOP开发
Castle包含了一个名为Aspect#的AOP框架,Aspect#是基于Castle动态代理技术来实现的。Aspect#于2005年6月被收录为Castle的其中一个子项目。它是针对CLI(.Net和Mono)实现的AOP框架,利用了反射、代理等机制。
Aspect#的基本用法为:
(1)配置必须按照以下顺序:
(2)[Imports]:引入命名空间,在下面的配置中用到的拦截器、混淆器所需要的。[Imports] [Global Interceptor map] [Global Mixin map] Aspects definitions
(3)[Global Interceptor map]:如果你想在程序共享同一个拦截器而不想重复声明可以将Interceptor声明为全局,在同一配置文件中重用,而不用再次打长长的名称,用声明的别名就可以了。Import Namespace.Name [in AssemblyName]
(4)[Global Mixin map] : 同样混淆器也可以声明为全局。interceptors [ "key" : InterceptorType ; "key2" : InterceptorType2 ]
(5)Aspects definitions :具体定义一个“切面(需要拦截的地方)”mixins [ "key" : MixinType ; "key2" : MixinType2 ]
(6)[include] :定义混淆器(mixin)组合的类aspect Name for Type [include] [pointcuts] end
(7)[pointcuts] :拦截的具体名称,这里先指定拦截的类型并可以用通配符匹配名称。类型如下:aspect MyAspect for Customer include DigitalGravity.Mixins.Security in DigitalGravity.XProject include System.Collections.ArrayList in System end
method :拦截方法的名称 property :拦截的属性名称 propertyread :拦截的读属性的名称 propertywrite:拦截的写属性的名称 pointcut method|property(*) end pointcut method|propertyread(*) end pointcut propertywrite(*) end