Possible Duplicate:
What is dependency injection?
I respond: "You don't need to use the new
operator."
Interviewer's response: "<SIGH>
. That's it?"
That's all I could come up with.
What would have been a more correct response if that is not correct?
It decouples a component from its external dependencies (e.g. other libraries, databases, etc.) allowing you to easily change them - even at runtime.
This can (for example) be useful in automated testing as you can inject mock objects via the public API.
Dependency injection decouples classes from the services they depend on, allowing you to register a set of services once and use them throughout your codebase.
This allows you to switch implementations non-intrusively, especially if the implementations are unit-tested.
It also allows you to put different service implementations with the same classes for different use-cases, such as web vs. GUI vs. testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With