nuget 库地址:https://www.nuget.org/packages/czgl.aop/
github 库地址:https://github.com/whuanle/czgl.aop
czgl.aop 是 基于 emit 编写的 一个简单轻量的aop框架,支持非侵入式代理,支持.net core/asp.net core,以及支持多种依赖注入框架。
1,快速入门
czgl.aop 使用比较简单,你只需要使用 [interceptor]
特性标记需要代理的类型,然后使用继承 actionattribute
的特性标记要被代理的方法或属性。
1.1 继承 actionattribute 特性
actionattribute
是用于代理方法或属性的特性标记,不能直接使用,需要继承后重写方法。
示例如下:
public class logattribute : actionattribute { public override void before(aspectcontext context) { console.writeline("执行前"); } public override object after(aspectcontext context) { console.writeline("执行后"); if (context.ismethod) return context.methodresult; else if (context.isproperty) return context.propertyvalue; return null; } }
before
会在被代理的方法执行前或被代理的属性调用时生效,你可以通过 aspectcontext
上下文,获取、修改传递的参数。
after 在方法执行后或属性调用时生效,你可以通过上下文获取、修改返回值。
1.2 标记代理类型
在被代理的类型中,使用 [interceptor]
特性来标记,在需要代理的方法中,使用 继承了 actionattribute
的特性来标记。
此方法是侵入式的,需要在编译前完成。
[interceptor] public class test : itest { [log] public virtual string a { get; set; } [log] public virtual void mymethod() { console.writeline("运行中"); } }
注意的是,一个方法或属性只能设置一个拦截器。
2,如何创建代理类型
czgl.aop 有多种生成代理类型的方式,下面介绍简单的方式。
请预先创建如下代码:
public class logattribute : actionattribute { public override void before(aspectcontext context) { console.writeline("执行前"); } public override object after(aspectcontext context) { console.writeline("执行后"); if (context.ismethod) return context.methodresult; else if (context.isproperty) return context.propertyvalue; return null; } } public interface itest { void mymethod(); } [interceptor] public class test : itest { [log] public virtual string a { get; set; } public test() { console.writeline("构造函数没问题"); } [log] public virtual void mymethod() { console.writeline("运行中"); } }
2.1 通过api直接创建
通过 czgl.aop 中的 aopinterceptor
类,你可以生成代理类型。
示例如下:
itest test1 = aopinterceptor.createproxyofinterface<itest, test>(); test test2 = aopinterceptor.createproxyofclass<test>(); test1.mymethod(); test2.mymethod();
createproxyofinterface
通过接口创建代理类型;createproxyofclass
通过类创建代理类型;
默认调用的是无参构造函数。
3,创建代理类型
通过api
你可以参考源码解决方案
中的 exampleconsole 项目。
如果要直接使用 aopinterceptor.createproxyofinterface
和 aopinterceptor.createproxyofclass
方法,通过接口或类来创建代理类型。
itest test1 = aopinterceptor.createproxyofinterface<itest, test>(); test test2 = aopinterceptor.createproxyofclass<test>();
如果要指定实例化的构造函数,可以这样:
// 指定构造函数 test2 = aopinterceptor.createproxyofclass<test>("aaa", "bbb"); test2.mymethod();
通过 microsoft.extensions.dependencyinjection
microsoft.extensions.dependencyinjection
是 .net core/asp.net core 默认的依赖注入容器。
如果需要支持 asp.net core 中使用 aop,你可以在 nuget 包中安装 czgl.aop.medi
。
如果你在控制台下使用 microsoft.extensions.dependencyinjection
,你可以使用名为 buildaopproxy
的 iservicecollection
拓展方法来为容器中的类型,生成代理类型。
示例如下:
iservicecollection _services = new servicecollection(); _services.addtransient<itest, test>(); var serviceprovider = _services.buildaopproxy().buildserviceprovider(); serviceprovider.getservice<itest>(); return serviceprovider;
你可以参考源码解决方案中的 examplemedi
项目。
如果你要在 asp.net core 中使用,你可以在 startup
中,configureservices
方法的最后一行代码使用 services.buildaopproxy();
。
public void configureservices(iservicecollection services) { services.addcontrollers(); services.buildaopproxy(); }
还可以在 program
的 ihostbuilder
中使用 .useserviceproviderfactory(new aopserviceproxviderfactory())
来配置使用 czgl.aop。
示例:
public static ihostbuilder createhostbuilder(string[] args) => host.createdefaultbuilder(args) .useserviceproviderfactory(new aopserviceproxviderfactory()) .configurewebhostdefaults(webbuilder => { webbuilder.usestartup<startup>(); });
可以参考解决方案中的 exampleconsole
和 examplewebmedi
两个项目。
你不必担心引入 czgl.aop 后,使用依赖注入会使程序变慢或者破坏容器中的原有属性。czgl.aop 只会在创建容器时处理需要被代理的类型,不会影响容器中的服务,也不会干扰到依赖注入的执行。
通过 autofac
如果需要在 autofac 中使用 aop,则需要引用 czgl.aop.autofac
包。
如果你在控制台程序中使用 autofac,则可以在 build()
后面使用 buildaopproxy()
。
containerbuilder builder = new containerbuilder(); builder.registertype<test>().as<itest>(); var container = builder.build().buildaopproxy(); using (ilifetimescope scope = container.beginlifetimescope()) { // 获取实例 itest myservice = scope.resolve<itest>(); myservice.mymethod(); } console.readkey(); }
要注意的是,在已经完成的组件注册创建一个新的容器后,才能调用 buildaopproxy()
方法,
这样针对一个新的容器你可以考虑是否需要对容器中的组件进行代理。
如果在 asp.net core 中使用 autofac,你需要在 program 类的 ihostbuilder 中使用:
.useserviceproviderfactory(new autofacserviceproviderfactory())
如果需要代理已经注册的组件,则将其替换为:
.useserviceproviderfactory(new czgl.aop.autofac.aopserviceproxviderfactory())
请参考 源码解决方案中的 exampleautofac
和 examplewebautofac
两个项目。
4,深入使用
代理类型
要被代理的类型,需要使用 [interceptor]
来标记,例如:
[interceptor] public class test : itest { }
支持泛型类型。
被代理的类型必须是可被继承的。
类型的构造函数没有限制,你可以随意编写。
在使用 api 创建代理类型并且实例化时,你可以指定使用哪个构造函数。
例如:
string a="",b="",c=""; itest test1 = aopinterceptor.createproxyofinterface<itest, test>(a,b,c);
api 会根据参数的多少以及参数的类型自动寻找合适的构造函数。
方法、属性代理
为了代理方法或属性,你需要继承 actionattribute
特性,然后为方法或属性标记此特性,并且将方法或属性设置为 virtual
一个类型中的不同方法,可以使用不同的拦截器。
[log1] public virtual void mymethod1(){} [log2] public virtual void mymethod2(){}
对于属性,可以在属性上直接使用特性,或者只在 get 或 set 构造器使用。
[log] public virtual string a { get; set; } // 或 public virtual string a { [log] get; set; } // 或 public virtual string a { get; [log] set; }
如果在属性上使用特性,相当于 [log] get; [log] set;
。
上下文
一个简单的方法或属性拦截标记是这样的:
public class logattribute : actionattribute { public override void before(aspectcontext context) { console.writeline("执行前"); } public override object after(aspectcontext context) { console.writeline("执行后"); if (context.ismethod) return context.methodresult; else if (context.isproperty) return context.propertyvalue; return null; } }
aspectcontext 的属性说明如下:
字段 | 说明 |
---|---|
type | 当前被代理类型生成的代理类型 |
constructorparamters | 类型被实例化时使用的构造函数的参数,如果构造函数没有参数,则 methodvalues.length = 0,而不是 methodvalues 为 null。 |
isproperty | 当前拦截的是属性 |
propertyinfo | 当前被执行的属性的信息,可为 null。 |
propertyvalue | 但调用的是属性时,返回 get 的结果或 set 的 value 值。 |
ismethod | 当前拦截的是方法 |
methodinfo | 当前方法的信息 |
methodvalues | 方法被调用时传递的参数,如果此方法没有参数,则 methodvalues.length = 0,而不是 methodvalues 为 null |
methodresult | 方法执行返回的结果(如果有) |
拦截方法或属性的参数
通过上下文,你可以修改方法或属性的参数以及拦截返回结果:
public class logattribute : actionattribute { public override void before(aspectcontext context) { // 拦截并修改方法的参数 for (int i = 0; i < context.methodvalues.length; i++) { context.methodvalues[i] = (int)context.methodvalues[i] + 1; } console.writeline("执行前"); } public override object after(aspectcontext context) { console.writeline("执行后"); // 拦截方法的执行结果 context.methodresult = (int)context.methodresult + 664; if (context.ismethod) return context.methodresult; else if (context.isproperty) return context.propertyvalue; return null; } } [interceptor] public class test { [log] public virtual int sum(int a, int b) { console.writeline("运行中"); return a + b; } }
test test = aopinterceptor.createproxyofclass<test>(); console.writeline(test.sum(1, 1));
方法的参数支持 in
、ref
、out
;支持泛型方法泛型属性;支持异步方法;
非侵入式代理
此种方式不需要改动被代理的类型,你也可以代理程序集中的类型。
public class logattribute : actionattribute { public override void before(aspectcontext context) { console.writeline("执行前"); } public override object after(aspectcontext context) { console.writeline("执行后"); if (context.ismethod) return context.methodresult; else if (context.isproperty) return context.propertyvalue; return null; } }
public class testno { public virtual string a { get; set; } public virtual void mymethod() { console.writeline("运行中"); } }
testno test3 = aopinterceptor.createproxyoftype<testno>(new proxytypebuilder() .addproxymethod(typeof(logattribute), typeof(testno).getmethod(nameof(testno.mymethod))) .addproxymethod(typeof(logattribute), typeof(testno).getproperty(nameof(testno.a)).getsetmethod()));
通过 proxytypebuilder 来构建代理类型。
代理方法或属性都是使用 addproxymethod
,第一个参数是要使用的拦截器,第二个参数是要拦截的方法。
如果要拦截属性,请分开设置属性的 get
、set
构造。
如果多个方法或属性使用同一个拦截器,则可以这样:
testno test3 = aopinterceptor.createproxyoftype<testno>( new proxytypebuilder(new type[] { typeof(logattribute) }) .addproxymethod("logattribute", typeof(testno).getmethod(nameof(testno.mymethod))) .addproxymethod("logattribute", typeof(testno).getproperty(nameof(testno.a)).getsetmethod()));
testno test3 = aopinterceptor.createproxyoftype<testno>( new proxytypebuilder(new type[] { typeof(logattribute) }) .addproxymethod("logattribute", typeof(testno).getmethod(nameof(testno.mymethod))) .addproxymethod(typeof(logattribute2), typeof(testno).getproperty(nameof(testno.a)).getsetmethod()));
在构造函数中传递过去所需要的拦截器,然后在拦截时使用。
到此这篇关于菜渣开源一个基于 emit 的 aop 库(.net core)的文章就介绍到这了,更多相关emit 的 aop 库内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论