当前位置: 首页 > news >正文

广安网站建设公司域名备案 没有网站吗

广安网站建设公司,域名备案 没有网站吗,100元建网站,dede网站建设的个人总结实现一个基于动态代理的 AOPIntro上次看基于动态代理的 AOP 框架实现#xff0c;立了一个 Flag#xff0c; 自己写一个简单的 AOP 实现示例#xff0c;今天过来填坑了目前的实现是基于 Emit 来做的#xff0c;后面有时间再写一个基于 Roslyn 来实现的示例效果演示演示代码立了一个 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.CreateInterfaceProxyITestService(); var testService ProxyGenerator.Instance.CreateInterfaceProxyITestService, TestService(); // var testService ProxyGenerator.Instance.CreateClassProxyTestService(); // 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(); 输出效果整体结构ProxyGeneratorProxyGenerator 代理生成器用来创建代理对象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 CreateInterfaceProxyTInterface(this ProxyGenerator proxyGenerator) (TInterface)proxyGenerator.CreateInterfaceProxy(typeof(TInterface));public static TInterface CreateInterfaceProxyTInterface, TImplement(this ProxyGenerator proxyGenerator) where TImplement : TInterface (TInterface)proxyGenerator.CreateInterfaceProxy(typeof(TInterface), typeof(TImplement));public static TClass CreateClassProxyTClass(this ProxyGenerator proxyGenerator) where TClass : class (TClass)proxyGenerator.CreateClassProxy(typeof(TClass));public static TClass CreateClassProxyTClass, TImplement(this ProxyGenerator proxyGenerator) where TImplement : TClass (TClass)proxyGenerator.CreateClassProxy(typeof(TClass), typeof(TImplement)); } AbstractAspectAbstractAspect 切面抽象类继承了 Attribute可以继承它来实现自己的切面逻辑public abstract class AbstractAspect : Attribute {public abstract void Invoke(MethodInvocationContext methodInvocationContext, Action next); } MethodInvocationContextMethodInvocationContext 方法执行上下文包含了执行方法时的原始方法信息以及代理方法信息方法参数方法返回值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 ConcurrentDictionarystring, ActionMethodInvocationContext _aspectDelegates new ConcurrentDictionarystring, ActionMethodInvocationContext(); public static void InvokeAspectDelegate(MethodInvocationContext context) {var action _aspectDelegates.GetOrAdd(${context.ProxyMethod.DeclaringType}.{context.ProxyMethod}, m {// 获取切面逻辑这里根据切面类型做了一个去重var aspects new ListAbstractAspect(8);if (context.MethodBase ! null){// 获取类方法上的切面逻辑foreach (var aspect in context.MethodBase.GetCustomAttributesAbstractAspect()){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.GetCustomAttributesAbstractAspect()){if (!aspects.Exists(x x.GetType() aspect.GetType())){aspects.Add(aspect);}}}}// 构建切面逻辑执行管道类似于 asp.net core 里的请求管道 以原始方法调用作为中间件的最后一步var builder PipelineBuilder.CreateMethodInvocationContext(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.ForITestService(); setting.PropertySetter(xx.TestProp).InterceptWithTryInterceptor().InterceptWithTryInterceptor1(); setting.Method(x x.Test2()).InterceptWithTryInterceptor().InterceptWithTryInterceptor1(); 然后基于 AspectCore 和 Castle.Core 来实现具体的 AOP 功能暂时先想一下争取尽快的发布一个基本可用的版本然后之前基于 EF Core 的自动审计也可以基于 AOP 来实现了这样就不需要显示继承 AuditDbContext 了~文章所有源码可以在 Github 上获取到Github 地址https://github.com/WeihanLi/SamplesInPractice/tree/master/AopSampleReference让 .NET 轻松构建中间件模式代码让 .NET 轻松构建中间件模式代码--支持中间件管道的中断和分支NET 下基于动态代理的 AOP 框架实现揭秘EF Core 数据变更自动审计设计AopSampleAspectCore
http://www.zqtcl.cn/news/578020/

相关文章:

  • 怎么样开始做网站网站建设 营业执照 经营范围
  • 威海做网站网站建设方案书 模版
  • 泗阳做网站南昌建设
  • 做企业网站用什么软件深圳制作企业网站
  • 大连微信网站开发兰州网站建设模板
  • 建设项目安监备案网站外贸 网站 seo
  • 企慕网站建设网络推广合肥市网站制作
  • 做空比特币网站大气简约企业网站模板免费下载
  • 坪山网站建设行业现状做网站能月入10万
  • 个人网站有什么内容广西网站建设推广
  • 安徽教育云网站建设网站seo诊断的主要内容
  • 网站建设例子开发工具宏怎么使用
  • 新乡做网站公司哪个地区网站建设好
  • 网站模板怎么编辑网站定制化
  • 利于优化的网站网络科技公司怎么赚钱
  • 制作网站的步骤和方法做物流的网站有哪些功能
  • vs做网站图片明明在文件夹里却找不到中国建筑网官网找客户信息
  • WordPress仿站培训黑龙江新闻夜航
  • 如何利用开源代码做网站济南做网站互联网公司有哪些
  • 生意网app下载官网郑州做网站优化公
  • wordpress网站更换域名wordpress 小工具定制
  • 上海做机床的公司网站设计网站怎样做色卡
  • 一个网站怎么绑定很多个域名做网站后台应该谁来做
  • 跑纸活做网站加大门户网站安全制度建设
  • 多商户开源商城seo对网店的作用有哪些
  • 提供微信网站建设福州seo建站
  • 泉州市住房与城乡建设网站潍坊网站建设方案外包
  • 网络文化经营许可证怎么申请免费seo提交工具
  • 网站建设 需求分析报告手机网站微信网站开发
  • 做司法考试题目的网站建站中企动力