Mastering Ninject for Dependency Injection
上QQ阅读APP看书,第一时间看更新

What is Dependency Injection?

Dependency Injection is one of the techniques in software engineering which improves the maintainability of a software application by managing the dependent components. In order to have a better understanding of this pattern, let's start this section with an example to clarify what is meant by a dependency, and what other elements are involved in this process.

Cameron is a skilled carpenter who spends most of his time creating wooden stuffs. Today, he is going to make a chair. He needs a saw, a hammer, and other tools. During the process of creating the chair, he needs to figure out what tool he needs and find it in his toolbox. Although what he needs to focus on is how to make a chair, without thinking of what tools he needs and how to find them, it is not possible to finish the construction of the chair.

The following code is the C# representation of Cameron, as a carpenter:

class Carpenter
{
  Saw saw = new Saw();
  void MakeChair()
  {
    saw.Cut();
    // ...
  }
}

Sarah is a heart surgeon. She works for a hospital and spends her days in the operation room, and today she is going to perform an open-heart surgery. It is a sophisticated procedure, and she needs to focus on the operation itself, rather than finding the tools during the operation. That is why she has an assistant to provide her with the tools she requires. This way, she ensures that the exact tool that she needs will be in her hand by her assistant. She doesn't need to know where the tool is and how to find it. These are her assistant's responsibilities.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

This is the C# implementation of Sarah, the surgeon:

class Surgeon
{
  private Forceps forceps;

  // The forceps object will be injected into the constructor 
  // method by a third party while the class is being created.
  public Surgeon(Forceps forceps)
  {
    this.forceps = forceps;
  }

  public void Operate()
  {
    forceps.Grab();
    //...
  }
} 

As we can see, she doesn't need to worry about how to get the forceps; they are provided to her by someone else.

In the previous examples, Cameron and Sarah are samples of dependent components that have a responsibility, and tools that they need are their dependencies. Dependency Injection is all about how they get to the tools they need. In the first example, the dependent component (Cameron) itself had to locate the dependency, while in the second one, a third party (the assistant) locates and provides it. This third party is called an Injector, which injects the dependencies.

DI or Inversion of Control (IoC)

Martin Fowler defines Inversion of Control (IoC) as a style of programming in which the framework takes the control of the flow instead of your code. Comparing handling an event to calling a function is a good example to understand IoC. When you call the functions of a framework, you are controlling the flow, because you decide in what sequence to call the functions. But in case of handling events, you are defining the functions and the framework is calling them, so the control is inverted to the framework instead of you. This example showed you how control can be inverted. DI is a specific type of IoC, because instead of your components concern about their dependencies, they are provided with the dependencies by the framework. Indeed, as Mark Seemann states in his book, Dependency Injection in .NET, IoC is a broader term which includes, but is not limited to, DI, even though they are often being used interchangeably. IoC is also known as the Hollywood Principle: "Don't call us, we'll call you".