Inversion of Control (IoC)

  • Post published:May 23, 2020

Inversion of Control (IoC) is a programming principle where the unconcerned logic in a class is delegated to some other class. In other words, IoC is all about inverting the flow of unconcerned logic (additional responsibilities) from one class to another class.

IoC helps to design loosely coupled classes and cleaner code, thereby making the code base of a program more reusable, extensible, testable, simplistic and maintainable. IoC is most commonly used in the context of object-oriented programming.

A good class is that class which takes care of its own logic (single responsibility) rather than overloading it with unconcerned logic.

Problem

Software programs contain logic, and logic have flow. Consider the below code base.

In the above program, the execution starts from the Main() method. The Main() method controls the flow of the program by choosing a database type, instantiating the Hero class and finally creating a hero.

When lines 5 and 6 in the Main() method are executed, the flow of logic for the Hero class is as follows:

theCodeReaper
  • first the Hero constructor is executed
  • the Hero class then decides which database context to instantiate based on the DatabaseType
  • the Create method of the Hero is invoked
  • finally, the Add method of the database instance is invoked.

From the above Hero code base and logic flow, it can be clearly seen that the Hero class is tightly coupled to the different types of databases and has an additional responsibility of been the decision maker to decide which database to use.

The decision making of which database to use (unconcerned logic) should not be the responsibility of the Hero class and should be inverted to some other class.

Solution

The Hero class should only focus on creating a hero and not deciding which database to use. As stated earlier, the execution of the program starts from the Main() method, and the Main() method controls the flow of logic, thus, the decision making can be inverted to the Main() method (external class to the Hero class).

Consider the below possible solution for the flow of logic.

theCodeReaper

In the above diagram, the Main() method, or invoker, decides which database to use. It is an external class to the Hero class.

The control has now been inverted to an external dependency.

The flow of the Hero class is now effected by an external dependency. The Hero class does not care on what type of database is used. The Hero class can now focus on just creating a hero.

The below code base presents the solution.

The program is now more decoupled since changes to the type of database used does not impact the Hero class, and changes to the Hero class does not impact the choice of database. The code is more maintainable.

Summary

Inversion of Control (IoC) is a powerful principle to use to create more decoupled programs with a cleaner code base. IoC further increases re-usability, extensibility, test-ability, simplicity and maintainability.

IoC can be implemented in a number of ways, as can be seen in the below diagram.

theCodeReaper

The solution to the problem in this article used dependency injection with constructor injection.

Further information on IoC can be found at the following link: