A factory function creates and returns a configured object or function. It separates the decision about what to create from the code that uses the result. section 5.4.3 section 9.6
In ASP.NET Core, factories support two closely related jobs:
- An endpoint-filter factory examines an endpoint’s method information and returns a filter function tailored to that endpoint. section 5.4.3
- A dependency-injection factory describes how to create a service, including services that are available only while the application is running. section 9.6
- Prefer constructor injection over resolving services with GetService() or GetRequiredService() inside a factory when possible. section 9.6
One factory, several endpoint signatures
Adding AddEndpointFilterFactory(ValidationHelper.ValidateIdFactory) to both GET and POST endpoints lets one factory inspect each method signature and create a filter that locates the id parameter wherever it appears. section 5.4.3
Figure 5.10 A filter factory is a generalized way to add endpoint filters. The factory reads details about the endpoint, such as its method signature, and builds a filter function. This function is incorporated into the final filter pipeline for the endpoint. The build step means that a single filter factory can create filters for multiple endpoints with different method signatures.
The factory reads endpoint details, builds a filter function, and places that function into the endpoint’s final filter pipeline. section 5.4.3










