Practical Differences Between Dependency Injection Styles

dependency-injectiondesign-patternsinversion-of-control

I'm new to dependency injection and I have a few questions about which style I should use in my applications. I have just read Inversion of Control
Containers and the Dependency Injection pattern
by Martin Fowler, but I
can't get the practical difference between constructor, setter and interface
injection.

It seems to me that the reasons for using one over the other are just a
matter of code cleaning and/or clarity. What is the difference? Are there any strong advantages or disadvantages to using one over the other, or is it just what I've stated before?

In my opinion, constructor injection is the most intuitive of all, as well as interface injection is the least. In the other hand, setter injection is a middle term, but, are you supposed to be able to change the instance of the dependency object you initially injected? Does this style of injection guarantees that the object which needs the dependency will always have it injected? I believe not, but please correct me if I'm wrong.

Best Answer

Constructor Injection has the advantage that it makes the dependency explicit and forces the client to provide an instance. It can also guarantee that the client cannot change the instance later. One (possible) downside is that you have to add a parameter to your constructor.

Setter Injection has the advantage that it doesn't require adding a parameter to the constructor. It also doesn't require the client to set the instance. This is useful for optional dependencies. This may also be useful if you want the class to create, for example, a real data repository by default, and then in a test you can use the setter to replace it with a testing instance.

Interface Injection, as far as I can tell, is not much different than setter injection. In both cases you are (optionally) setting a dependency that can be changed later.

Ultimately it is a matter of preference and whether or not a dependency is required. Personally, I use constructor injection almost exclusively. I like that it makes the dependencies of a class explicit by forcing the client to provide an instance in the constructor. I also like that the client cannot change the instance after the fact.

Often times, my only reason for passing in two separate implementations is for testing. In production, I may pass in a DataRepository, but in testing, I would pass in a FakeDataRepository. In this case I'll usually provide two constructors: one with no parameters, and another that accepts a IDataRepository. Then, in the constructor with no parameters, I will chain a call to the second constructor and pass in a new DataRepository().

Here's an example in C#:


public class Foo
{
  private readonly IDataRepository dataRepository;

  public Foo() : this(new DataRepository())
  {
  }

  public Foo(IDataRespository dataRepository)
  {
    this.dataRepository = dataRepository;
  }
}

This is known as Poor Man's Dependency Injection. I like it because in production client code, I don't need to repeat myself by having several repeated statements that look like

var foo = new Foo(new DataRepository());
However, I can still pass in an alternate implementation for testing. I realize that with Poor Man's DI I'm hardcoding my dependency, but that's acceptable for me since I mostly use DI for testing.