{
  "id": 34178,
  "title": "The Factory Class I Finally Deleted",
  "url": "https://urgent.news/2026/08/02/the-factory-class-i-finally-deleted",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-02T07:14:50.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/ssukhpinder/the-factory-class-i-finally-deleted-3oap"
  },
  "original_language": "en",
  "account": "The article discusses the author's decision to delete a class named \"NotificationSenderFactory\" that was previously used for managing notification senders in multiple codebases. The class, added in 2016, served as a factory or resolver with a switch statement to create instances of different notification sender implementations based on the channel. The article highlights two ways to write this class, both of which have their drawbacks: either the factory manages the senders, making it dependent on their constructors, or it wraps the IServiceProvider, which is considered a service locator pattern. The author also mentions the registration tax associated with the factory, including the need to register each concrete type and the factory itself.\n\nThe article then introduces an alternative approach using keyed services, which eliminates the need for the NotificationSenderFactory class and brings the key from runtime into the registration process. This change allows for more flexibility and cleaner code, as the dependencies are now explicitly stated in the constructors of the dependent classes. The author provides examples of using keyed services with the AddKeyedScoped method and demonstrates a DailyDigestService class that takes its notification sender dependency directly through the FromKeyedServices attribute.\n\nThe author shares the results of a small demonstration app, showing how the keyed services approach performs on POST requests to different notification endpoint URLs. The exchanges demonstrate the successful sending of notifications via email, push, and an additional \"digest\" notification, proving the effectiveness of this new approach.\n\nTwo surprising findings from the demonstration are revealed: keyed registrations are not visible to plain enumeration, and the exception message does not include the offending key value. The author recommends logging the key at the call site to provide more context when exceptions occur. Overall, the article emphasizes the benefits of using keyed services for managing dependencies in C# applications, simplifying code, and improving maintainability.",
  "summary": "The article discusses the author's experience with a NotificationSenderFactory class in a .NET application, which was used to create different notification sender implementations based on a channel type. The author had been using this class for years across various codebases but finally decided to delete it in .NET 10. The class had a switch statement to determine which sender to create based on the channel type, and it relied on an IServiceProvider to resolve the senders. The author highlights that there are only two ways to write this class, both of which have drawbacks, including the factory pulling in unwanted dependencies and the registration tax of adding multiple registrations for each sender. The author proposes using keyed services as a cleaner solution, eliminating the need for the factory class altogether.",
  "key_points": [
    "Author deletes NotificationSenderFactory class, added in 2016, to streamline code",
    "Introduces keyed services approach, eliminating factory dependency on senders",
    "Demonstrates DailyDigestService using AddKeyedScoped method for cleaner dependencies"
  ],
  "editors_take": null,
  "illustration": "https://urgent.news/ill/34178.png",
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}