Questions tagged [dependency-inversion]

Dependency inversion refers to a specific form of decoupling software modules. The principle is that high level modules and low level modules should depend on abstractions, and abstractions shall not depend on implementation details. Not to be confused with dependency injection which is a special form of dependency inversion.

What is it ?

Dependency inversion refers to a specific form of decoupling software modules. The principle is that high level modules and low level modules should depend on abstractions, and abstractions shall not depend on implementation details.

It reverses the more conventional and intuitive approach in object oriented programming of having high level modules are build on top of low level modules which they then depend on.

How is it implemented ?

Dependency inversion can be implemented in several ways, the most common ones being:

  • : the interface of an object refers to an abstraction, and the concrete implementation of that abstraction is provided at run time.
  • : an object depends on an abstraction, and the concrete implementation of that abstraction is obtained via an intermediary

More infos:

Disambiguation:

  • avoid the confusion with dependency injection, which is a special form of dependency inversion and has the same DI abreviation.
  • avoid the confusion with inversion of control, which may use dependency inversion, but is about a different concern
64 questions
18
votes
5 answers

What is the meaning of "inversion" in Dependency Inversion design principle?

I'm reading about design patterns. I know what this principle does. High-level and low-level classes depend on abstractions. But why we say this is inversion?
17
votes
1 answer

How is Inversion of Control related to Dependency Inversion

In many articles all over the web the terms Inversion of Control and Dependency Inversion Principle seem to be mixed up and used as synonyms (further confusion is enforced by the tools that are called "DI-Containers" and "IoC-Containers"). A…
14
votes
5 answers

How to avoid bidirectional class and module dependencies

To give some context, I'm using python. We have the following package structure: /package_name /request.py # defines class Request /response.py # defines class Response Let also assume that we have a bidirectional dependency at class and…
MLguy
  • 301
  • 1
  • 3
  • 7
9
votes
4 answers

Is dependency inversion principle necessary?

I've read a lot about dependency inversion principle but still, I can't apply it to my case. I just don't know when I should apply it and when not. I write a simple application in Java to generate invoices. For now, I have basic classes: Client,…
Awerde
  • 327
  • 2
  • 5
9
votes
3 answers

Clean Architecture Gateway layer depends on outer layer

Looking at the clean architecture layers and flow diagrams, and implemented it my self in my applications, I've always wondered which layer is supposed to contain the DB, or any 3rd Party service or SDK. Looking at both of these images raises the…
oren
  • 257
  • 2
  • 6
9
votes
2 answers

I'm struggling to see how dependency inversion doesn't lead to tighter coupling in lower level modules and less reuseability

I'm refactoring a project I did for my work and I'm trying to apply the SOLID principles to make the architecture cleaner. I've run into an issue with the Dependency Inversion Principle that I can't seem to solve. The Dependency Inversion Principle…
6
votes
2 answers

Is it good design to have one constructor that supplies a "default" concrete class to another that takes an abstraction?

I like to invert dependencies whenever possible by depending mostly on abstraction and allowing the concrete implementations to be passed into the object by clients, or a factory. I've found this to be pretty conducive to testability and…
6
votes
1 answer

Is there really such a thing as the onion architecture?

Note: BLL = Business Logic Layer (can also mean your domain) I'm trying to understand the onion architecture. It seems to me that it's actually the same thing as the layered architecture, only with the dependency inversion principle (DIP) applied.…
Bob Horn
  • 2,327
  • 3
  • 20
  • 26
6
votes
6 answers

Coding to Interfaces vs Abstract Inheritance

Inheritance vs coding to an interface is something I have wondered with respect to proper architecture design but actually have not run into an problems when using abstract inheritance over coding to an interface in my own projects. According to…
6
votes
2 answers

Why do I need dependency injection and the depencency inversion principal in my case?

I'm a student of best practices, architectural patterns, and design principals. I have been studying dependency injection and inversion of control a lot lately, and have been "drinking the koolade" pretty extensively with a lot of my projects over…
6
votes
1 answer

When should interface be owned by client?

In Agile Software Development: Principles, Patterns, and Practices, Uncle Bob talks about client owning the service interface. My questions are : Should client always own the interface or only when the client changes less often i.e. at higher level…
q126y
  • 1,713
  • 3
  • 14
  • 24
5
votes
2 answers

How to maintain Dependency Inversion Principle with enums & custom types?

From what I understand two components A and B should only communicate with one another via an interface. Ideally this interface should be in its own separate assembly so that the client need not be loaded with the dependencies of any particular…
user4779
  • 929
  • 1
  • 6
  • 13
5
votes
2 answers

Help with dependency inversion

I'm learning about Binary Trees in school and I'm trying to apply the SOLID principles to it. My problem is in my BinaryTree::insert() method I'm creating a new BinaryNode, and from what I've understood that's bad dependency direction because higher…
user644361
  • 161
  • 4
5
votes
1 answer

Why support cyclic dependencies at all? Are there valid use cases?

Background Time ago I learnt something about package design, in particular about loose coupling: The Acyclic Dependencies Principle The dependency structure between packages must be a Directed Acyclic Graph (DAG). That is, there must be no cycles…
laconbass
  • 161
  • 7
5
votes
4 answers

Dependency Inversion Principle and Hollywood analogy

Often DIP is known as the Hollywood principle: "Don't call us, we'll call you.". But doesn't always higher level module calls lower level module, whether there is dependency inversion or not? Then why is DIP called the Hollywood principle? Am I…
q126y
  • 1,713
  • 3
  • 14
  • 24
1
2 3 4 5