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 (三十)读写分离
FreeSql 支持数据库读写分离,本功能是客户端的读写分离行为,数据库服务器该怎么配置仍然那样配置,不受本功能影响,为了方便描术后面讲到的[读写分离]都是指客户端的功能支持. 各种数据库的读写方案不 ...
- android 和 webService交互
webService 很久不用了,第一次使用还是13年, 早已忘记怎么搞了.今天看了篇博文,写了个demo .记录下吧! 首先要下载skoap2 .... xxx.jar ,我用的是最新的3.6. ...
- java 当前时间月份
public static void main(String[] arg) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd ...
- CoDeSys
CoDeSys是全球最著名的PLC内核软件研发厂家德国的3S(SMART,SOFTWARE,SOLUTIONS)公司出的一款与制造商无关的IEC 61131-1编程软件.CoDeSys 支持完整版本的 ...
- Cookie的应用——Servlet实现三天免登录
1.工程结构: 2.Servlet的运用: (1)登录界面: protected void doGet(HttpServletRequest request, HttpServletResponse ...
- jmeter性能分析
1.硬件要求:包括客户端和服务端的cpu,mem,network,disk等,这些硬件设备必须满足性能测试的前提下,才能进行性能测试,否则得到的各项指标不一定是正确的 2.场景分析: 测试前的准备工作 ...
- 关于CPU核心,线程,进程,并发,并行,及java线程之间的关系
前言:作为一个转行java的小白,一直搞不清楚java中的多线程.于是来梳理一下关于CPU核心,线程,进程,并发,并行,及java线程之间的关系, 1.CPU角度来看: 我们以Intel的Core i ...
- 上手Dubbo之 环境搭建
和传统ssm整合--写XML配置文件 搭建服务的提供者和服务的消费者,实现服务消费者跨应用远程调用服务提供者 公共模块抽取 公共模块的抽取 服务的消费者远程调用服务的提供者, 最起码他自己要得到在服务 ...
- 控制执行流程之ForEach语法
1.定义 : 一种更加简洁的for语法,用于数组和容器===>>>foreach,无需创建int类型变量对被访问项构建的序列进行计数. 2.经典应用: 对于数组的初始化,如果选用f ...
- 品Spring:bean定义上梁山
认真阅读,收获满满,向智慧又迈进一步... 技术不枯燥,先来点闲聊 先说点好事高兴一下.前段时间看新闻说,我国正式的空间站建设已在进行当中.下半年,长征五号B运载火箭将在海南文昌航天发射场择机将空间站 ...