Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interviewer asks: "What's the value of dependency injection?" [duplicate]

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?

like image 497
arezzo Avatar asked Dec 12 '22 08:12

arezzo


2 Answers

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.

like image 64
Mark Byers Avatar answered Jun 07 '23 19:06

Mark Byers


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.

like image 28
SLaks Avatar answered Jun 07 '23 20:06

SLaks