Questions tagged [dependency-inversion]

For questions about the Dependency Inversion Principle in object-oriented programming, one of the SOLID principles originated by Robert C. Martin. It states that modules should, "depend on abstractions, not concretions."

Robert Martin covered the and in articles published in early 1996. His next article went on to discuss the implications of applying those principles to object-oriented programs.

The structure that results from rigorous use of [the OCP and the LSP] can be generalized into a principle all by itself. I call it “The Dependency Inversion Principle” (DIP).

Martin suggests that bad software design is rigid, fragile, and immobile; and these characteristics have a common cause.

What is it that makes a design rigid, fragile and immobile? It is the interdependence of the modules within that design.

The DIP seeks to alleviate this interdependence in two ways. It states.

  • High-level modules should not depend upon low-level modules. Both should depend upon abstractions.
  • Abstractions should not depend upon details. Details should depend upon abstractions.

Martin achieves these goals by taking traditional, procedural dependencies running from a high-level module to a low-level module and replacing them with abstractions (e.g. interfaces) that exist independently from the low-level modules which implement them.

The result of introducing abstractions that live outside of low-level modules is a reversal (i.e. inversion) to the previous direction of dependency. Rather than a dependency from high-level down to low-level modules, the low-level modules have dependencies pointing up to their abstractions.

Martin later included the DIP as the fifth of his .

See the DIP article under Principles of OOD.

119 questions
222
votes
16 answers

What is the dependency inversion principle and why is it important?

What is the dependency inversion principle and why is it important?
127
votes
4 answers

Difference between dependency injection and dependency inversion

Two design patterns namely Dependency Injection and Dependency Inversion exist out there, Articles are there in the net trying to explain the difference. But the need to explain it in easier words is still there. Any one out there to come up to ? I…
58
votes
4 answers

The non-generic method 'IServiceProvider.GetService(Type)' cannot be used with type arguments

I am using .NET Core dependency injection, but when I am trying to get the service in another class, I am getting the 'IServiceProvider.GetService(Type)' cannot be used with type arguments' error. What does this error means? I understand that a…
57913
  • 783
  • 1
  • 6
  • 13
22
votes
2 answers

What are "High-level modules" and "low-level modules" (in the context of Dependency inversion principle)?

I was reading Wikipedia's definition of Dependency inversion principle, and it uses two terms High-level modules and low-level modules, which I wasn't able to figure out. What are they and what does Dependency inversion principle have to do with…
19
votes
5 answers

What is difference between the Open/Closed Principle and the Dependency Inversion Principle?

The DIP states: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. And the OCP states: Software entities…
14
votes
4 answers

Dependency inversion principle in JavaScript

Is anyone able to help illustrate Dependency inversion principle in JavaScript jQuery? Which would highlight and explain these 2 points: A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B.…
13
votes
1 answer

Unit-Testing OSGi-Components

I'm currently thinking of "How to design an OSGi component so that it's easy to write tests for it with frameworks like jUnit and Mockito". Mocking inter-bundle-dependencies is quite easy since OSGi strengthens the DIP (Dependency Inversion…
Marc-Christian Schulze
  • 3,154
  • 3
  • 35
  • 45
12
votes
2 answers

Dependency Inversion Principle in PHP

Since PHP is a loosely typed language, how can the DIP principle be applied in PHP? A practical example would be greatly appreciated.
johnlemon
  • 20,761
  • 42
  • 119
  • 178
12
votes
2 answers

Multiple inheritance for R6 classes

Actual question What are my options to workaround the fact that R6 does not support multiple inheritance? Disclaimer I know that R is primarily a functional language. However, it does also have very powerful object-orientation built in. Plus: I…
Rappster
  • 12,762
  • 7
  • 71
  • 120
10
votes
2 answers

Dependency inversion (from S.O.L.I.D principles) in C++

After reading and watching much about SOLID principles I was very keen to use these principles in my work (mostly C++ development) since I do think they are good principles and that they indeed will bring much benefit to the quality of my code,…
dkish
  • 319
  • 3
  • 13
10
votes
3 answers

What is the difference between Dependency Inversion and the Separated Interface pattern (or Code to interface in general)?

I'm not able to figure out the difference between the Dependency Inversion Principle (one of the S.O.L.I.D principles) and the general 'Code to Interfaces' or Separated Interface pattern. All of them advocate creation of a layer of abstraction to…
aknon
  • 1,408
  • 3
  • 18
  • 29
9
votes
1 answer

Does dependency inversion principle mean that I have to create an interface for every module?

If I want my code to follow SOLID principles, specifically the dependency inversion principle, does that mean that I have to create an interface (abstraction) for every module, even if it has only one implementation ? In my opinion, and according to…
8
votes
1 answer

Which effects does the Dependency Inversion Principle have to a project structure?

In case I want to use the DIP to develop a hypothetical modular C++ project. Because of the modularity I choose to implement one specific feature completely in one library A. Another library B (or two, or three ...) is using this feature (e.g. a…
7
votes
4 answers

Are "Dependency Inversion" and "Design to Interfaces" the same principles?

Do the "Dependency Inversion Principle" (DIP) and "Design to Interfaces Principle" express the same principle? If not, what would be the difference? EDIT To clarify and narrow down the context a bit: by interface I mean a programmatic interface,…
eljenso
  • 16,789
  • 6
  • 57
  • 63
7
votes
1 answer

Finding the High-Level and Low-Level modules in dependencies for applying Dependency Inversion Principe

The Dependency Inversion Principle say: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. How can I practically…
Masoud
  • 8,020
  • 12
  • 62
  • 123
1
2 3 4 5 6 7 8