全新升级的AOP框架Dora.Interception[5]: 实现任意的拦截器注册方式

Dora.Interception提供了两种拦截器注册方式,一种是利用标注在目标类型、属性和方法上的InterceptorAttribute特性,另一种采用基于目标方法或者属性的调用表达式。通过提供的扩展点,我们可以任何我们希望的拦截器注册方式。

目录
一、IInterceptorProvider
二、InterceptorProviderBase
三、实现一种“万能”的拦截器注册方式
四、ConditionalInterceptorProvider

一、IInterceptorProvider

拦截器最终需要应用到某个具体的目标方法上,所以拦截器的注册就是如何建立拦截器与目标方法之间的映射关系,Dora.Interception将这一功能体现在如下所示的IInterceptorProvider接口上。顾名思义,IInterceptorProvider旨在解决为某个类型的某个方法提供拦截器列表的问题,这一个功能体现在GetInterceptors方法上。如下面的代码片段所示,该方法返回一组Sortable<InvokeDelegate>对象,InvokeDelegate代表拦截器本身,Sortable<InvokeDelegate>对象在此基础上添加了必要排序元素。

public interface IInterceptorProvider
{bool CanIntercept(Type targetType, MethodInfo method, out bool suppressed);IEnumerable<Sortable<InvokeDelegate>> GetInterceptors(Type targetType, MethodInfo method);void Validate(Type targetType, Action<MethodInfo> methodValidator, Action<PropertyInfo> propertyValidator) {}
}public sealed class Sortable<T>
{public int Order { get; }public T Value { get; set; }public Sortable(int order, T value){Order = order;Value = value;}
}

除了GetInterceptors方法,IInterceptorProvider接口还定义了额外两个方法,CanIntercept方法用来判断指定的方式是否需要被拦截,代码生成器会利用这个方法决定如果生成最终可供拦截的代理类。另一个Validate方法用来验证针对指定类型的拦截器注册方式是否合法,即拦截器是否应用到一些根本无法被拦截的方法或者属性上,具体的检验逻辑由方法提供的两个委托来完成。

二、InterceptorProviderBase

我们自定义的IInterceptorProvider实现类型一般派生于如下这个抽象基类InterceptorProviderBase,后者在接口的基础上提供了一个IConventionalInterceptorFactory接口类型的InterceptorFactory属性。顾名思义,IConventionalInterceptorFactory对象帮助我们按照约定定义的拦截器类型或者其实例转换成标准的拦截器表现形式,即InvokeDelegate委托。

public abstract class InterceptorProviderBase : IInterceptorProvider
{public IConventionalInterceptorFactory InterceptorFactory { get; }  protected InterceptorProviderBase(IConventionalInterceptorFactory interceptorFactory) ;public abstract bool CanIntercept(Type targetType, MethodInfo method, out bool suppressed);public abstract IEnumerable<Sortable<InvokeDelegate>> GetInterceptors(Type targetType, MethodInfo method);
}public interface IConventionalInterceptorFactory
{InvokeDelegate CreateInterceptor(Type interceptorType, params object[] arguments);InvokeDelegate CreateInterceptor(object interceptor);
}

三、实现一种“万能”的拦截器注册方式

接下来我们通过自定义的IInterceptorProvider类型实现一种“万能”的拦截器注册方式——根据指定的条件表达式将指定的拦截器关联到目标方法上。在提供具体实现之前,我们先来体验一下由它达成的编程模型。

public class FoobarInterceptor
{public ValueTask InvokeAsync(InvocationContext invocationContext){var method = invocationContext.MethodInfo;Console.WriteLine($"{method.DeclaringType!.Name}.{method.Name} is intercepted.");return invocationContext.ProceedAsync();}
}public class Foobar
{public virtual void M() { }public virtual object? P { get; set; }}

