Urgent.News

What's breaking now, across thousands of outlets.

Tech

The Factory Class I Finally Deleted

The PR added a WhatsApp notification channel. Three changes: a new WhatsAppSender class, fair. Its registration, fair. And NotificationSenderFactory , where a switch statement quietly grew its fourth arm. I've been approving some version of that third change since 2016 — different codebases, same class, same switch. Keyed services made it deletable back in .NET 8, and this week, on .NET 10, I…

Abstract editorial illustration

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.

The 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.

The 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.

Two 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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

CityLibrary

Django and React.js library management system 🚀 Dev.to Article Summary CityLibrary is a full-stack library management system built with React 19 , Django 5 , and Django REST Framework . It features a type-safe, responsive frontend powered by TanStack Router , TanStack Query , and TanStack Store , along with a Django Unfold administrative…

More from Sunday 2 August →