Types of Dependency Injection


Tuesday, May 4th, 2010 - By Abhishek Rakshit
In continuation with my effort of trying to simplify Dependency Injection, I want to elaborate on the different types of injection. Dependency Injection is decoupling an application and service so that the application does not need to know anything about the service implementation. Dependency injection can be broadly classified in three categories: Constructor, Setter and Interface.

Object dependencies when passed as parameters to a constructor is termed as Constructor Injection.

public class Account {
  public User user;
  public Account (User user) {
   this.user = user;
  }
}

Pico Container is a framework which prefers constructor injection. Constructor injection ensures that the application object is created with all its service dependencies satisfied. The constructor arguments clearly delineates the dependencies and makes the code easy to understand. Using constructor injection also relieves the developer to explicitly check for the validity of the created object i.e the constructor enforces that all the dependencies for the required object have been injected already. Another advantage of constructor injection is that immutable properties can be hidden by not providing a setter. If setters are used to initialize these objects it can become a problem because not having a setter clearly signifies that the object is non modifiable. The code snippet shown above is a simple example of an Account class where the dependency of User class is injected through the constructor.

But, there are cases where Constructor injection can get messy, like having a class with a lot of dependencies. A constructor with many parameters can make the code hard to manage. Moreover, when dealing with multiple constructors and inheritance one has to initialize the super class correctly while adding your own parameters too. In such cases, using Setter Injection might be a better choice. In Setter Injection an explicit setter method is responsible for injecting the desired dependency as shown below.


public class Account {
  public User user;
  public setUser (User user) {
   this.user = user;
  }
}

The configurations to accomplish the injections can be set using XML or directly through code. One particular advantage of Setter injection is that it allows the creation of the resource as late as possible and only when it is needed. This is really useful in cases where the injected instance is expensive to create and is used rarely. The Spring framework prefers this type of injection although it does support constructor and interface injection.

The third category is of Interface Injection where an interface defines the injection method. The method providing the implementation has to implement the interface and as such the service is dependent on the interface and not on the implementation.  The example below shows the Account class which depends on the IUser interface and the AccountController class injects the required dependency in this case the object of the User class which implements the IUser interface.


public class Account {
  public IUser user;
  public setUser (IUser user) {
   this.user = user;
  }
}

public class AccountController {
  public void initDefaultAccount () {
   Account defaultUser = new Account;
   defaultUser.setUser(new User());
  }
}

public class User implements IUser {
  public String name;
  public Long id;
...
}

Summary

To sum up, Dependency Injection in itself a very good practice to keep your code understandable, efficient and robust. However, what type of injection to use depends more or less on your code. The constructor and setter injections are the more commonly used approaches and there is a lot of debate going on about which is better. Both of them have their strengths and weaknesses and a good rule of thumb which works for most cases is using constructor injection for required dependencies and setter injection for optional dependencies. More detailed information about Dependency Injection can be found here.
 

One Comment

  1. [...] Dependency Injection on a Project I have previously tried explaining Dependency Injection and how it can be beneficial to any project. This leads us to an [...]

Leave a Reply