• 在.NET Core中三种实现“可插拔”AOP编程方式(附源码)


    一看标题肯定会联想到使用动态编织的方式实现AOP编程,不过这不是作者本文讨论的重点。

    本文讨论另外三种在netcore中可实现的方式,Filter(过滤器,严格意义上它算是AOP方式),DynamicProxy(动态代理方式,JAVA上早已不是新鲜事),Middleware(netcore中间件所实现的AOP方式)

    什么是AOP编程

    在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

    换白话文就是:保证开闭原则的前提下,不修改某一个模块(或函数)的任何一行代码,从而实现对该模块的横向扩展。

    可插拔:即使抛弃AOP,核心内容仍然可以运行,降低耦合,提高可重用。

    创建一个“能说你好,某某某”的ASP .NET CORE WebApi项目

    创建项目步骤此处略过,呵呵。

    我们假定一个最简单的“Say Hello”作为项目需求,通过URL传递姓名,再返回“hello {name}”。因此创建一个简单模型PeopleModel,创建一个接口ISay,并用Say实现ISay中的“hello {name}”功能。

    1     public interface ISay
    2     {
    3         PeopleModel SayHello(PeopleModel peopleModel);
    4     }
    1     public class Say : ISay
    2     {
    3         public PeopleModel SayHello(PeopleModel peopleModel)
    4         {
    5             peopleModel.Name = $"hello {peopleModel.Name}";
    6             return peopleModel;
    7         }
    8     }
    1     public class PeopleModel
    2     {
    3         public string Name { get; set; } = "";
    4         public int Age { get; set; }
    5         public int Sex { get; set; }
    6     }

    再创建一个MVC控制器

     1     [Route("api/[controller]")]
     2     [ApiController]
     3     public class DemoController : ControllerBase
     4     {
     5         [HttpGet]
     6         public PeopleModel Get([FromQuery] PeopleModel peopleModel)
     7         {
     8             return Say.SayHello(peopleModel);
     9         }
    10     }

    很简单的,不做解释,以免浪费篇幅。

    DynamicProxy方式

    动态代理的方式在JAVA上很早就出现了,比如在Spring框架里面。而NET中利用Autofac和Castle这两个框架同样也可以实现动态代理。

    需要用到的框架如下:

    Autofac:提供容器控制
    Autofac.Extensions.DependencyInjection:对autofac依赖注入进行扩展
    Autofac.Extras.DynamicProxy:对autofac动态代理进行扩展
    Castle.Core:使用动态代理的实现

    相信autofac很多朋友都不陌生了,而配合Castle框架就能实现动态代理模式,我们新建一个拦截器类,名为InjectInterceptor,而且必须要实现IInterceptor(该接口在Castle.DynamicProxy中),完整代码如下:

     1 using System;
     2 using Castle.DynamicProxy;
     3 
     4 namespace InterceptDemo.Intercepts.Inject
     5 {
     6     public class InjectInterceptor : IInterceptor
     7     {
     8         public virtual void Intercept(IInvocation invocation)
     9         {
    10             PreProceed(invocation);
    11             invocation.Proceed();
    12             PostProceed(invocation);
    13         }
    14 
    15         private void PreProceed(IInvocation invocation)
    16         {
    17             Console.WriteLine($"{DateTime.Now} inject interceptor invoke preproceed"); 
    18         }
    19 
    20         private void PostProceed(IInvocation invocation)
    21         {
    22             Console.WriteLine($"{DateTime.Now} inject interceptor invoke postproceed");
    23         }
    24     }
    25 }

    当继承IInterceptor接口时,必须要实现Intercept虚方法,该方法将传递IInvocation接口参数,调用Proceed函数将会实现方法体以外的函数,就是切面以外的函数。使用时只需要通过特性即可实现AOP方式,我们稍微修改一下Say这个类,增加一句话:[Intercept(typeof(InjectInterceptor))]

    当然,还需要在autofac中实现注册才行,代码如下:

    var builder = new ContainerBuilder();
    builder.Populate(services);
    builder.RegisterType<Say>().As<ISay>().EnableInterfaceInterceptors();
    builder.RegisterType<InjectInterceptor>();

    Filter方式

    过滤器的方式就更加简单了,在ASP.NET框架中,使用过滤器的地方非常非常的多,笔者不作一一介绍,直接贴代码:

     1     public class ActionFilter : ActionFilterAttribute
     2     {
     3         public override void OnActionExecuting(ActionExecutingContext context)
     4         {
     5             Console.WriteLine($"{DateTime.Now} on action exceuting");
     6         }
     7 
     8         public override void OnActionExecuted(ActionExecutedContext context)
     9         {
    10             Console.WriteLine($"{DateTime.Now} on action exceuted");
    11         }
    12     }

    Middleware方式

     中间件不仅可以实现自定义管道,还也可以作为netcore invoke的AOP编程。我们先建立一个对ApplicationBuilder的扩展类

    1     public static class InterceptHandler
    2     {
    3         public static IApplicationBuilder UseInterceptMiddleware(this IApplicationBuilder app)
    4         {
    5             return app.UseMiddleware<InterceptMiddlware>();
    6         }
    7     }

    再建立一个中间件

     1 using System;
     2 using System.Threading.Tasks;
     3 using Microsoft.AspNetCore.Http;
     4 
     5 namespace InterceptDemo.Intercepts.Middleware
     6 {
     7     public class InterceptMiddlware
     8     {
     9         private readonly RequestDelegate _next;
    10         
    11         public InterceptMiddlware(RequestDelegate next)
    12         {
    13             _next = next;
    14         }
    15 
    16         public async Task Invoke(HttpContext context)
    17         {
    18             PreProceed(context);
    19             await _next(context);
    20             PostProceed(context);
    21         }
    22 
    23         private void PreProceed(HttpContext context)
    24         {
    25             Console.WriteLine($"{DateTime.Now} middleware invoke preproceed");
    26         }
    27 
    28         private void PostProceed(HttpContext context)
    29         {
    30             Console.WriteLine($"{DateTime.Now} middleware invoke postproceed");
    31         }
    32     }
    33 }

    运行结果如下

    总结一下

    在NETCORE中可以使用AOP的方式有很多很多,包括国内优秀的开源框架asp.netcore同样可以实现AOP编程模式。

    笔者所提供的三种AOP方式可适用如下

    Filter:身份验证,参数验证,处理耗时等等WEB处理级的服务。

    DynamicProxy:功能模块之间的解耦和重用服务。

    Middleware:Request和Response之间建立的通信等底层服务,必要时还可以实现自定义管道。

    感谢阅读!

    源码地址:https://github.com/steveleeCN87/C-.three.aop.programming

    作者:李知洲

    出处:http://stevelee.cnblogs.com

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。

  • 相关阅读:
    使用gdb跟踪Linux内核启动过程(从start_kernel到init进程启动)
    对一个简单的时间片轮转多道程序内核代码的浅析
    初识计算机工作过程
    React 中 路由 react-router-dom 的用法
    Vue Nuxt.js项目启动后可以在局域网内访问的配置方法
    node express async regeneratorRuntime is not defined (已解决)
    node+vue实现微信支付(沙箱)完整版,亲测可用
    node+vue实现支付宝支付(沙箱)完整版,亲测可用
    Vue.js中Line第三方登录api实现[亲测可用]
    React中WebSocket使用以及服务端崩溃重连
  • 原文地址:https://www.cnblogs.com/SteveLee/p/three_aop_programming.html
Copyright © 2020-2023  润新知