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

Nest.js Dependency Inversion function not found

I followed the controller-service-repository architecture and I want to use dependency inversion on StoneRepository. Having the code from bellow I get: [Nest] 22656 - 03/21/2022, 5:01:44 PM ERROR [ExceptionsHandler] this.stoneRepository.getStones…
0
votes
1 answer

SOLID Principles, Can JDBC interface be said as an example of Dependency Inversion Principle?

Of the five SOLID Principles, Dependency Inversion is one of them and the last one. Can I quote JDBC interface as an example of Dependency Inversion Principle?
joven
  • 371
  • 1
  • 6
  • 17
0
votes
0 answers

IoC vs DIP differences

Is it possible to have Inversion of Control without the Dependency Inversion Principle? Some sources insist they're the same, others don't. Would like to get a conversation going around the differences.
Nick B
  • 657
  • 1
  • 13
  • 33
0
votes
2 answers

DLL design and the Dependency Inversion Principle

I have a somewhat basic design question that I have not been able to find a good answer to (here, on other forums nor the books I've consulted) I'm creating a dll and is wondering what the best way to expose its content would be. I'm aiming for a…
0
votes
1 answer

Using factory for everything?

My software shall control a device or multiple of them. The program flow is like that it will search for devices on all available interfaces and instantiate an object of the type IDevice for each device found. To state it here, all these objects…
0
votes
1 answer

Explanation of phrase 'should depend on abstractions' in Dependency Inversion (DI)

I have never quite understood the second part of the phrase... 'High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces)' If I changed... class Upper { foo = () => { console.log('bar') …
Exitos
  • 29,230
  • 38
  • 123
  • 178
0
votes
2 answers

Do you inject dependencies in the constructor even if the class does not own these dependencies, but uses them in methods?

If a School class relies on an EmailSender class you might have to inject this EmailSender through it's constructor with a generic interface like IMessagable or IMessageSender or even IEmailSender so that it can be swapped out for something else in…
0
votes
0 answers

I am learning on how to implement Dependency Injection [DI] in C#. Is this the valid DI program as an example?

1. Main Method class Program { static void Main(string[] args) { ILogger _log = new Logger(); IDataAccess _da = new DataAccess(); BusinessLogic bs = new BusinessLogic(_log,_da); …
0
votes
0 answers

StackOverFlow in the implementation of the Dependency Inversion Principle for cross-refenced classes

The Situation: I have three classes: Society, Father and Son. The Society has a list of Fathers. Each Father has one son. It is necessary in this application that each Father knows who his Son is and vice-versa each Son knows who his Father is. I…
0
votes
1 answer

Dependency inversion issue in php. (Repository Pattern)

I am implementing a repository pattern in Laravel, and it seems to be very tedious. For example, let's say I have products then I have to create a ProductRepository interface then a ProductRepository class that implements that interface, now I have…
0
votes
1 answer

Is the Open-Closed SOLID principle the same as Coding to an Interface?

Does open-for-extension and closed-for-modification mean code-to-an-interface? If I code to an interface so that future concrete implementations can be introduced by implementing the interface, and we create new classes without touching existing…
0
votes
1 answer

Trying to understand Figure 14.11 of the book "Clean Architecture"

This is from chapter 14 of the book "Clean Architecture" It's trying to break the direct dependency of Stable-->Flexible by introducing UServer. So that we can have Stable-->UServer<--Flexible where nobody would depend directly on the concrete…
Cui Pengfei 崔鹏飞
  • 8,017
  • 6
  • 46
  • 87
0
votes
2 answers

Deriving from a concrete class?

In book 'Head First Design Patterns', one of the way mentioned to not violate 'Dependency Inversion' principle is as: No class derive from a concrete class. Is it possible to follow this rule thoroughly? In many commonly used frameworks and…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
0
votes
1 answer

crash while try store value using dependency inversion

I want to implemented dependency inversion In app delegate in my app as my rootController is my UITabBarController but when I want to try it there is an error Fatal error: Unexpectedly found nil while unwrapping optional value This is my code in…
0
votes
1 answer

Dependency Inversion in swift

Hi I have a question for developer in here, I was reading an iOS programming book from big nerd ranch. I was interested how to structure and technique to create an app. and I was trying to implement it, and it was a dependency inversion and the code…
ferryawijayanto
  • 569
  • 4
  • 18