In my C# ASP.NET Core project, I have a class that is currently instantiated using a parameterless constructor. This class is used in hundreds of places throughout the solution.
I now need to refactor the class to support constructor dependency injection by requiring a parameter to be passed in (such as a string or a service). However, due to the large number of existing usages, I can't update all of them immediately.
Using optional constructor parameters is not a viable solution in my case — while it compiles, it causes runtime issues, possibly due to how the framework or dependency injection container handles nulls or default values.
I'm looking for a safe and maintainable way to introduce the new required constructor dependency without breaking existing code. I'm also trying to follow best practices and move toward dependency injection.
So far, I’ve considered options like adding an overloaded constructor, using a base class for shared logic, applying a factory or builder pattern, and marking the old constructor as obsolete to track usage.
What approach would you recommend to transition this class to constructor-based dependency injection without breaking the current system?
So far, I’ve considered options like adding an overloaded constructor- That sounds like a reasonable approach; you should try that one! Then you can piecemeal change things and eventually remove the default ctor.