当前位置:首页 > Web开发 > 正文

用于拦截Action请求

2024-03-31 Web开发

标签:

一、简介

  前几篇文章都是讲ASP.NET Core MVC中的依赖注入(DI)与扩展点的,也许大家都发此刻ASP.NET CORE中所有的组件都是通过依赖注入来扩展的,而且面向一构成果就会有一组接口或抽象工厂来扩展成果,就如IControllerActivator这样的成果点在上篇文章(检察.NET Core源代码通过Autofac实现依赖注入到Controller属性)中也提到了,今天我们主要介绍一个大类似的扩展点,ASP.NET Core MVC中为我们供给了新的机制为Action Filters(也就是过滤器)进行依赖注入的扩展。

二、过滤器依赖注入

  在ASP.NET Core MVC中,框架中为我们供给了类型为 IFilter 的 Attributes 来装饰Action,用于拦截Action请求,这有在以前的版本中就有了,但是如果我们想结合依赖注入使用的话要使用IFilterFactory接口来扩展Action Filter的创建过程。

  2.1 IFilterFactory接口界说

public interface IFilterFactory : IFilter { IFilter CreateInstance([NotNull] IServiceProvider serviceProvider); }

  我们想要创建一个Filter Attribute并需要依赖注入的话一般想要的代码为:

public class FilterClass : ActionFilterAttribute { public FilterClass(IDependency1 dependency1, IDependency2 dependency2) { // ...use dependencies } }

  ASP.NET Core MVC中为我们供给了两种简单的IFilterFactory ServiceFilterAttribute 和 TypeFilterAttribute 。来个例子看看怎么使用。

public class HomeController: Controller { [TypeFilter(typeof(FilterClass))] [ServiceFilter(typeof(FilterClass))] public IActionResult Index() { return View(); } }

  2.2 ServiceFilterAttribute 

   其实看到名字,有些伴侣就能想到了,它是基于依赖注入的一个IFilterFactory,Service这个词强化了它是一个通过获取处事来实现依赖注入的,大家想到了什么?是不是GetService()? 没错,其实它的机制就是这个。

     要使用ServiceFilter就必需在依赖注入容器里注册对应的类型,好比下面的例子就要先将FilterClass类型注册到IOC容器里。

public void ConfigureServices(IServiceCollection services) { services.AddSingleton<FilterClass>(); services.AddMvc() }

  固然如果FilterClass类型的结构器需要注入类型时,也需要在IOC容器里进行注册才可以使用。

  我们来看下ServiceFilterAttribute的源代码:

public class ServiceFilterAttribute : Attribute, IFilterFactory, IOrderedFilter { public ServiceFilterAttribute([NotNull] Type type) { ServiceType = type; } public Type ServiceType { get; private set; } public int Order { get; set; } public IFilter CreateInstance([NotNull] IServiceProvider serviceProvider) { var service = serviceProvider.GetRequiredService(ServiceType); var filter = service as IFilter; if (filter == null) { throw new InvalidOperationException(Resources.FormatFilterFactoryAttribute_TypeMustImplementIFilter( typeof(ServiceFilterAttribute).Name, typeof(IFilter).Name)); } return filter; } }

  2.3 TypeFilterAttribute

   固然你也可以选择使用这个类似于ServiceFilter过滤器的TypeFilter过滤器,它也同样实现了IFilterFactory接口,并可以通过它创建出可使用依赖注入的过滤器来。之所以叫TypeFilter就是因为它并不需要在依赖注入容器里注册类型就能创建出过滤器,  我们来看下它的代码:

public class TypeFilterAttribute : Attribute, IFilterFactory, IOrderedFilter { private ObjectFactory factory; public TypeFilterAttribute([NotNull] Type type) { ImplementationType = type; } public object[] Arguments { get; set; } public Type ImplementationType { get; private set; } public int Order { get; set; } public IFilter CreateInstance([NotNull] IServiceProvider serviceProvider) { if (this.factory == null) { var argumentTypes = Arguments?.Select(a => a.GetType())?.ToArray(); this.factory = ActivatorUtilities.CreateFactory(ImplementationType, argumentTypes ?? Type.EmptyTypes); } return (IFilter)this.factory(serviceProvider, Arguments); } }

三、结语

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32329.html