liveBook · Manning

IFilterFactory

As explained in ASP.NET Core in Action, 3e

IFilterFactory is an ASP.NET Core MVC interface for attributes that create filter instances for the framework.

IFilterFactory lets an attribute create a filter instance for ASP.NET Core MVC. It combines attribute-based usage with dependency injection for the filter’s services. section 22.3

The framework uses the factory in this sequence:

  • Scans controllers and actions when the application loads and builds each action’s filter pipeline. section 22.3
  • Calls CreateInstance() when a decorated action runs. section 22.3
  • Creates the filter and uses the dependency-injection container to supply dependencies such as RecipeService. section 22.3
  • Use TypeFilterAttribute to resolve filter dependencies, or ServiceFilterAttribute to resolve the registered filter itself. section 22.3
Figure 22.3 The framework scans your app on startup to find both filters and attributes that implement IFilterFactory. At runtime, the framework calls CreateInstance() to get an instance of the filter.
CH22_F03_Lock3

The figure shows startup discovery of filters and IFilterFactory attributes followed by runtime calls to CreateInstance(). section 22.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains startup discovery, filter-pipeline construction, and runtime CreateInstance() calls.
Examples
Uses EnsureRecipeExistsAttribute and EnsureRecipeExistsFilter with RecipeService, including ServiceFilterAttribute registration and decoration.
What you can do after
Apply it via ASP.NET Core MVC to decorate actions while injecting services into their filters.
How it connects
Connects dependency injection, MVC filters, filter pipelines, TypeFilterAttribute, and ServiceFilterAttribute.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.