FluentAspects -- 基于 Fluent API 的 Aop
FluentAspects -- 基于 Fluent API 的 Aop
Intro
上次我们做了一个简单的 AOP 实现示例,但是实现起来主要是基于 Attribute 来做的,对于代码的侵入性太强,于是尝试实现基于 Fluent API 的方式来做 AOP 。
抽象 InterceptorResolver
原来获取方法执行的 Interceptor 是通过 Attribute 来获取的,现在我们只需要将获取 Interceptor 的逻辑抽象出来就可以实现不必依赖于 Attribute 了
方法执行上下文定义:
public interface IInvocation
{
public MethodInfo ProxyMethod { get; }
public object ProxyTarget { get; }
public MethodInfo Method { get; }
public object Target { get; }
public object[] Arguments { get; }
Type[] GenericArguments { get; }
public object ReturnValue { get; set; }
}
方法拦截器 Interceptor 接口定义:
public interface IInterceptor
{
Task Invoke(IInvocation invocation, Func<Task> next);
}
自定义 Interceptor 只需要继承这个接口实现相应的逻辑就好了
获取 IInterceptorResolver 接口定义:
public interface IInterceptorResolver
{
IReadOnlyCollection<IInterceptor> ResolveInterceptors(IInvocation invocation);
}
原来基于 Attribute 获取 Interceptor 的方式可以实现一个 AttributeInterceptorResolver
想要基于 Fluent API 来获取 Interceptor ,只需要实现基于 Fluent API 的 InterceptorResolver 就可以了,具体的实现可以参考 FluentConfigInterceptorResolver
示例预览
测试服务定义:
public interface ISvc1
{
void Invoke();
}
public interface ISvc2
{
void Invoke();
}
public class Svc2 : ISvc2
{
public void Invoke()
{
Console.WriteLine($"invoking in {GetType().Name} ...");
}
public void Invoke2()
{
Console.WriteLine($"invoking in {GetType().Name} ...");
}
}
public class Svc3
{
public virtual void Invoke()
{
Console.WriteLine($"invoking in {GetType().Name} ...");
}
}
public class Svc4
{
public virtual void Invoke()
{
Console.WriteLine($"invoking in {GetType().Name} ...");
}
public void Invoke2()
{
Console.WriteLine($"invoking2 in {GetType().Name} ...");
}
public virtual void Invoke3()
{
Console.WriteLine($"invoking3 in {GetType().Name} ...");
}
}
测试 Interceptor
internal class LogInterceptor : IInterceptor
{
public async Task Invoke(IInvocation invocation, Func<Task> next)
{
Console.WriteLine($"invoke {invocation.ProxyMethod} in {GetType().Name} begin");
await next();
Console.WriteLine($"invoke {invocation.ProxyMethod} in {GetType().Name} end");
}
}
测试代码:
public static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddFluentAspects(options =>
{
// 为所有拦截的方法添加拦截器
options.InterceptAll()
.With<LogInterceptor>()
;
// 对 Svc3 类型禁用拦截器
options.NoInterceptType<Svc3>();
// Svc4 类型的 Invoke3() 方法禁用拦截器
options.NoInterceptMethod<Svc4>(s => s.Invoke3());
});
services.AddTransientProxy<Svc4>();
var serviceProvider = services.BuildServiceProvider();
var proxyFactory = serviceProvider.GetRequiredService<IProxyFactory>();
var svc1 = proxyFactory.CreateProxy<ISvc1>();
svc1.Invoke();
Console.WriteLine();
var svc2 = proxyFactory.CreateProxy<ISvc2, Svc2>();
svc2.Invoke();
Console.WriteLine();
var svc3 = proxyFactory.CreateProxy<Svc3>();
svc3.Invoke();
Console.WriteLine();
var svc4 = proxyFactory.CreateProxyWithTarget<ISvc2, Svc2>(new Svc2());
svc4.Invoke();
Console.WriteLine();
// 直接从注册的服务中获取
var svc5 = serviceProvider.GetRequiredService<Svc4>();
svc5.Invoke();
Console.WriteLine();
svc5.Invoke2();
Console.WriteLine();
svc5.Invoke3();
Console.WriteLine();
Console.WriteLine("finished");
Console.ReadLine();
}
输出结果预览:

