Dependency Injection and Dependency Inversion
Tags: webdev, programming, beginners, architecture
Published: 2023-06-24 20:38:27


## Dependency Injection and Dependency Inversion

Creating and maintaining robust and high-quality software are constant challenges for developers. As systems become more complex, issues such as lack of modularity and difficulty in conducting unit testing arise. To address these problems, design patterns and programming principles promoting the separation of concerns and code reuse have emerged. Two fundamental concepts in this regard are Dependency Injection and Dependency Inversion.

In this article, we will review the differences and the relations between Dependency Injection and Dependency inversion. Here I will try to help you understand each one because sometimes we could get confused using those terms.

Let us start with the definitions.

### Dependency Injection

![](https://cdn-images-1.medium.com/max/2000/0*2PPCDYW31Al-Ltzs.gif)

Dependency Injection is a design pattern that allows for the separation of an object’s dependencies from its implementation. Instead of the object creating or searching for its dependencies directly, they are supplied from the outside, providing greater flexibility and ease in conducting unit testing. In this approach, the dependent object is unaware of the details of how its dependencies are created or obtained; it only concerns itself with its usage.

In terms of Unit testing the dependency injection helps to make it easier. Because by injecting dependencies, alternative or mock implementations of the dependencies can be provided to independently isolate and test the dependent object. This allows for more efficient testing and reduces dependency on external environments or resources that are difficult to replicate in the test environment.

We can implement Dependency Injection in various ways, such as through a constructor, a configuration method, or by utilizing dependency injection containers. By employing this approach, code modularity is enhanced as dependencies can be easily changed or replaced without impacting the implementation of the dependent object. Furthermore, Dependency Injection facilitates the principle of “programming to an interface, not an implementation,” promoting code reuse and extensibility.

Let us see a pseudocode example:

    // Definition of a class that depends on an interface
    
    interface ILogger {
      method log(message: string)
    }
    
    class UserService {
      private logger: ILogger
    
      constructor(logger: ILogger) {
        this.logger = logger
      }
    
      method createUser(username: string) {
        // Logic for creating a user
    
        // Logging a log message using the ILogger dependency
        this.logger.log("A new user has been created: " + username)
      }
    }
    
    // Usage of the UserService class with an ILogger implementation
    
    class ConsoleLogger implements ILogger {
      method log(message: string) {
        // Logic for displaying the log message in the console
        console.log(message)
      }
    }
    
    // Creating an instance of UserService and supplying the ILogger implementation
    
    loggerInstance = new ConsoleLogger()
    userServiceInstance = new UserService(loggerInstance)
    
    // Using the UserService
    userServiceInstance.createUser("john_doe")

*In this example, the UserService class depends on an ILogger interface for logging messages. The concrete implementation ConsoleLogger implements the ILogger interface and provides the functionality of logging messages to the console.*

*In the constructor of UserService, an instance of ILogger is injected, allowing UserService to use any implementation of ILogger without being tightly coupled to a specific concrete implementation. When the createUser method is called, a message is logged using the supplied ILogger dependency.*

*By using dependency injection, the ILogger implementation can be easily changed to another one without modifying the logic of UserService. This provides flexibility and facilitates testing and maintaining the code.*

In short, dependency injection is a technique that promotes the separation of concerns, modularity, and flexibility by supplying the dependencies required by an object from the outside. It provides benefits such as code reuse and ease of unit testing, which contributes to more robust and maintainable software development.

### Dependency Inversion

![](https://cdn-images-1.medium.com/max/2000/0*eV6C7jUSWnOQ5J07.png)

Dependency inversion is a design principle in programming that states that high-level modules should not depend on low-level modules directly. Instead, both should depend on abstractions or interfaces.

This principle is applied when we are implementing a layer architecture, then we can say that the lower layers can not make reference to the upper layer. Translating to packages we cold say that inner packages can not make reference to outer packages.

Rather than a high-level module knowing and depending on the specific implementation details of low-level modules, abstractions or interfaces are introduced between them. This means that high-level modules depend on these abstractions rather than directly depending on low-level modules.

![Example](https://cdn-images-1.medium.com/max/2000/0*HMT2pYIkGOjsvO9Y.png)

This principle promotes modularity, loose coupling, and flexibility in software design. By using abstractions and interfaces, it becomes possible to change the implementations of low-level modules without affecting the high-level modules. This allows for easy swapping of different implementations without modifying the code of the high-level modules.

Furthermore, dependency inversion facilitates code reuse. By depending on abstractions instead of concrete implementations, different interchangeable implementations can be used in different contexts or scenarios.

### Conclusions

Dependency Inversion and Dependency Injection are closely related and are often used together to achieve a more flexible and modular software design. Dependency Injection allows you to implement dependency inversion by providing abstractions or interfaces through the injection of the corresponding dependencies through the use of interfaces or abstractions.

In other words, instead of a dependent object being directly attached to a concrete implementation, an implementation is supplied to it via injection using an interface, allowing the dependent object to communicate with its dependency through an abstraction or a interface, thus following the principle of dependency inversion.

In short, dependency injection is a technique used to implement dependency inversion. It allows the separation of concrete dependencies from dependent objects through the injection of abstractions or interfaces, which facilitates flexibility, decoupling, and code reuse.