I was going through the following link to understand what high-level and low-level modules mean in the context of Dependency Inversion Principle.
As per the explanation given there, is the following code snippet a good/appropriate example?
public class HighLevel
{
private IAbstraction _abstraction;
public HighLevel(IAbstraction abstraction)
{
_abstraction = abstraction;
}
public void Act()
{
_abstraction.DoSomething();
}
}
public interface IAbstraction
{
void DoSomething();
}
public class LowLevel: IAbstraction
{
public void DoSomething()
{
//Do something
}
}
High-level modules contain the important policy decisions and business models of an application – The identity of the application. Low-level modules contain detailed implementations of individual mechanisms needed to realize the policy.
In our example, CustomerBusinessLogic depends on the DataAccess class, so CustomerBusinessLogic is a high-level module and DataAccess is a low-level module. So, as per the first rule of DIP, CustomerBusinessLogic should not depend on the concrete DataAccess class, instead both classes should depend on abstraction.
Definition of the Dependency Inversion Principle The general idea of this principle is as simple as it is important: High-level modules, which provide complex logic, should be easily reusable and unaffected by changes in low-level modules, which provide utility features.
High level module is the interface / abstraction that will be consumed directly by the presentation layer. Low level on the other hand are bunch of small modules (subsystems) help the high level do their work. Example below is the high level module.
To make a long answer short: yes, this is an example of a Dependency Inversion Principle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With