实现一个简单的基于动态代理的 AOP
实现一个基于动态代理的 AOP
Intro
上次看基于动态代理的 AOP 框架实现,立了一个 Flag, 自己写一个简单的 AOP 实现示例,今天过来填坑了
目前的实现是基于 Emit 来做的,后面有时间再写一个基于 Roslyn 来实现的示例
效果演示
演示代码:
切面逻辑定义:
public class TryInvokeAspect : AbstractAspect
{
public override void Invoke(MethodInvocationContext methodInvocationContext, Action next)
{
Console.WriteLine($"begin invoke method {methodInvocationContext.ProxyMethod.Name} in {GetType().Name}...");
try
{
next();
}
catch (Exception e)
{
Console.WriteLine($"Invoke {methodInvocationContext.ProxyMethod.DeclaringType?.FullName}.{methodInvocationContext.ProxyMethod.Name} exception");
Console.WriteLine(e);
}
Console.WriteLine($"end invoke method {methodInvocationContext.ProxyMethod.Name} in {GetType().Name}...");
}
}
public class TryInvoke1Aspect : AbstractAspect
{
public override void Invoke(MethodInvocationContext methodInvocationContext, Action next)
{
Console.WriteLine($"begin invoke method {methodInvocationContext.ProxyMethod.Name} in {GetType().Name}...");
try
{
next();
}
catch (Exception e)
{
Console.WriteLine($"Invoke {methodInvocationContext.ProxyMethod.DeclaringType?.FullName}.{methodInvocationContext.ProxyMethod.Name} exception");
Console.WriteLine(e);
}
Console.WriteLine($"end invoke method {methodInvocationContext.ProxyMethod.Name} in {GetType().Name}...");
}
}
public class TryInvoke2Aspect : AbstractAspect
{
public override void Invoke(MethodInvocationContext methodInvocationContext, Action next)
{
Console.WriteLine($"begin invoke method {methodInvocationContext.ProxyMethod.Name} in {GetType().Name}...");
try
{
next();
}
catch (Exception e)
{
Console.WriteLine($"Invoke {methodInvocationContext.ProxyMethod.DeclaringType?.FullName}.{methodInvocationContext.ProxyMethod.Name} exception");
Console.WriteLine(e);
}
Console.WriteLine($"end invoke method {methodInvocationContext.ProxyMethod.Name} in {GetType().Name}...");
}
}
测试服务定义
// 测试接口定义
public interface ITestService
{
[TryInvokeAspect]
void Test();
[TryInvokeAspect]
[TryInvoke1Aspect]
[TryInvoke2Aspect]
void Test1(int a, string b);
[TryInvokeAspect]
string Test2();
[TryInvokeAspect]
int Test3();
}
// 测试接口实例定义
public class TestService : ITestService
{
[TryInvokeAspect]
public virtual string TestProp { get; set; }
public void Test()
{
Console.WriteLine("test invoked");
}
public virtual void Test1(int a, string b)
{
Console.WriteLine($"a:{a}, b:{b}");
}
[TryInvoke1Aspect]
public virtual string Test2()
{
return "Hello";
}
[TryInvokeAspect]
public virtual int Test3()
{
return 1;
}
}
测试代码:
//var testService = ProxyGenerator.Instance.CreateInterfaceProxy<ITestService>();
var testService = ProxyGenerator.Instance.CreateInterfaceProxy<ITestService, TestService>();
// var testService = ProxyGenerator.Instance.CreateClassProxy<TestService>();
// testService.TestProp = "12133";
testService.Test();
Console.WriteLine();
testService.Test1(1, "str");
var a = testService.Test2();
var b = testService.Test3();
Console.WriteLine($"a:{a}, b:{b}");
Console.ReadLine();
输出效果:

