AOP框架Dora.Interception 3.0 [2]: 实现原理
和所有的AOP框架一样,我们必须将正常的方法调用进行拦截,才能将应用到当前方法上的所有拦截器纳入当前调用链。Dora.Interception采用IL Eimit的方式实现对方法调用的拦截,接下来我们就来聊聊大致的实现原理。
一、与依赖注入框架的无缝集成
由于Dora.Interception是为.NET Core定制的AOP框架,而依赖注入是.NET Core基本的编程方式,所以Dora.Interception最初就是作为一个依赖注入框架的扩展而涉及的。我们知道.NET Core的依赖注入框架支持三种服务实例提供方式。由于Dora.Interception最终会利用IL Emit的方式动态生成目标实例的类型,所以它只适合第一种服务注册方式。
- 如果注册的是一个服务类型,最终会选择一个匹配的构造函数来创建服务实例;
- 如果注册的是一个服务实例创建工厂,那么目标服务实例就由该工厂来创建;
- 如果注册的是一个服务实例,那么它会直接作为目标服务实例。
二、两种拦截方式
.NET Core的依赖注入框架采用ServiceDescriptor对象来描述服务注册。拦截器最终会注册到ImplementationType 属性表示的实现类型上,所以Dora.Interception需要根据该类型生成一个可以被拦截的代理类型。针对ServiceType属性表示的服务类型的不同,我们会采用不同的代码生成方式。
针对接口
如果注册服务时提供的是一个接口和它的实现类型,我们会按照如下的方式来生成可被拦截的代理类型。假设接口和实现类型分别为IFoobar和Foobar,那么我们会生成一个同样实现IFoobar接口的FoobarProxy类型。FoobarProxy对象是对Foobar对象的封装,对于它实现的方法来说,如果没有拦截器应用到Foobar类型对应的方法上,它只需要调用封装的这个Foobar对象对应的方法就可以了。反之,针对拦截器的调用将会注入到FoobarProxy实现的方法中。
针对类型
如果注册是提供的服务类型并不是一个接口,而是一个类型,比如服务类型和实现类型都是Foobar,上述的代码生成机制就不适用了。此时我们要求Foobar必须是一个非封闭(Sealed)的类型,而且拦截器只能应用到它的虚方法上。基于这种假设,我们生成的代理类型FoobarProxy实际上市Foobar的子类,如果拦截器应用到Foobar的某个虚方法上,FoobarProxy只需要重写这个方法将应用的拦截器注入到方法调用管道中。
三、ICodeGenerator & ICodeGeneratorFactory
上述针对IL Emit的动态代理类型生成体现在如下这个ICodeGenerator接口上,该接口唯一的方法GenerateInterceptableProxyClass会根据提供的上下文信息生成可被拦截的代理类型。作为代码生成上下文的的CodeGenerationContext对象来说,它除了提供服务注册的类型和实现类型之外,它还提供了IInterceptorRegistry对象。
public interface ICodeGenerator { Type GenerateInterceptableProxyClass(CodeGenerationContext context); } public class CodeGenerationContext { public Type InterfaceOrBaseType { get; } public Type TargetType { get; } public IInterceptorRegistry Interceptors { get; } public CodeGenerationContext(Type baseType, IInterceptorRegistry interceptors ); public CodeGenerationContext(Type @interface, Type targetType, IInterceptorRegistry interceptors); }
IInterceptorRegistry接口在Dora.Interception中表示某个类型针对拦截器的注册。它的IsEmpty表示拦截器是否应用到目标类型的任意成员中;IsInterceptable方法帮助我们确定指定的方法是否应用了拦截器;应用到某个方法的所有拦截器可以通过GetInterceptor方法提取出来。
public interface IInterceptorRegistry { bool IsEmpty { get; } InterceptorDelegate GetInterceptor(MethodInfo methodInfo); bool IsInterceptable(MethodInfo methodInfo); MethodInfo GetTargetMethod(MethodInfo methodInfo); }
如果我们需要得到针对某个类型的IInterceptorRegistry对象,可以调用IInterceptorResolver接口的如下两个GetInterceptors方法重载。
public interface IInterceptorResolver { IInterceptorRegistry GetInterceptors(Type initerfaceType, Type targetType); IInterceptorRegistry GetInterceptors(Type targetType); }
与代码生成相关的还具有如下这个ICodeGeneratorFactory接口,它是创建ICodeGenerator的工厂。
四、ServiceDescriptor的转换
由于服务实例最终是通过依赖注入框架提供的,而最终得到怎样的服务实例则由最初的服务注册决定。为了让依赖注入框架能够提供一个可被拦截的代理对象,而不是原始的目标对象,我们必须改变初始的服务注册,为此我们定义了如下这个InterceptableServiceDescriptor。如下面的代码片段所示,InterceptableServiceDescriptor实际是一个基于工厂的ServiceDescriptor,创建代理对象的逻辑体现在GetImplementationFactory方法返回Func<IServiceProvider, object>对象上。
public sealed class InterceptableServiceDescriptor : ServiceDescriptor, IInterceptableServiceDescriptor { private readonly Type _targetType; : base(serviceType, GetImplementationFactory(serviceType, implementationType), lifetime) { if (serviceType.IsGenericTypeDefinition) { throw new ArgumentException("Open generic type (generic type definition) is not support", nameof(serviceType)); } _targetType = implementationType; } Type IInterceptableServiceDescriptor.TargetType => _targetType; private static Func<IServiceProvider, object> GetImplementationFactory(Type serviceType, Type implementationType) { return serviceProvider => { var interceptorResolver = serviceProvider.GetRequiredService<IInterceptorResolver>(); var codeGeneratorFactory = serviceProvider.GetRequiredService<ICodeGeneratorFactory>(); var factoryCache = serviceProvider.GetRequiredService<IInterceptableProxyFactoryCache>(); if (serviceType.IsInterface) { var interceptors = interceptorResolver.GetInterceptors(serviceType, implementationType); if (interceptors.IsEmpty) { return ActivatorUtilities.CreateInstance(serviceProvider, implementationType); } else { var target = ActivatorUtilities.CreateInstance(serviceProvider, implementationType); return factoryCache.GetInstanceFactory(serviceType, implementationType).Invoke(target); } } else { var interceptors = interceptorResolver.GetInterceptors(implementationType); if (interceptors.IsEmpty) { return ActivatorUtilities.CreateInstance(serviceProvider, implementationType); } else { return factoryCache.GetTypeFactory(implementationType).Invoke(serviceProvider); } } }; } }
我们可以利用提供的如下的扩展方法直接创建InterceptableServiceDescriptor 对象作为服务注册。
public static class AddInterceptionExtensions { public static IServiceCollection AddInterceptable(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime lifetime); public static IServiceCollection AddTransientInterceptable(this IServiceCollection services, Type serviceType, Type implementationType); public static IServiceCollection AddScopedInterceptable(this IServiceCollection services, Type serviceType, Type implementationType); public static IServiceCollection AddSingletonInterceptable(this IServiceCollection services, Type serviceType, Type implementationType); public static IServiceCollection AddInterceptable<TService, TImplementation>(this IServiceCollection services, ServiceLifetime lifetime); public static IServiceCollection AddTransientInterceptable<TService, TImplementation>(this IServiceCollection services); public static IServiceCollection AddScopedInterceptable<TService, TImplementation>(this IServiceCollection services); public static IServiceCollection AddSingletonInterceptable<TService, TImplementation>(this IServiceCollection services); public static IServiceCollection TryAddInterceptable(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime lifetime); public static IServiceCollection TryAddTransientInterceptable(this IServiceCollection services, Type serviceType, Type implementationType); public static IServiceCollection TryAddScopedInterceptable(this IServiceCollection services, Type serviceType, Type implementationType); public static IServiceCollection TryAddSingletonInterceptable(this IServiceCollection services, Type serviceType, Type implementationType); public static IServiceCollection TryAddInterceptable<TService, TImplementation>(this IServiceCollection services, ServiceLifetime lifetime); public static IServiceCollection TryAddInterceptable<TService, TImplementation>(this IServiceCollection services); public static IServiceCollection TryAddScopedInterceptable<TService, TImplementation>(this IServiceCollection services); public static IServiceCollection TryAddSingletonInterceptable<TService, TImplementation>(this IServiceCollection services); public static IServiceCollection TryAddEnumerableInterceptable(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime lifetime); public static IServiceCollection TryAddEnumerableInterceptable<TService, TImplementation>(this IServiceCollection services, ServiceLifetime lifetime) }
五、另一种改变服务注册的方式
如果我们依然希望采用默认提供的服务注册API,那么我们可以将服务注册的转换实现在利用IServiceCollection集合创建IServiceProvider对象的时候,为此我们定义了如下这个BuildInterceptableServiceProvider扩展方法。顺便说一下,另一个AddInterception扩展方法用来注册Dora.Interception框架自身的一些核心服务。BuildInterceptableServiceProvider方法内部会调用这个方法,如果没有采用这种方式来创建IServiceProvider对象,AddInterception扩展方法必须显式调用。
public static class ServiceCollectionExtensions { public static IServiceProvider BuildInterceptableServiceProvider(this IServiceCollection services, Action<InterceptionBuilder> configure = null); public static IServiceCollection AddInterception(this IServiceCollection services, Action<InterceptionBuilder> configure = null); }
六、InterceptableServiceProviderFactory
.NET Core依赖注入框架利用自定义的IServiceProviderFactory<TContainerBuilder>实现与第三方依赖注入框架的整合。如下这个的InterceptableServiceProviderFactory是我们为Dora.Interception定义的实现类型。
public sealed class InterceptableServiceProviderFactory : IServiceProviderFactory<IServiceCollection> { public InterceptableServiceProviderFactory(ServiceProviderOptions options, Action<InterceptionBuilder> configure); public IServiceCollection CreateBuilder(IServiceCollection services); public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder); }
为了在服务承载应用(含ASP.NET Core应用)更好地使用Dora.Interception,可以调用我们为IHostBuilder定义的UseInterceptableServiceProvider扩展方法,该方法会帮助我们完成针对InterceptableServiceProviderFactory的注册。
public static class HostBuilderExtensions { public static IHostBuilder UseInterceptableServiceProvider(this IHostBuilder builder,ServiceProviderOptions options = null,Action<InterceptionBuilder> configure = null); }
我们在《AOP框架Dora.Interception 3.0 [1]: 编程体验》提供的演示程序(如下所示)正是调用了这个UseInterceptableServiceProvider方法。
public class Program { public static void Main(string[] args) { Host.CreateDefaultBuilder() .UseInterceptableServiceProvider(configure: Configure) .ConfigureWebHostDefaults(buider => buider.UseStartup<Startup>()) .Build() .Run(); static void Configure(InterceptionBuilder interceptionBuilder) { interceptionBuilder.AddPolicy(policyBuilder => policyBuilder .For<CacheReturnValueAttribute>(order: , cache => cache .To<SystemClock>(target => target .IncludeMethod(clock => clock.GetCurrentTime(default))))); } } }
AOP框架Dora.Interception 3.0 [1]: 编程体验
AOP框架Dora.Interception 3.0 [2]: 实现原理
AOP框架Dora.Interception 3.0 [3]: 拦截器设计
AOP框架Dora.Interception 3.0 [4]: 基于特性的拦截器注册
AOP框架Dora.Interception 3.0 [5]: 基于策略的拦截器注册
AOP框架Dora.Interception 3.0 [6]: 自定义拦截器注册方式
AOP框架Dora.Interception 3.0 [2]: 实现原理的更多相关文章
- AOP框架Dora.Interception 3.0 [1]: 编程体验
.NET Core正式发布之后,我为.NET Core度身定制的AOP框架Dora.Interception也升级到3.0.这个版本除了升级底层类库(.NET Standard 2.1)之外,我还对它 ...
- AOP框架Dora.Interception 3.0 [5]: 基于策略的拦截器注册方式
注册拦截器旨在解决如何将拦截器应用到目标方法的问题.在我看来,针对拦截器的注册应该是明确而精准的,也就是我们提供的注册方式应该让拦截器准确地应用到期望的目标方法上,不能多也不能少.如果注册的方式过于模 ...
- AOP框架Dora.Interception 3.0 [4]: 基于特性的拦截器注册
按照单一职责的原则,拦截器只负责需要的拦截操作的执行,至于它采用何种方式应用到目标方法上,以及它在整个拦截器管道中的位置则属于“拦截器注册”的范畴.Dora.Interception提供了几种典型的注 ...
- AOP框架Dora.Interception 3.0 [3]: 拦截器设计
对于所有的AOP框架来说,多个拦截器最终会应用到某个方法上.这些拦截器按照指定的顺序构成一个管道,管道的另一端就是针对目标方法的调用.从设计角度来将,拦截器和中间件本质是一样的,那么我们可以按照类似的 ...
- 全新升级的AOP框架Dora.Interception[1]: 编程体验
多年之前利用IL Emit写了一个名为Dora.Interception(github地址,觉得不错不妨给一颗星)的AOP框架.前几天利用Roslyn的Source Generator对自己为公司写的 ...
- 全新升级的AOP框架Dora.Interception[2]: 基于“约定”的拦截器定义方式
Dora.Interception有别于其他AOP框架的最大的一个特点就是采用针对"约定"的拦截器定义方式.如果我们为拦截器定义了一个接口或者基类,那么拦截方法将失去任意注册依赖服 ...
- 全新升级的AOP框架Dora.Interception[3]: 基于特性标注的拦截器注册方式
在Dora.Interception(github地址,觉得不错不妨给一颗星)中按照约定方式定义的拦截器可以采用多种方式注册到目标方法上.本篇文章介绍最常用的基于"特性标注"的拦截 ...
- 全新升级的AOP框架Dora.Interception[6]: 框架设计和实现原理
本系列前面的五篇文章主要介绍Dora.Interception(github地址,觉得不错不妨给一颗星)的编程模式以及对它的扩展定制,现在我们来聊聊它的设计和实现原理.(拙著<ASP.NET C ...
- 全新升级的AOP框架Dora.Interception[4]: 基于Lambda表达式的拦截器注册方式
如果拦截器应用的目标类型是由自己定义的,Dora.Interception(github地址,觉得不错不妨给一颗星)可以在其类型或成员上标注InterceptorAttribute特性来应用对应的拦截 ...
随机推荐
- FreeSql (二十四)Linq To Sql 语法使用介绍
原本不支持 IQueryable 主要出于使用习惯的考虑,如果继承 IQueryable,编写代码的智能总会提示出现一堆你不想使用的方法(对不起,我有强迫症),IQueryable 自身提供了一堆没法 ...
- linux非root用户下安装软件,搭建生产环境
之前的用实验室的服务器,因为某些原因,使用的用户没有root权限.linux的非root用户很多软件无法安装,非常的不方便.我的方法是使用brew来代替系统的包管理工具.brew是最先用在mac上的包 ...
- localStorage详细总结
一.localStorage简介: 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cookie中每条cooki ...
- Tomcat类加载器体系结构
<深入理解java虚拟机>——Tomcat类加载器体系结构 标签: java / 虚拟机 / tomcat Tomcat 等主流Web服务器为了实现下面的基本功能,都实现了不止一个自定义的 ...
- MOOC C++笔记(三):类和对象提高
第三周:类和对象提高 this指针 作用 this指针作用就是指向成员函数所作用的对象. 非静态成员函数中可以直接使用this来代表指向该函数作用的指针. 成员函数中默认有一个this指针指向当前对象 ...
- 59 (OC)* atomic是否绝对安全
场景:如今项目中有这样一个场景,在一个自定义类型的Property在一个线程中改变的同时也要同时在另一个线程中使用它,使我不得不将Property定义成atomic,但是由此发现atomic并不会保证 ...
- ReactNative之Redux详解
用redux有一段时间了,感觉还是有必要把其相关的知识点系统的总结一下的,毕竟好记性不如烂笔头.上篇博客更新了关于<ES6中的迭代器.Generator函数以及Generator函数的异步操作& ...
- [Leetcode][动态规划] 第931题 下降路径最小和
一.题目描述 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示 ...
- 视频转成在github的readme中展示项目的gif动图
本文中涉及的FastStone Capture和FFmpeg两个软件的百度网盘链接: 链接:https://pan.baidu.com/s/1D5LO9Qmjl-vwJZfnbAloyQ 提取码:56 ...
- 新手学习FFmpeg - 调用API完成视频的读取和输出
在写了几个avfilter之后,原本以为对ffmpeg应该算是入门了. 结果今天想对一个视频文件进行转码操作,才发现基本的视频读取,输出都搞不定. 痛定思痛,仔细研究了一下ffmpeg提供的examp ...