NET 10 Keyed Services Eliminate the Redundant Factory Class Pattern
A long-standing software design pattern — the notification sender factory class with a growing switch statement — has been made obsolete by keyed services in .NET 8 and refined further in .NET 10. The factory pattern, used to resolve interface implementations based on a string key, was effectively a service locator anti-pattern dressed up with a friendlier name. With keyed services, developers can register each implementation directly against a named key, removing the need for a separate factory class and its associated boilerplate registrations. Call sites that know the implementation at compile time can use the FromKeyedServices attribute, while runtime lookups can query IServiceProvider directly by key. The result is that the resolution logic lives in the dependency injection registrations themselves, reducing drift between the factory and the actual service mappings.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in