I have a large codebase with a lot of "anti-pattern" singletons, utility classes with static methods and classes creating their own dependencies using new
keyword. It makes a code very difficult to test.
I want to gradually migrate code to dependency injection container (in my case it's Guice
, because it is a GWT
project). From my understanding of dependency injection, it's all or nothing. Either all classes are managed by Spring/Guice or none. Since the codebase is large I cannot transform the code over night. So I need a way to do it gradually.
The problem is that when I start with a class that needs to be injected into other classes, I cannot use a simple @Inject
in those classes, because those classes are not managed by container yet. So this creates a long chain up to the "top" classes that are not injected anywhere.
The only way I see is to make an Injector
/ application context globally available through a singleton for the time being, so that other classes can get a managed beans from it. But it contradicts important idea of not revealing the composition root
to the application.
Another approach would be bottom-up: to start with "high-level" classes, include them into dependency injection container and slowly move down to "smaller" classes. But then I have to wait a long time, since I can test those smaller classes that still depends on globals/statics.
What would be the way to achieve such gradual migration?
P.S. The question Gradual approaches to dependency injection is similar in title, but it doesn't answer my question.