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
6
votes
1 answer

What is the difference between the "Open/closed principle" and the "Dependency inversion principle"?

I read articles about S.O.L.I.D. but I don't see any difference between OCP and DIP. Look at this example of OCP: http://www.oodesign.com/open-close-principle.html The code which holds OCP also fulfils the DIP. Can anyone give me an example of…
6
votes
3 answers

SOLID Design Principles : Liskov Substitution Principle and Dependency Inversion Principle

Just a thought and a question to the Stack Overflow and Microsoft Development Community about the OO software design principles called SOLID. What is the difference between the Liskov Substitution Principle and the Dependency Inversion Principle…
6
votes
2 answers

How to implement a Typescript interface to an es5-style "class"?

The way we can implement an Interface to an es6 class is pretty straightforward: interface IDog { bark(): void } class Dog implements IDog { bark(): void { } } The question is: how to implement the same interface to this…
Seu Madruga
  • 325
  • 7
  • 13
5
votes
1 answer

Dependency Inversion Principle: High Level and Low Level module example

I was going through the following link to understand what high-level and low-level modules mean in the context of Dependency Inversion Principle. As per the explanation given there, is the following code snippet a good/appropriate example? public…
Sang Suantak
  • 5,213
  • 1
  • 27
  • 46
4
votes
1 answer

Use type classes to implement dependency inversion in a Haskell application?

One major architectural goal when designing large applications is to reduce coupling and dependencies. By dependencies, I mean source-code dependencies, when one function or data type uses another function or another type. A high-level architecture…
4
votes
1 answer

How can I apply the dependency inversion principle when using third party libraries?

I was reading about the dependency inversion principle and as far as I understand the relation is inverted because package A (high level) defines the interface and package B (low level) implements the interface, instead of directly invoking the…
user11917850
4
votes
3 answers

Does Inheritance contradict Dependency Inversion Principle

The dependency Inversion principle says that (Head First Java): Depend upon abstractions. Do not depend upon concrete classes. What does it mean with respect to inheritance? As a subclass depends on concrete class. I am asking for a case when say…
lynxx
  • 544
  • 3
  • 18
4
votes
2 answers

Clarification on the Dependency Inversion Principle

Excuse me for cross-posting on Software Engineering, didn't know that it was frowned upon. The answer I was got there was exactly what I was looking for, for those curious: https://softwareengineering.stackexchange.com/a/347143/269571 Original…
4
votes
2 answers

Javascript dependency injection & DIP in node: require vs constructor injection

I'm new to NodeJs development coming from the .NET world i'm searching the web for best practices regrading DI / DIP in Javascript In .NET i would declare my dependencies at the constructor whereas in javascript i see a common pattern is to declare…
4
votes
2 answers

Is this a valid use of DIP (SOLID)?

I would like to ask if that implementation of classes Genotypes and Individual violates the Dependency Inversion Principle? If so, how to fix that? Here is the code: public interface IGenotype { //some code... } public abstract class…
4
votes
2 answers

Design: Class circular dependency?

I've been reading different answers here about solving the circular dependency in software design and I still can't get it. Please help me understand it. Let's say I have class A and class B which call methods of each other: class A { public: …
4
votes
2 answers

Explain this motivational poster about Dependency Inversion Principle

In this blog post, the Dependency Inversion Principle was described by this motivational poster: I don't understand what the poster means: How does soldering a lamp directly to the wall violate dependency inversion principle and how does having…
Ming-Tang
  • 17,410
  • 8
  • 38
  • 76
3
votes
1 answer

Dependency Inversion Principle. Why can I access classes of a non-directly referenced project in ASP.NET Core 5?

Imagine I have four projects in my solution: UI <- startup project Domain Repository Boot The UI projects has dependencies to the Domain and Boot projects. The Boot project has dependencies to the Domain and Repository projects for DI container…
3
votes
0 answers

Trying to deal with circular dependencies using dependency inversion in Python

Here's a simple diagram that illustrates the problem Description: there is a Pipeline entity that contains a Source entity, the Source is validated using the SourceValidator, and it in turn, in order to validate the source, must create a test…
3
votes
1 answer

A code-based registration vs a design time registration in the context of the IoC

What is the difference between the code-based registration and a design time registration in the context of the IoC? I met the concepts here while learning the IoC, DIP, DI and IoC containers.