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
2
votes
2 answers

SOLID: Does DIP mean that composition/aggreation to an object is forbidden?

I am trying to understand and apply SOLID principles. Regarding the Dependency Inversion Principle, does it means that composition/aggregation to an object is forbidden? So that an interface must always be used to access another class method? I mean…
Plouff
  • 3,290
  • 2
  • 27
  • 45
2
votes
2 answers

Should concrete implementation provide any public API not present in the interface it implements?

"Code to interfaces" is considered good practice. Such code is easy to unit test and enables loose coupling. Users only know the interfaces and the onus of wiring concrete objects is upon the top-most level (this can be done in some init code or…
nits.kk
  • 5,204
  • 4
  • 33
  • 55
2
votes
1 answer

Implementing Dependency Inversion Principle using Maven and Spring

As per this Wikipedia article: Implementing Dependency Inversion Principle can be done in two ways: Having an abstraction of a low level component in a separate package upon which both high level and low level components depend. Having the…
2
votes
1 answer

How can objects communicate without violating the Dependency Inversion Principle?

I am building a Path Planner that will help people plan a path through an RPG console game. I want to create a table that shows each step through the stage. I have actually implemented a working version of this, however, it is seemingly awful OOP…
BBedit
  • 7,037
  • 7
  • 37
  • 50
2
votes
1 answer

Dependency Inversion Principle - Where should the interfaces go?

I've been scratching my head about this for a few months and I've still been able to satisfactorily convince myself that I have the right answer. We have a, very typical, situation where we have dependencies between multiple layers of our…
DoctorMick
  • 6,703
  • 28
  • 26
1
vote
2 answers

Dependency inversion in python - why is it used? Can't see the benefit

I've been trying to understand dependency inversion in python. I understand the theory that everybody quotes but I've not yet seen the code example with and without inversion that would clearly demonstrate the benefits. I've found only one highly…
1
vote
0 answers

Jetpack Compose and Dependency Inversion

In Jetpack Compose, most examples look something like this: ScreenLevelContainer { TopBar { ... } ContentA { ... } ContentB { ... } // etc. } Contents of any given composable may be comprised of other deeply nested composables. They talk…
1
vote
1 answer

How to delay the adoption of a particular dependency injection framework during the early development phase?

Robert C. Martin in his book "Clean Architecture: A Craftsman's Guide to Software Structure and Design" mentions that a good architecture allows to delay decisions about details. One of those he mentions is: It is not necessary to adopt a…
Juan
  • 2,024
  • 3
  • 17
  • 36
1
vote
1 answer

Dependency Inversion with assosiatedType in protocol

I am having a hard time trying to implement Dependency Inversion. Looking around, find a fantastic article Swift Type Erasure. I am not sure how to can I get advantage in my situation. Here is what I am trying to achieve. Protocol for…
1
vote
1 answer

How to properly add dependecy of entity class to other classes and reduce coupling

I was reading the dependency inversion principle. It states that the module should be dependent on abstraction. I understand most of the parts, and I can apply them. There is one place where there is confusion. Whenever we call a dependent class…
nak
  • 846
  • 2
  • 10
  • 26
1
vote
1 answer

May a Repository call an UseCase in Clean Architecture?

This is a very tricky question, because when we check the rules it's not explicit that a repository couldn't call an UseCase. However, it doesn't seem logical. Is there any definition/good practices and why it shouldn't do this? Thanks!
1
vote
1 answer

dependency inversion for data class(classes that define the structure of certain type)

So I have this data class: public class RecentProject { public string ProjectName { get; set; } public string ProjectPath { get; set; } public DateTime CreationDate { get; set; } public string OutputFolder { set; get; } =…
Ahmed Fawzy
  • 309
  • 2
  • 8
1
vote
1 answer

Is there a way of centralizing the access to a DataContext in C# using DI pattern?

this is what I got so far, but have to keep passing the DataClassesDataContext around. I wonder if there is a better more centralized way of using the DataClassesDataContext and fill the connectionstring each time the data context is used... Thanks…
1
vote
2 answers

Dependency inversion design choice question

I am currently reading an otherwise excellent tutorial regarding the Dependency Inversion Principle https://www.baeldung.com/java-dependency-inversion-principle and there is something I am not able to interpret despite considerable long time of…
1
vote
2 answers

Instantiate a class object in startup.cs in .Net Core

I am trying to pass an interface and settings into a class while creating an instance of that class in startup.cs in .Net Core Project. I am using the below code to do so. I have some code written in the constructor of Student class but while…