我们依然以上面这个简单的拦截器类型FoobarInterceptor为例,现在我们需要将它应用到Foobar类型的M和P属性的Set方法上,针对FoobarInterceptor的注册就可以按照如下方式来完成。如代码片段所示,我们在调用InterceptionBuilder的RegisterInterceptors扩展方法中提供了一个Action<ConditionalInterceptorProviderOptions>委托,并利用它添加了针对FoobarInterceptor与两个Func<Type, MethodInfo, bool>委托之间的关系,后者用来匹配目标方法(含属性方法)。

var foobar= new ServiceCollection().AddSingleton<Foobar>().BuildInterceptableServiceProvider(interception => interception.RegisterInterceptors(RegisterInterceptors)).GetRequiredService<Foobar>();foobar.M();
_ = foobar.P;
foobar.P = null;
Console.ReadLine();static void RegisterInterceptors(ConditionalInterceptorProviderOptions options)
{options.For<FoobarInterceptor>().To(1, (type, method) => type == typeof(Foobar) && method.Name == "M").To(1, (type, method)  => type == typeof(Foobar) && method.IsSpecialName && method.Name == "set_P");
}

程序运行后会在控制台输出如下的结果,可以看出FoobarInterceptor拦截确实只应用到M和P属性的Set方法上,属性的Get方法并未被拦截。

d1a6978e717abf250ec1deb1ae622653.png

四、ConditionalInterceptorProvider

上述这种针对匹配条件的“万能”注册方式是通过如下这个ConditionalInterceptorProvider类型实现的。ConditionalInterceptorProviderOptions类型定义了对应的配置选项,其核心就是一组ConditionalInterceptorRegistration对象的集合,而每一个ConditionalInterceptorRegistration对象是一个表示匹配条件的Func<Type, MethodInfo, bool>委托与拦截器工厂的Func<IConventionalInterceptorFactory, Sortable<InvokeDelegate>>委托之间的映射关系,后者利用指定的IConventionalInterceptorFactory来创建一个对应的Sortable<InvokeDelegate>对象。

public class ConditionalInterceptorProvider : InterceptorProviderBase
{private readonly ConditionalInterceptorProviderOptions _options;public ConditionalInterceptorProvider(IConventionalInterceptorFactory interceptorFactory, IOptions<ConditionalInterceptorProviderOptions> optionsAccessor) : base(interceptorFactory)=> _options = optionsAccessor.Value;public override bool CanIntercept(Type targetType, MethodInfo method, out bool suppressed){suppressed = false;return _options.Registrations.Any(it => it.Condition(targetType, method));}public override IEnumerable<Sortable<InvokeDelegate>> GetInterceptors(Type targetType, MethodInfo method)=> _options.Registrations.Where(it => it.Condition(targetType, method)).Select(it => it.Factory(InterceptorFactory)).ToList();
}public class ConditionalInterceptorProviderOptions
{public IList<ConditionalInterceptorRegistration> Registrations { get; } = new List<ConditionalInterceptorRegistration>();public Registry<TInterceptor> For<TInterceptor>(params object[] arguments)=> new(factory => factory.CreateInterceptor(typeof(TInterceptor), arguments), this);
}public class Registry<TInterceptor>
{private readonly Func<IConventionalInterceptorFactory, InvokeDelegate> _factory;private readonly ConditionalInterceptorProviderOptions _options;public Registry(Func<IConventionalInterceptorFactory, InvokeDelegate> factory, ConditionalInterceptorProviderOptions options){_factory = factory;_options = options;}public Registry<TInterceptor> To(int order, Func<Type, MethodInfo, bool> condition){var entry = new ConditionalInterceptorRegistration(condition, factory=>new Sortable<InvokeDelegate>(order, _factory(factory)));_options.Registrations.Add(entry);return this;}
}public class ConditionalInterceptorRegistration
{public Func<Type, MethodInfo, bool> Condition { get; }public Func<IConventionalInterceptorFactory, Sortable<InvokeDelegate>> Factory { get; }public ConditionalInterceptorRegistration(Func<Type, MethodInfo, bool> condition, Func<IConventionalInterceptorFactory, Sortable<InvokeDelegate>> factory){Condition = condition;Factory = factory;}
}

