1

Do we achieve Dependency Inversion Principle using Dependency Injection technique?

Am I correct?

Dave Schweisguth
37.8k10 gold badges102 silver badges123 bronze badges
asked Apr 14, 2011 at 13:24
0

2 Answers 2

2

Yes. Dependency inversion says "do not seek your dependencies yourself, they will be supplied to you." Dependency injection is a way of supplying the dependencies to the object from the outside.

answered Apr 14, 2011 at 13:29
Sign up to request clarification or add additional context in comments.

2 Comments

Is dependency inversion one of the applications of Inversion of control principle?
That’s a lot of words and I’m not very versed in these things, but I’d say yes. It’s all about the same principle: Keep your design loosely coupled, your objects should work as Lego bricks that can be assembled in any way supported by the interfaces.
1

yes it is

The Dependency Inversion Principle in two sentences:

High level modules should not depend upon low level modules. Both should depend upon abstractions.

and

Abstractions should not depend upon details. Details should depend upon abstractions.

while

The Dependency Injection Pattern is one of the concrete solutions to the application/code composition problem. And yes this solution is implementation of the Dependency Inversion Principle. Probably the most known place to read is Martin Fowler - Inversion of Control Containers and the Dependency Injection pattern.

answered Apr 14, 2011 at 13:35

2 Comments

+1 for explaining it in simple terms :) So, dependency inversion principle is a corollary to the "Hollywood principle" which states that high level modules should depend on low level modules but not the other way?
Actually, this answer is wrong. DIP requires that a high-level module depends only on abstractions defined at this same high level. Then, low-level modules implement the abstraction defined at the high-level. This allows the high-level module (including the high-level abstractions it uses) to be reused in different contexts/applications. DI, on the other hand, is simply a way for objects to obtain references to other objects, through configurable runtime resolution without lookup calls. So, you can have designs that employ DIP but not DI, and vice-versa; they really are independent ideas.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.