DEV Community

Roshan
Roshan

Posted on

Dependency Injection Demystified: What, Why, and How

Introduction

Managing dependencies in software applications is like organizing the wiring in a complex machine. Without a structured approach, it quickly becomes unmanageable. Dependency Injection (DI) is the methodology that transforms this chaos into clean, modular, and testable code.

In this article, we’ll demystify Dependency Injection by exploring:

  • What Dependency Injection is.
  • Why it’s crucial for modern software development.
  • How you can implement it effectively in your projects.

If you’re looking for a deeper dive into DI patterns, anti-patterns, and best practices, check out my book:

👉 Mastering Dependency Injection in .NET 8: Advanced Concepts and Patterns


What Is Dependency Injection?

Dependency Injection is a technique that achieves Inversion of Control (IoC) by delegating the responsibility of dependency creation to an external source. Instead of a class creating its own dependencies, they are "injected" by a container or framework.

Key Principles of DI

  1. Dependency Inversion Principle (DIP):
    • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  2. Inversion of Control (IoC):
    • The control of object creation is transferred from the class to an external source.
  3. Separation of Concerns (SoC):
    • Classes focus solely on their primary responsibilities, leaving dependency management to a container or framework.

Simple Example Without DI

public class OrderService
{
    private readonly PaymentProcessor _paymentProcessor = new PaymentProcessor();

    public void PlaceOrder(Order order)
    {
        _paymentProcessor.Process(order);
    }
}
Enter fullscreen mode Exit fullscreen mode

Best Practices for Dependency Injection

To make the most of Dependency Injection, follow these best practices:

  1. Keep Dependencies Explicit

    • Always make dependencies visible, either through constructor injection or property injection. Avoid hidden dependencies, which make code harder to understand and maintain.
  2. Avoid Over-Injection

    • If a class requires too many dependencies, it might have too many responsibilities. Split it into smaller, more focused classes.

Example of Over-Injection (Code Smell):

   public class OrderService
   {
       public OrderService(IPaymentProcessor paymentProcessor, INotificationService notificationService,
                           ILogger logger, IDatabaseConnection dbConnection)
       {
           // Too many dependencies
       }
   }
Enter fullscreen mode Exit fullscreen mode

Why It’s Bad:

  • Hidden Dependencies: The OrderService appears to have no dependencies, but it actually depends on IPaymentProcessor through the Service Locator.
  • Harder to Test: Dependencies are resolved at runtime, making unit tests difficult to set up.
  • Violates Explicit Dependency Principle: Dependencies should be visible in the class’s constructor, method, or property.

Use Constructor Injection to make dependencies explicit:


public class OrderService
{
    private readonly IPaymentProcessor _paymentProcessor;

    public OrderService(IPaymentProcessor paymentProcessor)
    {
        _paymentProcessor = paymentProcessor;
    }

    public void ProcessOrder(Order order)
    {
        _paymentProcessor.Process(order);
    }
}

Enter fullscreen mode Exit fullscreen mode

2. Over-Injection (Dependency Salad)

What It Is:

Over-injection occurs when a class has too many injected dependencies. This often indicates that the class has too many responsibilities, violating the Single Responsibility Principle (SRP).

Example of Over-Injection:


`public class OrderService`

public class OrderService
{
    public OrderService(IPaymentProcessor paymentProcessor, INotificationService notificationService,
                        ILogger logger, IInventoryManager inventoryManager,
                        IShippingCalculator shippingCalculator)
    {
        // Too many dependencies
    }
}

Enter fullscreen mode Exit fullscreen mode

Why It’s Bad:

  • Difficult to Understand: A constructor with many parameters becomes hard to read and maintain.
  • Hard to Test: Setting up unit tests requires mocking multiple dependencies.
  • Violation of SRP: The class likely has too many responsibilities.

Solution:

  • Refactor Responsibilities: Break the class into smaller, more focused services.
  • Use Facade or Aggregator Patterns: Combine related dependencies into a single object to reduce the number of injected services.

Refactored Example:




public class OrderService
{
    private readonly IOrderProcessor _orderProcessor;

    public OrderService(IOrderProcessor orderProcessor)
    {
        _orderProcessor = orderProcessor;
    }

    public void PlaceOrder(Order order)
    {
        _orderProcessor.Process(order);
    }
}

Enter fullscreen mode Exit fullscreen mode

3. Circular Dependencies

What It Is:

Circular dependencies occur when two or more classes depend on each other, either directly or indirectly. This creates a dependency loop that can break the DI container or lead to runtime errors.