More
最近十几天的时间一直在搞这个,相比之前写的示例,真正实现一个完整的 AOP 框架还是要做比较多的事情的,之前的 AOP 示例,没有考虑泛型,也没有什么设计,所以前面的示例只能算是一个小玩具。
在实现的过程中,参考了很多 AspectCore 的代码,有一些代码甚至是直接从 AspectCore 里抄过来的。
推荐大家有机会研究学习一下柠檬大佬的 AspectCore 的源码,这个 AOP 框架的代码组织,代码细节都挺不错的。
AspectCore 源码地址: https://github.com/dotnetcore/AspectCore-Framework
Reference
- https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common/Aspect
- https://github.com/WeihanLi/SamplesInPractice/blob/master/FluentAspectSample/Program.cs
- https://github.com/dotnetcore/AspectCore-Framework
FluentAspects -- 基于 Fluent API 的 Aop的更多相关文章
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/fluent-api-in-code-first.aspx EF 6 Code-Firs ...
- 实现一个简单的基于动态代理的 AOP
实现一个简单的基于动态代理的 AOP Intro 上次看基于动态代理的 AOP 框架实现,立了一个 Flag, 自己写一个简单的 AOP 实现示例,今天过来填坑了 目前的实现是基于 Emit 来做的, ...
- 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...
- 基于注解的Spring AOP的配置和使用
摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...
- 使用 Fluent API 配置/映射属性和类型(摘自微软Data Access and Storage)
使用 Fluent API 配置/映射属性和类型 使用实体框架 Code First 时,默认行为是使用一组 EF 中内嵌的约定将 POCO 类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体 ...
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...
- Entity Framework Code First (五)Fluent API - 配置关系
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...
- 使用Fluent API 配置/映射属性和类型
Code First约定-Fluent API配置 使用Fluent API 配置/映射属性和类型 简介 通常通过重写派生DbContext 上的OnModelCreating 方法来访问Code F ...
- 使用 Fluent API 配置/映射属性和类型
使用 Fluent API 配置/映射属性和类型 使用实体框架 Code First 时,默认行为是使用一组 EF 中内嵌的约定将 POCO 类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体 ...
随机推荐
- Chrome截图截满滑动栏,QQ截长屏,录屏
1.Chrome截图截满滑动栏 一般我们截图都是用QQ的Ctrl+shift+A,但是网页不好截,这里我们可以用Chrome控制台来截全网页: F12或Ctrl+shift+i打开控制台: 点击一下控 ...
- 在markdown中怎么划线?-[markdown]
使用html标签:<hr/> 示例: 百灵鸟,飞过蓝天. <hr/> 我爱你,我亲爱的祖国. <hr/> 生为华夏傲骨,死是华夏精魂. 效果: (水)
1036 Boys vs Girls (25分) This time you are asked to tell the difference between the lowest grade o ...
- nginx产品环境安全配置-主配置文件
以下配置为产品环境的nginx基于安全和效率的主配置文件,不包含fastcgi相关配置 cat /etc/nginx/nginx.conf user nginx; worker_processes a ...
- 听说这个 IP 和子网掩码异常难算
IP地址格式 每个Internet主机或路由器都有IP地址.所有的IP地址包括网络号和主机号(就像是手机号,前几位是区号,后几位是序列号). 说明如下 A类地址用于主机数目非常多的网络.A类地址允许有 ...
- spring06
主要是学会spring中关于通过注释来配置bean. 以及处理怎样智选其一或者补选某一个bean还有就是关于当出现多个相同的bean的时候怎样配置: 具体的代码和配置文件如下: <?xml ve ...
- git rebase解决合并冲突
git rebase解决合并冲突 记录合并冲突解决方法,使用的git rebase,感觉很好用 1.git rebase 文档 https://git-scm.com/docs/git-rebas ...
- LARAVEL快速入门
一.查询 $map = []; $map[] = ['u.store_id','=',0]; $map[] = ['u.reg_time','<',time()]; $map[] = ['u.u ...
- Linux环境安装Docker
1. 使用APT安装 # 更新数据源 apt-get update # 安装所需依赖 apt-get -y install apt-transport-https ca-certificates cu ...
- cmake cpack 安装打包程序
//set(CPACK_GENERATOR "TGZ") 7Z(.7z) 7Zzip(.7z) TBZ2(tar.bz2) TGZ(.tar.gz) TXZ(.tar.xz) TZ ...