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
3 answers

How should this class be modified in order to follow the DIP (Dependency Injection Principle)?

How I can modify this class to follow DIP (Dependency Inversion Principle) in order to remove the two ArrayList dependencies in the constructor? How should the interfaces be? One thing that confuses me is that the new references points to an…
0
votes
0 answers

how to create unit tests for a graphl resolver mocking the controller without using class

I'm trying to create an api using dependency inversion concepts with inversify. In case I have a graphl resolver that uses a controller that comes from the inversify container, but when writing tests with jest I can't mock this controller, can…
0
votes
2 answers

Dependency inversion and clean architecture with TypeScript

Consider the following trivial example in light of clean architecture, how can I improve on it? Specifically, I'm concerned with the app.ts file floating as it is. It acts as the composition root, where all dependencies are wired up but it just...…
0
votes
1 answer

How do I decouple lamp from button using the dependency inversion principle?

As per figure 5 in DIP I wrote the following code. How do I write it to conform to figure 6 in the same document? class Lamp: def __init__(self): self.state = "off" def turn_on(self): self.state = "on" def…
progmatico
  • 4,714
  • 1
  • 16
  • 27
0
votes
0 answers

Does Composition violates Dependency Inversion principle?

As for my understanding, Composition means, if a class A has an object of another class B in the way of "composition", A is responsible for maintaining life cycle of the object of B. Explaining further more: There are two classes named A and B. A…
0
votes
0 answers

dependency inversion implementation in javascript

the book "Clean Architecture" said: When using dynamically typed languages like Ruby and Python ... dependency inversion does not require either the declaration or the inheritance of interfaces. but I don't quite understand it, the book didn't…
Littlee
  • 3,791
  • 6
  • 29
  • 61
0
votes
1 answer

How is a method getting is called by an instance of its abstract class. I've attached the github issue

How is the method being called since the NumberTriviaRepository that is imported is just an abstract class whose implementation is in a completely different file as in the attached issue Please refer:…
0
votes
2 answers

Dependency Inversion Principle: is a low level module allowed to have a hidden reference to another low level module?

Consider the following code from Web Dev Simplified's video on the Dependency Inversion Principe: https://www.youtube.com/watch?v=9oHY5TllWaU class Store { constructor(paymentProcessor) { this.paymentProcessor = paymentProcessor; } …
0
votes
2 answers

Spring inject interface in controller request handler method when implementation annotated

I have a simple FooBarRepository interface. I have a single implementation called DefaultFooBarRespository, which looks like this: @Repository("fooBarRepository") public class DefaultFooBarRepository implements FooBarRepository { … } I know that…
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
0
votes
0 answers

Is OWIN a Dependency injection container or DI framework?

We have an ASP.NET 4.8 project and we thought to introduce Dependency injection principle. A colleague suggested to use OWIN as DI container. I tried to search for more information regarding ownin, but I don't seem to get the relation between owin…
0
votes
1 answer

If condition not working as expected in java class

So i was making a dependecy inversion principle example and i'm getting the wrong output from the class powerSwitch which is supposed to turn the lamp or the televesion (which extend the switchable abstract class) on if it's off and off it's…
0
votes
1 answer

Data Layer call and implementing DI

I'm learning how to implement DI in a 3 layers architecture, but I'm not quite sure if what I have implemented is actually a DI or some sort of mud abomination. The code works in the sense that obviously the DBWriter gets reached but I'm wondering…
0
votes
0 answers

How does interface change the flow of control (control flow), as in Dependency Inversion?

I am reading Clean Architecture by Robert Martin, chapter 5. It says (the quote is not 100% accurate, I changed it for brevity): In Figure above, module HL1 calls the F() function in module ML1 . The fact that it calls this function through an…
Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87
0
votes
0 answers

How can I remove my if else conditions from my code and apply dynamic behavior when I add new logic?

I have the following code class Store { constructor(paymentProcessor) { this.paymentProcessor = paymentProcessor; } purchaseBike(price, quantity) { if(this.paymentProcessor instanceof StripePaymentProcessor) { …
0
votes
1 answer

Dependency Inversion Principle

I'm trying to learn SOLID Principles and I am very confuse with this dependency Inversion principle. Can someone explain and see if my code below violates the mentioned principle? Sorry for the messy code. asking this through my phone. interface…
Ron8320
  • 1
  • 4