这一组映射关系利用ConditionalInterceptorProviderOptions的For<TInterceptor>方法进行添加,该方法返回一个Registry<TInterceptor>对象,后者提供的To方法指定了作为匹配条件的Func<Type, MethodInfo, bool>委托和决定拦截器执行顺序的Order值。ConditionalInterceptorProvider利用构造函数注入的IOptions<ConditionalInterceptorProviderOptions>得到这组映射关系,CanIntercept方法利用这组关系的匹配条件确定指定的方法是否应该被拦截,另一个GetInterceptors方法则利用匹配的工厂来创建返回的这组Sortable<InvokeDelegate>对象。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/281956.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SCAU 算法课的题

8594 有重复元素的排列问题&#xff08;优先做&#xff09; 时间限制:1000MS 内存限制:1000K提交次数:1610 通过次数:656 题型: 编程题 语言: G;GCC;VC Description 设集合R{r1,r2,...,rn}是要进行排列的n个元素&#xff0c;其中r1,r2,...,rn可能相同。 试着设计一个算法&am…

react 数组新增_React 新特性 Hooks 讲解及实例(二)

本文是 React 新特性系列的第二篇&#xff0c;第一篇请点击这里&#xff1a;React 新特性讲解及实例什么是 HooksHook 是 React 16.8 的新增特性。它可以让你在不编写 类组件 的情况下使用 state以及其他的 React 特性。类组件的不足状态逻辑复用难缺少复用机制渲染属性和高阶组…

智课雅思词汇---二十二、-al即是名词性后缀又是形容词后缀

智课雅思词汇---二十二、-al即是名词性后缀又是形容词后缀 一、总结 一句话总结&#xff1a; 后缀&#xff1a;-al ②[名词后缀] 1、构成抽象名词&#xff0c;表示行为、状况、事情 refusal 拒绝 proposal 提议 withdrawal 撤退 1、名词性后缀acy是什么意思&#xff1f; 后缀&a…

javascript事件处理程序

javascript 事件处理程序 1、普通事件处理程序 <input type"button" value"click me" οnclick"showMessage()" /> function showMessage(){alert("clicked");} 2、DOMO 级事件处理程序 <span style"white-space:pre&…

eclipse新发现功能之dos和terminal(ssh连接)

dos功能&#xff1a; window——》show view——》other——》remote systems&#xff0c;选择remote shell&#xff0c;选择确定或者双击&#xff0c;打开了一个新工具窗口。点击remote shell窗口最右上角的小三角&#xff0c;在launch子菜单中选择local&#xff0c;点击即可。…

7天学会python_7天学会Python最佳可视化工具Seaborn(五):结构化展示多维数据

当探索具有中等数量(不多不少的意思……)维度的数据集时&#xff0c;一个很好的方式是基于不同的子数据集构建不同的实例&#xff0c;并将它们以网格的方式组织在一张图之中。这种技术有时被称为“lattice”或“trellis”(大概是格子图、网格图)&#xff0c;这跟“small multip…

面对峰值响应冲击,解决高并发的三大策略

2019独角兽企业重金招聘Python工程师标准>>> 当前在互联网的大潮下&#xff0c;众所周知淘宝、京东这些交易系统每天产生的数据量都是海量的&#xff0c;每天的交易并发也是惊人的&#xff0c;尤其是“双11”、“6.18”这些活动&#xff0c;对系统的峰值响应提出了非…

.NET 采用 SkiaSharp 生成二维码和图形验证码及图片进行指定区域截取方法实现

在最新版的 .NET 平台中&#xff0c;微软在逐步放弃 System.Drawing.Imaging &#xff0c;给出的理由如下&#xff1a;System.Drawing命名空间对某些操作系统和应用程序类型有一些限制。在Windows&#xff0c; System.Drawing 依赖于GDI操作系统附带的本机库。 某些Windows SKU…

Linux运维人员必会开源运维工具体系

新手必会用深&#xff08;8-15k&#xff09;标记&#xff0c;老鸟必会深浅蓝色(15-25K)标记操作系统&#xff1a;Centos,Ubuntu,Redhat,suse,Freebsd网站服务&#xff1a;nginx,apache,lighttpd,php,tomcat,resin数据 库&#xff1a;MySQL,MariaDB,PostgreSQLDB中间件&#x…