整体结构
ProxyGenerator
ProxyGenerator 代理生成器,用来创建代理对象
public class ProxyGenerator
{
public static readonly ProxyGenerator Instance = new ProxyGenerator();
public object CreateInterfaceProxy(Type interfaceType)
{
var type = ProxyUtil.CreateInterfaceProxy(interfaceType);
return Activator.CreateInstance(type);
}
public object CreateInterfaceProxy(Type interfaceType, Type implementationType)
{
var type = ProxyUtil.CreateInterfaceProxy(interfaceType, implementationType);
return Activator.CreateInstance(type);
}
public object CreateClassProxy(Type classType, params Type[] interfaceTypes)
{
var type = ProxyUtil.CreateClassProxy(classType, interfaceTypes);
return Activator.CreateInstance(type);
}
public object CreateClassProxy(Type classType, Type implementationType, params Type[] interfaceTypes)
{
var type = ProxyUtil.CreateClassProxy(implementationType, interfaceTypes);
return Activator.CreateInstance(type);
}
}
为了更方便的使用泛型,定义了几个扩展方法:
public static class Extensions
{
public static TInterface CreateInterfaceProxy<TInterface>(this ProxyGenerator proxyGenerator) =>
(TInterface)proxyGenerator.CreateInterfaceProxy(typeof(TInterface));
public static TInterface CreateInterfaceProxy<TInterface, TImplement>(this ProxyGenerator proxyGenerator) where TImplement : TInterface =>
(TInterface)proxyGenerator.CreateInterfaceProxy(typeof(TInterface), typeof(TImplement));
public static TClass CreateClassProxy<TClass>(this ProxyGenerator proxyGenerator) where TClass : class =>
(TClass)proxyGenerator.CreateClassProxy(typeof(TClass));
public static TClass CreateClassProxy<TClass, TImplement>(this ProxyGenerator proxyGenerator) where TImplement : TClass =>
(TClass)proxyGenerator.CreateClassProxy(typeof(TClass), typeof(TImplement));
}
AbstractAspect
AbstractAspect 切面抽象类,继承了 Attribute,可以继承它来实现自己的切面逻辑
public abstract class AbstractAspect : Attribute
{
public abstract void Invoke(MethodInvocationContext methodInvocationContext, Action next);
}
MethodInvocationContext
MethodInvocationContext 方法执行上下文,包含了执行方法时的原始方法信息以及代理方法信息,方法参数,方法返回值
public class MethodInvocationContext
{
public MethodInfo ProxyMethod { get; }
public MethodInfo MethodBase { get; }
public object ProxyTarget { get; }
public object Target { get; }
public object[] Parameters { get; }
public object ReturnValue { get; set; }
public MethodInvocationContext(MethodInfo method, MethodInfo methodBase, object proxyTarget, object target, object[] parameters)
{
ProxyMethod = method;
MethodBase = methodBase;
ProxyTarget = proxyTarget;
Target = target;
Parameters = parameters;
}
}
代理方法逻辑
生成代理的方法在上一节已经介绍,主要就是通过 Emit 生成代理类,要写一些 Emit 代码, Emit 不在今天的讨论范围内,这里不多介绍,生成代理方法的时候,会检查方法上的 Attribute ,如果是切面逻辑就注册切面逻辑,最后像 asp.net core 中间件一样组装在一起拼成一个委托。
核心代码如下:
// var invocation = new MethodInvocationContext(method, methodBase, this, parameters);
var localAspectInvocation = il.DeclareLocal(typeof(MethodInvocationContext));
il.Emit(OpCodes.Ldloc, localCurrentMethod);
il.Emit(OpCodes.Ldloc, localMethodBase);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldloc, localTarget);
il.Emit(OpCodes.Ldloc, localParameters);
// 创建一个 MethodInvocationContext 实例
il.New(typeof(MethodInvocationContext).GetConstructors()[0]);
il.Emit(OpCodes.Stloc, localAspectInvocation);
// AspectDelegate.InvokeAspectDelegate(invocation);
il.Emit(OpCodes.Ldloc, localAspectInvocation);
var invokeAspectDelegateMethod =
typeof(AspectDelegate).GetMethod(nameof(AspectDelegate.InvokeAspectDelegate));
// 执行方法以及注册的切面逻辑
il.Call(invokeAspectDelegateMethod);
il.Emit(OpCodes.Nop);
if (method.ReturnType != typeof(void))
{
// 获取方法返回值
il.Emit(OpCodes.Ldloc, localAspectInvocation);
var getMethod = typeof(MethodInvocationContext).GetProperty("ReturnValue").GetGetMethod();
il.EmitCall(OpCodes.Callvirt, getMethod, Type.EmptyTypes);
if (method.ReturnType.IsValueType)
{
// 如果是值类型,做一下类型转换
il.EmitCastToType(typeof(object), method.ReturnType);
}
il.Emit(OpCodes.Stloc, localReturnValue);
il.Emit(OpCodes.Ldloc, localReturnValue);
}
il.Emit(OpCodes.Ret);
注册并执行切面逻辑代码实现:
// 缓存方法体执行的委托,包含切面逻辑的执行和方法的调用
private static readonly ConcurrentDictionary<string, Action<MethodInvocationContext>> _aspectDelegates = new ConcurrentDictionary<string, Action<MethodInvocationContext>>();
public static void InvokeAspectDelegate(MethodInvocationContext context)
{
var action = _aspectDelegates.GetOrAdd($"{context.ProxyMethod.DeclaringType}.{context.ProxyMethod}", m =>
{
// 获取切面逻辑,这里根据切面类型做了一个去重
var aspects = new List<AbstractAspect>(8);
if (context.MethodBase != null)
{
// 获取类方法上的切面逻辑
foreach (var aspect in context.MethodBase.GetCustomAttributes<AbstractAspect>())
{
if (!aspects.Exists(x => x.GetType() == aspect.GetType()))
{
aspects.Add(aspect);
}
}
}
// 获取接口方法上的切面
var methodParameterTypes = context.ProxyMethod.GetParameters().Select(p => p.GetType()).ToArray();
foreach (var implementedInterface in context.ProxyTarget.GetType().GetImplementedInterfaces())
{
var method = implementedInterface.GetMethod(context.ProxyMethod.Name, methodParameterTypes);
if (null != method)
{
foreach (var aspect in method.GetCustomAttributes<AbstractAspect>())
{
if (!aspects.Exists(x => x.GetType() == aspect.GetType()))
{
aspects.Add(aspect);
}
}
}
}
// 构建切面逻辑执行管道,类似于 asp.net core 里的请求管道, 以原始方法调用作为中间件的最后一步
var builder = PipelineBuilder.Create<MethodInvocationContext>(x => x.Invoke());
foreach (var aspect in aspects)
{
// 注册切面逻辑
builder.Use(aspect.Invoke);
}
// 构建方法执行委托
return builder.Build();
});
// 执行委托
action.Invoke(context);
// 检查返回值,防止切面逻辑管道的中断执行导致值类型返回值没有赋值
if (context.ProxyMethod.ReturnType != typeof(void))
{
if (context.ReturnValue == null && context.ProxyMethod.ReturnType.IsValueType)
{
// 为值类型返回值设置默认值作为返回值
context.ReturnValue = Activator.CreateInstance(context.ProxyMethod.ReturnType);
}
}
}
More
以上基本可以实现一个 AOP 功能,但是从扩展性以及功能上来说都还比较欠缺,基于 Attribute 的方式固然可以实现功能,但是太不灵活,如果我要在一个无法修改的接口上的某一个方法做一个切面逻辑,显然只使用 Attribute 是做不到的,还是 Fluent-API 的方式比较灵活。
想做一层 AOP 的抽象,切面逻辑通过 Fluent-API 的方式来注册,大概的 API 可能是这样的:
var settings = FluentAspects.For<ITestService>();
setting.PropertySetter(x=>x.TestProp)
.InterceptWith<TryInterceptor>()
.InterceptWith<TryInterceptor1>();
setting.Method(x=> x.Test2())
.InterceptWith<TryInterceptor>()
.InterceptWith<TryInterceptor1>();
然后基于 AspectCore 和 Castle.Core 来实现具体的 AOP 功能,暂时先想一下,争取尽快的发布一个基本可用的版本,然后之前基于 EF Core 的自动审计也可以基于 AOP 来实现了,这样就不需要显示继承 AuditDbContext 了~
文章所有源码可以在 Github 上获取到,Github 地址: https://github.com/WeihanLi/SamplesInPractice/tree/master/AopSample
Reference
- 让 .NET 轻松构建中间件模式代码
- 让 .NET 轻松构建中间件模式代码--支持中间件管道的中断和分支
- NET 下基于动态代理的 AOP 框架实现揭秘
- EF Core 数据变更自动审计设计
- AopSample
- AspectCore
实现一个简单的基于动态代理的 AOP的更多相关文章
- 动态代理到基于动态代理的AOP
动态代理,是java支持的一种程序设计方法. 动态代理实现中有两个重要的接口和类,分别是InvocationHandler(interface),Proxy(class). 要实现动态代理,必须要定义 ...
- .NET 下基于动态代理的 AOP 框架实现揭秘
.NET 下基于动态代理的 AOP 框架实现揭秘 Intro 之前基于 Roslyn 实现了一个简单的条件解析引擎,想了解的可以看这篇文章 https://www.cnblogs.com/weihan ...
- 设计模式 - 动态代理原理及模仿JDK Proxy 写一个属于自己的动态代理
本篇文章代码内容较多,讲的可能会有些粗糙,大家可以选择性阅读. 本篇文章的目的是简单的分析动态代理的原理及模仿JDK Proxy手写一个动态代理以及对几种代理做一个总结. 对于代理模式的介绍和讲解,网 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(十七)——服务保护之动态配置与热重载
在上一篇文章里,我们通过注入sentinel component到apigateway实现了对下游服务的保护,不过受限于目前变更component需要人工的重新注入配置以及重启应用更新componen ...
- 并发编程概述 委托(delegate) 事件(event) .net core 2.0 event bus 一个简单的基于内存事件总线实现 .net core 基于NPOI 的excel导出类,支持自定义导出哪些字段 基于Ace Admin 的菜单栏实现 第五节:SignalR大杂烩(与MVC融合、全局的几个配置、跨域的应用、C/S程序充当Client和Server)
并发编程概述 前言 说实话,在我软件开发的头两年几乎不考虑并发编程,请求与响应把业务逻辑尽快完成一个星期的任务能两天完成绝不拖三天(剩下时间各种浪),根本不会考虑性能问题(能接受范围内).但随着工 ...
- mybatis源码学习:基于动态代理实现查询全过程
前文传送门: mybatis源码学习:从SqlSessionFactory到代理对象的生成 mybatis源码学习:一级缓存和二级缓存分析 下面这条语句,将会调用代理对象的方法,并执行查询过程,我们一 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(十八)——服务保护之多级缓存
很久没有更新dapr系列了.今天带来的是一个小的组件集成,通过多级缓存框架来实现对服务的缓存保护,依旧是一个简易的演示以及对其设计原理思路的讲解,欢迎大家转发留言和star 目录:一.通过Dapr实现 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统(二十)——Saga框架实现思路分享
今天这篇博文的主要目的是分享一下我设计Saga的实现思路来抛砖引玉,其实Saga本身非常的类似于一个简单的工作流体系,相比工作流不一样的部分在于它没有工作流的复杂逻辑处理机制(比如会签),没有条件分支 ...
- 通过Dapr实现一个简单的基于.net的微服务电商系统
本来想在Dpar 1.0GA时发布这篇文章,由于其他事情耽搁了放到现在.时下微服务和云原生技术如何如荼,微软也不甘示弱的和阿里一起适时推出了Dapr(https://dapr.io/),园子里关于da ...
随机推荐
- mysql数据库中的mybatis中xml解决in不起作用的问题
在sql语句中,某个字段进行in条件的时候,不起作用, 但是执行语句查询为null数据,但是根据表中数据来看是不可能有null数据的可能性的,所以不知道什么原因导致数据出不来 我因此想到以下解决办法来 ...
- effective-java学习笔记---静态工厂方法替代构造方法
使用静态方法的优点: 1.它们是有名字的,生成的客户端代码更易阅读. 如:返回素数的静态方法 BigInteger.probablePrime 2.与构造方法不同,它们不需要每次调用时都创建一个对象. ...
- nginx负载均衡例子
upstream demo { ip_hash;//客户连接后, 一直用这个IP,直到会话结束,否则,动态程序可能会在换IP后出错 server 192.168.1.1:80 weight=5 | d ...
- Java ArrayList自动扩容机制
动态扩容 1.add(E e)方法中 ① ensureCapacityInternal(size+1),确保内部容量,size是添加前数组内元素的数量 ② elementData[size++] ...
- 面试都在问的「微服务」「RPC」「服务治理」「下一代微服务」一文带你彻底搞懂!
❝ 文章每周持续更新,各位的「三连」是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) ❞ 单体式应用程序 与微服务相对的另一个概念是传统的「单体式应用程 ...
- 详谈XSS防御方法
1.HttpOnly 严格的说,httponly并非为了对抗XSS,它解决的是XSS后的Cookie劫持攻击.Cookie设置了httponly之后,JavaScript读不到该cookie的值. ...
- 听说你还搞不定java中的==和equals?
相信很多读者关于==和equals懂了又懵,懵了又懂,如此循环,事实上可能是因为看到的博客文章之类的太多了,长篇大论,加上一段时间的洗礼之后就迷路了.本篇文章再一次理清楚.当然如果觉得本文太啰嗦的话, ...
- Vertica的这些事(四)——-vertica加密数据
通过创建 Secure Access Policies可以对vertica中的某一列数据进行加密: CREATE ACCESS POLICY ON [schema][tablename] FOR CO ...
- python爬虫之requests的高级使用
1.requests能上传文件 # 导入requests模块 import requests # 定义一个dict files = {'file': open('D:/360Downloads/1.t ...
- js中使用Timer来计时程序执行时 - [javascript] - [开发]
在我们开发过程中,我们也在不断的学习,以及优化自己的代码质量. 我们时常需要一个计时器,来对代码某段或者某些段执行进行计时,以评估代码运行质量,考虑是否优化. 以及优化后的直观对比. JavaScri ...