Reputation: 1217
I've inherited some code that has a class AuthenticationManager with all static methods.
Im introducing DI and wanted to add a constructor that took a dependency UserController
UserController _userController;
public AuthenticationManager(UserController userCont)
{
_userController = userCont;
}
Now Im getting the compile time error as a non-static variable is referenced from a static method. What would your best practice recommendation be to get this to work with the minmimal changes to this class and the calling code?
We're using the SimpleServiceLocator as the IOC container.
Upvotes: 7
Views: 1818
Reputation: 22171
Well it depends on how often the class is used throughout the code. You'll likely want to create an IAuthenticationManager
interface that includes methods that match the static methods you want to replace with instance methods. Then you could create an AuthenticationManager
class that implements the interface, and accepts the UserController
dependency via its constructor.
You would then need to replace all the static method call-sites the instance methods. You would probably want to inject an IAuthenticationManager
into the classes via a constructor or a property. If need-be, you could also pass an IAuthenticationManager
to the methods (at the call-sites) as a parameter.
Unfortunately replacing static methods takes quite a bit of refactoring. It is worth the effort though. It opens up the door for unit-testing.
Keep in mind that you can always refactor one method at a time by extracting an interface for one of the static methods. Do each method one at a time to take a step-wise approach to your refactoring (in other words, each method gets its own interface).
I would recommend taking a look at this book if you can: Working Effectively With Legacy Code. Great book that covers all sort of situation like this one.
Upvotes: 9