企业百度网站怎么做wordpress又拍云cdn伪静态
企业百度网站怎么做,wordpress又拍云cdn伪静态,网站的建设目标文档,深圳建筑信息平台FluentAspects -- 基于 Fluent API 的 AopIntro上次我们做了一个简单的 AOP 实现示例#xff0c;但是实现起来主要是基于 Attribute 来做的#xff0c;对于代码的侵入性太强#xff0c;于是尝试实现基于 Fluent API 的方式来做 AOP 。抽象 InterceptorResolver原来获取方法执… FluentAspects -- 基于 Fluent API 的 AopIntro上次我们做了一个简单的 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, FuncTask next);
}
自定义 Interceptor 只需要继承这个接口实现相应的逻辑就好了获取 IInterceptorResolver 接口定义public interface IInterceptorResolver
{IReadOnlyCollectionIInterceptor 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} ...);}
}
测试 Interceptorinternal class LogInterceptor : IInterceptor
{public async Task Invoke(IInvocation invocation, FuncTask 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().WithLogInterceptor();// 对 Svc3 类型禁用拦截器options.NoInterceptTypeSvc3();// Svc4 类型的 Invoke3() 方法禁用拦截器options.NoInterceptMethodSvc4(s s.Invoke3());});services.AddTransientProxySvc4();var serviceProvider services.BuildServiceProvider();var proxyFactory serviceProvider.GetRequiredServiceIProxyFactory();var svc1 proxyFactory.CreateProxyISvc1();svc1.Invoke();Console.WriteLine();var svc2 proxyFactory.CreateProxyISvc2, Svc2();svc2.Invoke();Console.WriteLine();var svc3 proxyFactory.CreateProxySvc3();svc3.Invoke();Console.WriteLine();var svc4 proxyFactory.CreateProxyWithTargetISvc2, Svc2(new Svc2());svc4.Invoke();Console.WriteLine();// 直接从注册的服务中获取var svc5 serviceProvider.GetRequiredServiceSvc4();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-FrameworkReferencehttps://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common/Aspecthttps://github.com/WeihanLi/SamplesInPractice/blob/master/FluentAspectSample/Program.cshttps://github.com/dotnetcore/AspectCore-Framework
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/90659.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!