Software Architecture — Inversion of Control Part 1

Aaron Janes
4 min readJan 29, 2023

--

Inversion of Control (IOC) is a design principle used in software architecture to invert control flow in a system. The IOC design pattern changes the paradigm of system control. Using the IOC design principle, the management of a system is inverted so that instead of the client code controlling a program’s flow, the framework or container contains the program’s flow. IOC allows for more flexibility and modularity in design and reduces coupling between components, creating greater flexibility, re-usability, and testability in software systems.

Inversion of Control (IoC) organizes the different parts of a software system so that they work flexibly and efficiently.

Imagine that you are building a big puzzle, and each piece is a different part of your software. With IOC, you are not the one putting all the pieces together; instead, you have a helper doing that for you. This helper is called a “container,” which could be a higher-level component or framework, and it’s responsible for connecting all the different pieces and ensuring they work as they should. Simply put, IOC works with the mandate of separating the “when” concerns from the “what” in your software.

There are two main types of IOC: Dependency Injection (DI) and Service Locator. When using “Dependency Injection” (DI) in software development, a “dependency” (a “what” concern) is something that a piece of the software needs to work on (a “when” situation).

The DI is a technique used in software development that separates these concerns of the program. It is a way to provide a component with its dependencies rather than having the component create them for itself. This approach can make a program more flexible, modular, and easier to test.

For example, a car needs gasoline to run, and gasoline is the dependency of the vehicle. In the same way, a piece of software might need access to a database or a specific library to work. With DI, instead of the part of software looking for and creating these dependencies, it is given as a “gift” or “injected” into it by the container. This way, the piece of software doesn’t need to worry about how to get these dependencies; it just uses them.

Consider the “Car” a class that needs to interact with an engine class. Instead of the Car class creating the engine object itself, the engine object is “injected” into the Car class by another component. This way, the car can use many different engines, and engines can be used by many other vehicles.

Dependency injection here allows for greater flexibility, as the Car class can work with any engine as long as the injector provides it in the proper format.

With this example, IOC makes the software more flexible because the different pieces can work together differently depending on the context.
It makes it easier to test because you can replace the dependencies with “fake” or “mocked” versions for testing purposes. For the scope of this paper, I will focus on describing dependency injection and its patterns, benefits and drawbacks.

In simple terms, dependency injection means that one component of your program (the dependent component) relies on another component (the injector) to provide the objects it needs to function. The dependent component does not need to know how these objects are instantiated, only that they are delivered.

There are several different ways to implement dependency injection, but the most popular method is using a dependency injection container (also called an injector). The dependency injection container creates and provides the dependent objects to the dependent component.
Some popular frameworks that use dependency injection include Spring for Java , Angular in JavaScript and consider React hooks as dependency injection.

Dependency injection in JavaScript is a design pattern that involves providing an external dependency to a module rather than having the module create or access the dependency on its own.
Here is an example of dependency injection in JavaScript:

In this example, the logger module depends on a logService to write log messages. Instead of creating or accessing the logService within the logger module, the logService is provided as an argument to the logger module when it’s imported into the app.js. Dependency Injection allows the app.js to control which logService the logger module uses.

One of the main benefits of dependency injection is that it can make a program more testable by injecting dependencies into a component, rather than having the component create them itself, it becomes easier to mock or substitute those dependencies for testing.

To highlight how dependency injection makes testing components easier, consider this example. Instead of a class creating its database connection, through dependency injection, it database connection in injected. Dependency injection allows for greater flexibility and easier testing, as the class can be given a mock database connection instead of a real one when running tests. Following the same example, it allows the software to be more flexible as now any database connection can be given as an upgrade or replacement. So in the future, it will allow your software to scale efficiently.

This decoupling of concerns is what makes Dependency Injection and IOC extremely powerful.

It’s worth noting that IOC and Dependency Injection can be a double-edged sword; if not used carefully and with a straightforward design in mind, it can lead to a big mess in your codebase and make it harder to understand and maintain.

In conclusion, IOC and dependency injection are powerful techniques that can make a program more flexible, modular, scalable and easier to test.

--

--

Aaron Janes
Aaron Janes

Written by Aaron Janes

Technical Team Lead and 4 x AWS Certified

No responses yet