SOLID - Dependency Inversion Principle in C#

The “D” in SOLID is the Dependency Inversion Principle (DIP). This principle helps in reducing the coupling between high-level modules and low-level modules by introducing an abstraction layer. Let’s dive into the essence of the DIP with a flavorful twist: using food as our theme, we’ll explore how to apply this principle in C# programming.

The Essence of the Dependency Inversion Principle

The Dependency Inversion Principle consists of two key parts:

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Abstractions should not depend upon details. Details should depend upon abstractions.

In simpler terms, DIP suggests that our code should depend on interfaces or abstract classes instead of concrete classes. This approach allows us to change the behavior of our high-level modules without altering their code, just by substituting different implementations of the interfaces or abstract classes they depend on.

A Culinary Example: The Food Preparation Process

Imagine you’re designing a system to represent a kitchen where various foods can be prepared. In a non-DIP-compliant design, you might directly instantiate concrete classes within your high-level modules, which leads to a tightly coupled design.

Example Violation DIP

Let’s start by illustrating a violation of the Dependency Inversion Principle:

public class Kitchen
{
    private SandwichMaker _sandwichMaker = new SandwichMaker();

    public void PrepareLunch()
    {
        _sandwichMaker.MakeSandwich();
    }
}

public class SandwichMaker
{
    public void MakeSandwich()
    {
        // Logic to make a sandwich
        Console.WriteLine("Sandwich is ready!");
    }
}

In this example, the Kitchen class directly depends on the SandwichMaker class. This design is inflexible because if you want to prepare something else, like a salad, you’ll need to modify the Kitchen class by adding a new dependency.

Applying DIP: Introducing Abstractions

Now, let’s refactor the above example to adhere to the Dependency Inversion Principle:

public interface IFoodPreparer
{
    void PrepareFood();
}

public class Kitchen
{
    private IFoodPreparer _foodPreparer;

    public Kitchen(IFoodPreparer foodPreparer)
    {
        _foodPreparer = foodPreparer;
    }

    public void PrepareLunch()
    {
        _foodPreparer.PrepareFood();
    }
}

public class SandwichMaker : IFoodPreparer
{
    public void PrepareFood()
    {
        // Logic to make a sandwich
        Console.WriteLine("Sandwich is ready!");
    }
}

public class SaladMaker : IFoodPreparer
{
    public void PrepareFood()
    {
        // Logic to make a salad
        Console.WriteLine("Salad is ready!");
    }
}

In the refactored example, both the Kitchen class and the food preparers (SandwichMaker and SaladMaker) depend on the IFoodPreparer abstraction. This design adheres to DIP and offers several benefits:

Dependency Injection

To leverage the benefits of DIP fully, you’d typically use a technique called Dependency Injection (DI). DI allows you to pass the dependencies (in this case, the specific food preparer) into the Kitchen class at runtime, further enhancing flexibility and decoupling.

var kitchenWithSandwichMaker = new Kitchen(new SandwichMaker());
kitchenWithSandwichMaker.PrepareLunch();

var kitchenWithSaladMaker = new Kitchen(new SaladMaker());
kitchenWithSaladMaker.PrepareLunch();

Wrapping Up

The Dependency Inversion Principle is a powerful guideline for creating flexible and maintainable software. By depending on abstractions rather than concrete classes, we can build systems that are easier to extend and modify.

Recent Posts

SOLID - Dependency Inversion Principle (DIP) in C#
SOLID - Interface Segregation Principle (ISP) in C#
SOLID - Liskov Substitution Principle (LSP) in C#
SOLID - Open/Closed Principle (OCP) in C#
SOLID - Single Responsiblity Principle (SRP) in C#
How to Make a CLI Menu in C#