ServiceFilter和TypeFilter-注入这些过滤器有什么区别? [英] ServiceFilter and TypeFilter - what is the difference in injection those filters?

查看:567
本文介绍了ServiceFilter和TypeFilter-注入这些过滤器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ServiceFilter,我们必须在Startup.cs中注册. TypeFilter是由Microsoft.Extensions.DependencyInjection.ObjectFactory注入的,我们不需要注册该过滤器.

那么什么时候应该使用ServiceFilter和何时使用TypeFilter?

解决方案

根据 Pro ASP.NET核心MVC 2 书.第19章:过滤器,第615页

使用TypeFilter属性时,过滤器的新实例 为每个请求创建类.这与以下行为相同 直接将过滤器用作属性,但TypeFilter除外 属性允许过滤器类声明依赖项,这些依赖项是 通过服务提供商解决. ServiceFilter属性 更进一步,并使用服务提供商来创建过滤器 目的.这允许将过滤器对象置于生命周期内 管理.

差异 由于ServiceFilter使用ServiceProvider来解析所讨论的过滤器的实例,因此您可以控制在启动类中注册的过滤器的生命周期:

services.AddSingleton<TimeFilter>();

在上面的代码行中,将仅为MVC应用程序生命周期创建一次TimeFilter(不适用于每个http请求生命周期或当客户要求时),而不会为所有http请求提供服务使用TypeFilter是因为您无法指示MVC框架何时实例化和配置在TypeFilter下使用的过滤器.

如果将过滤器注册为Singleton,则仅创建该过滤器的一个实例,这意味着CLR的工作量减少,而TypeFilter会为每个http请求创建新的过滤器类实例. >

用法 假设您将TypeFilter应用于两个操作方法,对于每个HTTP请求,将创建该TypeFilter的新实例,调用构造函数并注入依赖项(您可以使用以下方法控制依赖项的生命周期: Service Provider).相反,使用ServiceFilter可以确定其Singleton还是ScopedTransient.如果Singleton,则仅为所有请求创建一个实例.

要记住的关键内容

这是我们要使用ServiceFilterService Provider管理的过滤器类型的生命周期.如果过滤器具有依赖性,则我们已经像往常一样使用Service Provider对其进行了管理.

ServiceFilter we must register in Startup.cs. TypeFilter is injected by Microsoft.Extensions.DependencyInjection.ObjectFactory, we don't need register that filter.

So when we should use ServiceFilter and when TypeFilter ?

解决方案

According to Pro ASP.NET Core MVC 2 book. Chapter 19: Filters, page # 615

When using the TypeFilter attribute, a new instance of the filter class is created for every request. This is the same behavior as applying a filter directly as an attribute, except that the TypeFilter attribute allows a filter class to declare dependencies that are resolved through the service provider. The ServiceFilter attribute goes a step further and uses the service provider to create the filter object. This allows filter objects to be placed under life-cycle management as well.

THE DIFFERENCE Since the ServiceFilter uses the ServiceProvider to resolve the instance of the filter in question, you have control over the lifecycle of the filter which is registered in the startup class:

services.AddSingleton<TimeFilter>();

From above line of code, the TimeFilter will only be created once for the MVC application lifecycle (not for each http request life cycle or when client asks for it) that will serve for all the http requests which is not possible using TypeFilter because there is no way you can instruct MVC framework when to instantiate and dispose the filter used under TypeFilter.

If the filter is registered as Singleton then only one instance of that filter is created which means less work for CLR which is unlike in case of TypeFilter that creates new instance of filter class for each http request.

THE USAGE Say you have a TypeFilter applied on two action methods, for each HTTP request, a new instance of that TypeFilter will be created, the constructor will be called and dependencies will be injected (you can control the life cycle of dependencies using the Service Provider). In contrast, with ServiceFilter you decide if its Singleton or Scoped or Transient. If its Singleton then only one instance is created for all the requests.

KEY THING TO REMEMBER

It’s the filter type’s life cycle that we want to manage by using ServiceFilter and Service Provider. If the filter has dependencies, we already manage that using Service Provider like we normally do.

这篇关于ServiceFilter和TypeFilter-注入这些过滤器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