实现一个基于动态代理的 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优化委托的 DynamicInvoke
- 下一篇: 程序员过关斩将-- 工作好多年可能还未真