unity读取Text

sing UnityEngine;using System.Collections;using System.IO; //需要导入System.IO&#xff0c;主要使用它的File类public class TextTest : MonoBehaviour { private string Mytxt; //用来存放文本内容 void Start() { Mytxt ReadFile("C:\\Users\\Admin\\Desktop\\测试…

hibernate mysql 主从_MYSQL主从复制和写分离

基础篇https://edu.51cto.com/course/19845.htmlhttps://edu.51cto.com/course/19845.htmlhttps://edu.51cto.com/course/19841.htmlhttps://edu.51cto.com/course/21197.htmlhttps://edu.51cto.com/course/19886.htmlhttps://edu.51cto.com/course/19887.htmlhttps://edu.51ct…

深入剖析Redis系列(五) - Redis数据结构之字符串

前言 字符串类型 是 Redis 最基础的数据结构。字符串类型 的值实际可以是 字符串&#xff08;简单 和 复杂 的字符串&#xff0c;例如 JSON、XML&#xff09;、数字&#xff08;整数、浮点数&#xff09;&#xff0c;甚至是 二进制&#xff08;图片、音频、视频&#xff09;&am…

全新升级的AOP框架Dora.Interception[6]: 框架设计和实现原理

本系列前面的五篇文章主要介绍Dora.Interception的编程模式以及对它的扩展定制&#xff0c;现在我们来聊聊它的设计和实现原理。目录一、调用链抽象二、基于约定的拦截器定义三、基于调用上下文的依赖注入容器四、拦截器的提供五、调用链的构建六、方法拦截的实现原理七、依赖注…

activemq 安全连接

一、定义用户组1.1 simpleAuthenticationPlugin通过在activemq.xml中配置用户组<plugins> <simpleAuthenticationPlugin> <users> <authenticationUser username"admin" password"password" groups"admins,publishers,consumer…

React Native在Android当中实践(五)——常见问题

React Native在Android当中实践&#xff08;一&#xff09;——背景介绍 React Native在Android当中实践&#xff08;二&#xff09;——搭建开发环境 React Native在Android当中实践&#xff08;三&#xff09;——集成到Android项目当中 React Native在Android当中实践&#…

完成登录与注册页面的前端

完成登录与注册页面的HTMLCSSJS&#xff0c;其中的输入项检查包括&#xff1a; 用户名6-12位 首字母不能是数字 只能包含字母和数字 密码6-12位 注册页两次密码是否一致 JS&#xff1a; function fnLogin() {var uSer document.getElementById("user");var pAss do…

mysql505复位密码_mysql5 如何复位根用户密码[官方文档]

如何复位根用户密码如果你从未为MySQL设置根用户密码&#xff0c;服务器在以根用户身份进行连接时不需要密码。但是&#xff0c;建议你为每个账户设置密码如果你以前设置了根用户密码&#xff0c;但却忘记了该密码&#xff0c;可设置新的密码。下述步骤是针对Windows平台的。在…

WPF效果第二百零一篇之实现合并单元格

早一段时间又一次出差青海省西宁市;回来又是总结又是各种琐事,也没顾得上去分享点东西;大周末的就在家分享一下,这二天再次基于ListBox实现的合并单元格的效果:1、ListBox嵌套ListBox的前台布局:<ListBox ItemsSource"{Binding LCPListData}" x:Name"Manufac…

转载 maven 详解 http://www.cnblogs.com/binyue/p/4729134.html

--声明规范 <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!--声…

ASP.NET Core中使用EasyCaching作为缓存抽象层

简介做后端开发&#xff0c;缓存应该是天天在用&#xff0c;很多时候我们的做法是写个帮助类&#xff0c;然后用到的时候调用一下。这种只适合简单层次的应用&#xff1b;一旦涉及到接口实现调整之类的&#xff0c;这种强耦合的做法很不合适。有些其他的功能又要去重复造轮子。…