动手造轮子:实现一个简单的 AOP 框架
动手造轮子:实现一个简单的 AOP 框架
Intro
最近实现了一个 AOP 框架 -- FluentAspects,API 基本稳定了,写篇文章分享一下这个 AOP 框架的设计。
整体设计
概览

IProxyTypeFactory
用来生成代理类型,默认提供了基于 Emit 动态代理的实现,基于接口设计,可以扩展为其他实现方式
接口定义如下:
public interface IProxyTypeFactory
{
Type CreateProxyType(Type serviceType);
Type CreateProxyType(Type serviceType, Type implementType);
}
IProxyFactory
用来生成代理实例,默认实现是基于 IProxyTypeFactory 生成代理类型之后创建实例
接口定义如下:
public interface IProxyFactory
{
object CreateProxy(Type serviceType, object[] arguments);
object CreateProxy(Type serviceType, Type implementType, params object[] arguments);
object CreateProxyWithTarget(Type serviceType, object implement, object[] arguments);
}
IInvocation
执行上下文,默认实现就是方法执行的上下文,包含了代理方法信息、被代理的方法信息、方法参数,返回值以及用来自定义扩展的一个 Properties 属性
public interface IInvocation
{
MethodInfo ProxyMethod { get; }
object ProxyTarget { get; }
MethodInfo Method { get; }
object Target { get; }
object[] Arguments { get; }
Type[] GenericArguments { get; }
object ReturnValue { get; set; }
Dictionary<string, object> Properties { get; }
}
IInterceptor
拦截器,用来定义公用的处理逻辑,方法拦截处理方法
接口定义如下:
public interface IInterceptor
{
Task Invoke(IInvocation invocation, Func<Task> next);
}
invocation 是方法执行的上下文,next 代表后续的逻辑处理,类似于 asp.net core 里的 next ,如果不想执行方面的方法不执行 next 逻辑即可
IInterceptorResolver
用来根据当前的执行上下文获取到要执行的拦截器,默认是基于 FluentAPI 的实现,但是如果你特别想用基于 Attribute 的也是可以的,默认提供了一个 AttributeInterceotorResovler,你也可以自定义一个适合自己的 InterceptorResolver
public interface IInterceptorResolver
{
IReadOnlyList<IInterceptor> ResolveInterceptors(IInvocation invocation);
}
IInvocationEnricher
上面 IInvocation 的定义中有一个用于扩展的 Properties,这个 enricher 主要就是基于 Properties 来丰富执行上下文信息的,比如说记录 TraceId 等请求链路追踪数据,构建方法执行链路等
public interface IEnricher<in TContext>
{
void Enrich(TContext context);
}
public interface IInvocationEnricher : IEnricher<IInvocation>
{
}
AspectDelegate
AspectDelegate 是用来将构建要执行的代理方法的方法体的,首先执行注册的 InvocationEnricher,丰富上下文信息,然后根据执行上下文获取要执行的拦截器,构建一个执行委托,执行拦截器逻辑
// apply enrichers
foreach (var enricher in FluentAspects.AspectOptions.Enrichers)
{
try
{
enricher.Enrich(invocation);
}
catch (Exception ex)
{
InvokeHelper.OnInvokeException?.Invoke(ex);
}
}
// get delegate
var builder = PipelineBuilder.CreateAsync(completeFunc);
foreach (var interceptor in interceptors)
{
builder.Use(interceptor.Invoke);
}
return builder.Build();
更多信息可以参考源码: https://github.com/WeihanLi/WeihanLi.Common/blob/dev/src/WeihanLi.Common/Aspect/AspectDelegate.cs
使用示例
推荐和依赖注入结合使用,主要分为以微软的注入框架为例,有两种使用方式,一种是手动注册代理服务,一种是自动批量注册代理服务,来看下面的实例就明白了
服务注册:
IServiceCollection services = new ServiceCollection();
services.AddFluentAspects(options =>
{
// 注册拦截器配置
options.NoInterceptProperty<IFly>(f => f.Name);
options.InterceptAll()
.With<LogInterceptor>()
;
options.InterceptMethod<DbContext>(x => x.Name == nameof(DbContext.SaveChanges)
|| x.Name == nameof(DbContext.SaveChangesAsync))
.With<DbContextSaveInterceptor>()
;
options.InterceptMethod<IFly>(f => f.Fly())
.With<LogInterceptor>();
options.InterceptType<IFly>()
.With<LogInterceptor>();
// 注册 InvocationEnricher
options
.WithProperty("TraceId", "121212")
;
});
使用方式一,手动注册代理服务:
为了方便使用,提供了一些扩展方法:
services.AddTransientProxy<IFly, MonkeyKing>();
services.AddSingletonProxy<IEventBus, EventBus>();
services.AddDbContext<TestDbContext>(options =>
{
options.UseInMemoryDatabase("Test");
});
services.AddScopedProxy<TestDbContext>();
var serviceProvider = services.BuildServiceProvider();
使用方式二,批量自动注册代理服务:
services.AddTransient<IFly, MonkeyKing>();
services.AddSingleton<IEventBus, EventBus>();
services.AddDbContext<TestDbContext>(options =>
{
options.UseInMemoryDatabase("Test");
});
var serviceProvider = services.BuildFluentAspectsProvider(options =>
{
options.InterceptAll()
.With<TestOutputInterceptor>(output);
});
// 使用 Castle 来生成代理
var serviceProvider = services.BuildFluentAspectsProvider(options =>
{
options.InterceptAll()
.With<TestOutputInterceptor>(output);
}, builder => builder.UseCastle());
// 忽略命名空间为 Microsoft/System 的服务类型
var serviceProvider = services.BuildFluentAspectsProvider(options =>
{
options.InterceptAll()
.With<TestOutputInterceptor>(output);
}, builder => builder.UseCastle(), t=> t.Namespace != null && (t.Namespace.StartWith("Microsft") ||t.Namespace.StartWith("Microsft")));
More
这个框架还不是很完善,有一些地方还是需要优化的,目前还是在我自己的类库中,因为我的类库里要支持 net45,所以有一些不好的设计改起来不太方便,打算迁移出来作为一个单独的组件,直接基于 netstandard2.0/netstandard2.1, 甩掉 netfx 的包袱。
Reference
动手造轮子:实现一个简单的 AOP 框架的更多相关文章
- 动手造轮子:实现简单的 EventQueue
动手造轮子:实现简单的 EventQueue Intro 最近项目里有遇到一些并发的问题,想实现一个队列来将并发的请求一个一个串行处理,可以理解为使用消息队列处理并发问题,之前实现过一个简单的 Eve ...
- 动手造轮子:实现一个简单的 EventBus
动手造轮子:实现一个简单的 EventBus Intro EventBus 是一种事件发布订阅模式,通过 EventBus 我们可以很方便的实现解耦,将事件的发起和事件的处理的很好的分隔开来,很好的实 ...
- 动手造轮子:基于 Redis 实现 EventBus
动手造轮子:基于 Redis 实现 EventBus Intro 上次我们造了一个简单的基于内存的 EventBus,但是如果要跨系统的话就不合适了,所以有了这篇基于 Redis 的 EventBus ...
- 动手写一个简单的Web框架(模板渲染)
动手写一个简单的Web框架(模板渲染) 在百度上搜索jinja2,显示的大部分内容都是jinja2的渲染语法,这个不是Web框架需要做的事,最终,居然在Werkzeug的官方文档里找到模板渲染的代码. ...
- 动手写一个简单的Web框架(Werkzeug路由问题)
动手写一个简单的Web框架(Werkzeug路由问题) 继承上一篇博客,实现了HelloWorld,但是这并不是一个Web框架,只是自己手写的一个程序,别人是无法通过自己定义路由和返回文本,来使用的, ...
- 动手写一个简单的Web框架(HelloWorld的实现)
动手写一个简单的Web框架(HelloWorld的实现) 关于python的wsgi问题可以看这篇博客 我就不具体阐述了,简单来说,wsgi标准需要我们提供一个可以被调用的python程序,可以实函数 ...
- 自己动手设计并实现一个linux嵌入式UI框架(设计)
看了"自己动手设计并实现一个linux嵌入式UI框架"显然没有尽兴,因为还没有看到庐山真面目,那我今天继续,先来说说,我用到了哪些知识背景.如:C语言基础知识,尤其是指针.函数指针 ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- 一个简单的web框架实现
一个简单的web框架实现 #!/usr/bin/env python # -- coding: utf-8 -- __author__ = 'EchoRep' from wsgiref.simple_ ...
随机推荐
- 简述 zookeeper 基于 Zab 协议实现选主及事务提交
Zab 协议:zookeeper 基于 Paxos 协议的改进协议 zookeeper atomic broadcast 原子广播协议. zookeeper 基于 Zab 协议实现选主及事务提交. 一 ...
- 【github龟速克星】如何下载快如闪电
详见:https://www.kesci.com/home/project/5e96fe1ae7ec38002d03cd56 借助第三方网站:https://g.widora.cn/
- .net core HttpClient 使用之消息管道解析(二)
一.前言 前面分享了 .net core HttpClient 使用之掉坑解析(一),今天来分享自定义消息处理HttpMessageHandler和PrimaryHttpMessageHandler ...
- shiro的使用详解~
在家无聊复习一下shiro 打开了之前的项目,由于lombok插件安装不了,不知道idea抽什么风所以get,set方法报错了,将就一下 只看shiro的使用,配置在shiro分类中有 登录方法 pa ...
- SSM基础pom和xml的整合配置
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit ...
- VUE基础插值表达式
vue 基本操作插值表达式 首先使用js导入vue.js文件 <style> [v-claok]{ display:none;//解决闪烁问题 } </style> <d ...
- JAVA-Servlet操纵方法
此篇自用查询 存储数据的区域对象域对象的通用的方法:ServletContext context=getServletContext();获取ServletContext对象setAtrribute( ...
- Redis详解(九)------ 哨兵(Sentinel)模式详解
在上一篇博客----Redis详解(八)------ 主从复制,我们简单介绍了Redis的主从架构,但是这种主从架构存在一个问题,当主服务器宕机,从服务器不能够自动切换成主服务器,为了解决这个问题,我 ...
- Java——super的使用
总是忘了,还是记下来吧~~ With super(), the superclass no-argument constructor is called. With super(parameter l ...
- 13.Java连接Redis_Jedis_事务
Jedis事务我们使用JDBC连接Mysql的时候,每次执行sql语句之前,都需要开启事务:在MyBatis中,也需要使用openSession()来获取session事务对象,来进行sql执行.查询 ...