Inversion of Control and Dependency Injection

In the previous tutorial, we looked at what Dependency Injection is. So, in this tutorial, we will go into Inversion of Control (IoC), and find out the relationship between these two concepts.

As I said, with Dependency Injection, an object does not depend on another object and is the same for other objects. When needed, the object will call the other object and vice versa. And I have asked you, where the objects will be created and located so that they can call each other. The answer is that we must have a container, and that container is part of the IoC concept.

IoC aims to provide a simple mechanism for containing dependent objects and managing those dependent objects through their life cycle. A dependent subject will require a certain number of dependent objects managed by the IoC. IoC will provide ways in which dependent subjects can access and interact with dependent objects.

IoC is divided into two different categories, namely:

  • Dependency Lookup
  • Dependency Injection

and they will have two different types of reality.

Dependency Lookup will look for dependencies in the IoC container, and then you can use code to populate the dependent object into a dependent subject. Meanwhile, Dependency Injection will inject dependent objects into the dependent subject directly.

I think you all can distinguish the difference between IoC and Dependency Injection, can’t you? Now we will learn more about the different types of IoC.

Dependency Lookup

Dependency Lookup is divided into two different types:

  • Dependency Pull
  • Contextualized Dependency Lookup (CDL)

With Dependency Pull, dependent objects will be retrieved from a place where the dependent objects are registered rather than taken directly from the container. If you have worked through EJB, you are familiar with Dependency Pull because to get dependencies in EJB application, you have to pass the JNDI API.

Inversion of Control and Dependency InjectionAs for Contextualized Dependency Lookup, getting a dependent object occurs directly with the containing container rather than through where the dependent object is registered.

Inversion of Control and Dependency Injection

Dependency Injection

Dependency Injection gives us two common ways to put objects that depend on dependent objects: Constructor Injection and Setter Injection.

With the Constructor Injection, putting a dependent object into the dependent subject will pass to the constructor of the dependent subject. The dependent object will be a parameter in those constructors.

For example:

With Setter Injection, placing the dependent object will pass to a setter method inside the dependent subject.

For example:

Add Comment