DWS Interview Question: What is Dependency Injection?... | Glassdoor

Interview Question

One Out of: Java and .NET (Developer) Or Solution Architect (Not Decided) Interview Melbourne (Australia)

What is Dependency Injection? How would I go about using it

  in "day to day programming"?
design patterns, dependency injection, s-o-l-i-d (uppercase)

Interview Answer

1 Answer


First: Dependency _Inversion_ (not injection) is when before, a system that is developed would call little components (the dependencies) as they were given, whereas now, with inverted dependency, the system defines and publishes abstract interfaces as dependencies that the components had better fulfill, and the components are written to just fulfill the abstract interfaces.

Based on this principle, Dependency _Injection_ is where a system defines/publishes its dependencies, but the actual components or elements that fulfill the dependencies are supplied ("injected") later, possibly at runtime.

"Day to day programming" had me dumbfounded. What the interviewer meant was: There are some of Dependency Injection Frameworks out there, where the code that you write gets called by the framework, not the other way round, and the framework "injects" the dependencies your code needs. Example frameworks are Spring in the Java sphere and Managed Extensibility Framework in the .Net sphere. "Day to day programming" meant: "When you program without a framework".

It still had me dumbfounded because in .Net, you never program without a "framework" -- the library itself is called the .Net framework.

The expected answer was (probably) that dependencies can also be injected manually by code that calls your code. Dependencies can be supplied to a class that needs them via public properties, or via the constructor, or via interfaces that the class that receives the dependency defines, e.g. via a factory method.

Interview Candidate on Oct 8, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.