Reputation: 1338
I understand the benefit of DI from the Singleton point of view and the reduction of boiler-plate code. But I found this on wikipedia too:
Another benefit is that it offers configuration flexibility because alternative implementations of a given service can be used without recompiling code
When I use Spring or Guice it always makes a contract between 1 service and 1 implementation of it. Am I missing a feature or understanding the statement incorrectly?
Upvotes: 0
Views: 340
Reputation: 172666
You would normally have to recompile the part of the application that contains the configuration, but the rest of the application can stay the same. When those parts are put in separate modules / assemblies, recompilation of those parts is not needed. When you configure the container using XML (in theory) nothing needs to be recompiled.
You could even go one step further and change behavior at runtime (using decorators for instance) if you wish.
Upvotes: 2
Reputation: 41137
If the decision of which implementation to use for a given service is in configuration, you can change that decision purely in configuration with no code changes, as long as the alternative implementation you want to use already exists.
This is largely the case quite naturally with Spring, where this decision is usually in XML configuration files read at application startup.
Upvotes: 0