Dependency Inversioninversion well applied gives flexibility and stability at the level of the entire architecture of your application. It will allow your application evolvesto evolve more securesecurely and stable.
Traditional Layered Architecturelayered architecture
You have to understand layer, package, or library. Let's see how would the code would be.
Layered Architecturearchitecture with Dependency Inversiondependency inversion
The Dependency Inversiondependency inversion indicates the following:
What are the high-level modules and low level?
Thinking Thinking modules such as libraries or packages , high-level module would be those that traditionally have dependencies and low level on which they depend .
In
In other words, module high level would be where the action is invoked ,and low level where the action is performed.
A reasonable conclusion to draw from this principle is that there should be no dependence between concretions, but there must be a dependence on an abstraction. But according to the approach we take we can be misapplying investment depend dependency, but an abstraction .
We would have a library or package for the data access layer which define the abstraction.
To get dependency inversion, the persistence interface must be defined in the module or package where this high level logic or domain is and not in the low-level module .
First define what is the domain layer is and the abstraction of herits communication is defined persistence.
After the persistence layer depends on the domain , getting to invert now if a dependency is defined.
It is important to assimilate the concept well deepen, deepening the purpose and benefits. If we stay in mechanically and learn the typical case repository, we will not be able to identify where we can apply the principle of dependence.
But why do we invert a dependency? What is the main objective beyond specific examples?
Such commonly ** allows the most stable things are not dependent on less stable things will change more frequently . **allows the most stable things, that are not dependent on less stable things, to change more frequently.
It is easier for the persistence type isto be changed, either the database or technology to access the same database thatthan the domain logic or actions designed to communicate with persistence. Because of this, the dependence is reversed because as it is easier to change the persistence if this change occurs , in. In this way we will not have to change the domain . The domain layer is the most stable of all, which is why it should not depend on anything.
But there is not just this repository example , there. There are many scenarios where this principle applies and there are architectures based on this principle.
There are architectures where dependency inversióninversion is key to its definition, in. In all the domaindomains it is the most important and it is abstractions that will indicate the communication protocol between the domain and the rest of the packages or libraries are defined.
In Clean architecture the domain is located in the center and if you look in the direction of the arrows indicating dependency, it is clear what are the most important and stable layers , the. The outer layers are considered unstable tools so avoiding dependavoid depending on them.
It happens the same way Hexagonal Architecturewith the hexagonal architecture, where the domain is also located in the central part and ports are abstractions of communication from the domino outward. Here again it is evident that the domain is the most stable and traditional dependence is invertinverted.