Example of Circular Dependencies:

public class ClassA
{
    public ClassA(ClassB b) { }
}

public class ClassB
{
    public ClassB(ClassA a) { }
}

Enter fullscreen mode Exit fullscreen mode

Why It’s Bad:

  • Breaks the DI Container: Many DI containers cannot resolve circular dependencies.
  • Difficult to Debug: It’s hard to trace the dependency loop in large systems.
  • Indicates Poor Design: Circular dependencies often point to a lack of separation of concerns.

Solution:

  • Introduce an Intermediary Service: Refactor the design to remove the circular reference by introducing a new service that handles shared responsibilities.
  • Reevaluate Class Responsibilities: Ensure each class has a clear and focused purpose.

Refactored Example:


public class ClassA
{
    private readonly ISharedService _sharedService;

    public ClassA(ISharedService sharedService)
    {
        _sharedService = sharedService;
    }
}

public class ClassB
{
    private readonly ISharedService _sharedService;

    public ClassB(ISharedService sharedService)
    {
        _sharedService = sharedService;
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Injecting Wrong Service Lifetimes

What It Is:

Using incorrect lifetimes for services can lead to performance issues, unintended behavior, or even application crashes.

Examples of Wrong Lifetimes:

  1. Injecting a Scoped service into a Singleton service:
    • Why It’s Bad: The Scoped service is tied to a specific request, but the Singleton service persists for the application’s lifetime, causing conflicts.
  2. Overusing Transient services in performance-critical paths:
    • Why It’s Bad: Creates unnecessary object churn, leading to garbage collection overhead.

Solution:

  • Understand Lifetimes: Choose Singleton, Scoped, or Transient based on the use case.
  • Use Factories for Scoped/Transient Services: When a Singleton needs a Scoped or Transient dependency, inject a factory instead.

Using a Factory for Scoped Service:


public class SingletonService
{
    private readonly Func<IScopedService> _scopedServiceFactory;

    public SingletonService(Func<IScopedService> scopedServiceFactory)
    {
        _scopedServiceFactory = scopedServiceFactory;
    }

    public void Execute()
    {
        var scopedService = _scopedServiceFactory();
        scopedService.DoWork();
    }
}
Enter fullscreen mode Exit fullscreen mode

5. Hidden Dependencies

What It Is:

Hidden dependencies occur when a class uses dependencies that are not explicitly injected or declared, making the code harder to understand and maintain.

Example of Hidden Dependencies:


public class OrderService
{
    private readonly ILogger _logger = new Logger(); // Hidden dependency

    public void PlaceOrder(Order order)
    {
        _logger.Log("Placing order...");
        // Order logic
    }
}

Enter fullscreen mode Exit fullscreen mode

Why It’s Bad:

  • Violates Explicit Dependency Principle: Dependencies should be clearly declared and managed.
  • Harder to Test: Hidden dependencies cannot be easily mocked or replaced in unit tests.

Solution:

  • Always inject dependencies explicitly through constructors, properties, or methods.

Refactored Example:

public class OrderService
{
    private readonly ILogger _logger;

    public OrderService(ILogger logger)
    {
        _logger = logger;
    }

    public void PlaceOrder(Order order)
    {
        _logger.Log("Placing order...");
        // Order logic
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Dependency Injection is more than just a design pattern—it’s a mindset for building modular, scalable, and maintainable software systems. By understanding its principles and embracing best practices, you can create code that is easier to test, extend, and adapt to change.

In this article, we explored:

  • What Dependency Injection is: A way to achieve Inversion of Control by delegating dependency creation to external sources.
  • Why it matters: Promotes loose coupling, enhances testability, and supports scalability.
  • How to implement it: Through Constructor, Method, and Property Injection, alongside leveraging .NET’s built-in DI container.

Your Next Steps

Mastering Dependency Injection takes time, but the rewards are well worth it. If you’re ready to take your understanding to the next level, dive deeper into advanced patterns, and explore real-world examples, I invite you to check out my book:

👉 Mastering Dependency Injection in .NET 8: Advanced Concepts and Patterns

This book is your complete guide to:

  • Core principles and practical applications.
  • Advanced patterns like Scoped Factories, Decorators, and more.
  • Optimizing performance and designing testable systems.

Whether you’re new to DI or a seasoned developer, this book will help you unlock the full potential of Dependency Injection in your projects.


Let’s embrace DI as the foundation of modern software development and build systems that are future-ready, maintainable, and scalable.

Happy coding! 🎉

Top comments (0)