Nuget 库地址:https://www.nuget.org/packages/CZGL.AOP/git
Github 库地址:https://github.com/whuanle/CZGL.AOPgithub
CZGL.AOP 是 基于 EMIT 编写的 一个简单轻量的AOP框架,支持非侵入式代理,支持.NET Core/ASP.NET Core,以及支持多种依赖注入框架。web
CZGL.AOP 使用比较简单,你只须要使用 [Interceptor]
特性标记须要代理的类型,而后使用继承 ActionAttribute
的特性标记要被代理的方法或属性。api
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
上下文,获取、修改传递的参数。ide
After 在方法执行后或属性调用时生效,你能够经过上下文获取、修改返回值。函数
在被代理的类型中,使用 [Interceptor]
特性来标记,在须要代理的方法中,使用 继承了 ActionAttribute
的特性来标记。ui
此方法是侵入式的,须要在编译前完成。代理
[Interceptor] public class Test : ITest { [Log] public virtual string A { get; set; } [Log] public virtual void MyMethod() { Console.WriteLine("运行中"); } }
注意的是,一个方法或属性只能设置一个拦截器。
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("运行中"); } }
经过 CZGL.AOP 中的 AopInterceptor
类,你能够生成代理类型。
示例以下:
ITest test1 = AopInterceptor.CreateProxyOfInterface<ITest, Test>(); Test test2 = AopInterceptor.CreateProxyOfClass<Test>(); test1.MyMethod(); test2.MyMethod();
CreateProxyOfInterface
经过接口建立代理类型;CreateProxyOfClass
经过类建立代理类型;
默认调用的是无参构造函数。
你能够参考源码解决方案
中的 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
是 .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 中使用 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
两个项目。
要被代理的类型,须要使用 [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()));
在构造函数中传递过去所须要的拦截器,而后在拦截时使用。