Fork me on GitHub

Action过滤器重构

注:本文参照 NickChapsas的Attributes get a feature long-overdue in C# 11

  今天看一个泛型特性的例子,这个功能在C#11才受支持。

  在asp.net core mvc中,可以给action添加filter,达到拦截作用,实现如下:

public class MyFilter : IAsyncActionFilter
{
    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        Console.WriteLine("Action前执行");
        await next();
        Console.WriteLine("Action后执行");
    }
}

  使用方式,在Action上添加ServiceFilter特性即可,如下:

[ServiceFilter(typeof(MyFilter))]       
public IEnumerable<WeatherForecast> Get()
{
      return Enumerable.Range(1, 5).Select(index => new WeatherForecast
      {
           Date = DateTime.Now.AddDays(index),
           TemperatureC = Random.Shared.Next(-20, 55),
           Summary = Summaries[Random.Shared.Next(Summaries.Length)]
      })
      .ToArray();
}

  在运行前记得把MyFilter注放到Service容器中:

builder.Services.AddScoped<MyFilter>();

  为了支持C#11,在项目文件.csproj中,PropertyGroup中添加一行<LangVersion>preview</LangVersion>

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-preview.4.22251.1" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
  </ItemGroup>
</Project>

  现在就可以定义一个继承IFilterFactory的特性类了,并且是泛型的。

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class GSWFilterAttribute<TFilter> : Attribute, IFilterFactory, IOrderedFilter where TFilter : IAsyncActionFilter
{
    public bool IsReusable { get; set; }

    public int Order { get; set; }

    public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
    {
        if (serviceProvider != null)
        {
            var filter = (IFilterMetadata)serviceProvider.GetRequiredService(typeof(TFilter));
            if (filter is IFilterFactory filterFactory)
            {
                filter = filterFactory.CreateInstance(serviceProvider);
            }
            return filter;
        }
        else
        {
            throw new ArgumentNullException(nameof(serviceProvider));
        }
    }
}

  使用时,直接把泛型类型放上就可以了,如下:

[GSWFilter<MyFilter>]
public IEnumerable<WeatherForecast> Get()
{
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    { 
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

  虽然两种方法实现的功能是一样的,但后一种看起来更优雅一些。同时说明一下,如果多个MyFilter功能的过滤器,可以增加Order属性,如下:

[GSWFilter<MyFilter1>(Order = 2)]
[GSWFilter<MyFilter2>(Order = 1)]
public IEnumerable<WeatherForecast> Get()
  想要更快更方便的了解相关知识,可以关注微信公众号 

 

 

posted @ 2022-12-06 01:37  桂素伟  阅读(30)  评论(0编辑  收藏  举报