Home Programming Mastering Ninject for Dependency Injection

Mastering Ninject for Dependency Injection

By Daniel Baharestani
books-svg-icon Book
Subscription FREE
eBook + Subscription €11.99
eBook €21.99
Print + eBook €26.99
READ FOR FREE Free Trial for 7 days. €11.99 p/m after trial. Cancel Anytime! BUY NOW BUY NOW BUY NOW
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
READ FOR FREE Free Trial for 7 days. €11.99 p/m after trial. Cancel Anytime! BUY NOW BUY NOW BUY NOW
Subscription FREE
eBook + Subscription €11.99
eBook €21.99
Print + eBook €26.99
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
About this book
Dependency injection is an approach to creating loosely coupled applications. Maintainability, testability, and extensibility are just a few advantages of loose coupling. Ninject is a software library which automates almost everything that we need in order to implement a dependency injection pattern. Mastering Ninject for Dependency Injection will teach you everything you need to know in order to implement dependency injection using Ninject in a real-life project. Not only does it teach you about Ninject core framework features that are essential for implementing dependency injection, but it also explores the power of Ninject's most useful extensions and demonstrates how to apply them. Mastering Ninject for Dependency Injection starts by introducing you to dependency injection and what it's meant for with the help of sufficient examples. Eventually, you'll learn how to integrate Ninject into your practical project and how to use its basic features. Also, you will go through scenarios wherein advanced features of Ninject, such as Multi-binding, Contextual binding, providers, factories and so on, come into play. As you progress, Mastering Ninject for Dependency Injection will show you how to create a multilayer application that demonstrates the use of Ninject on different application types such as MVC, WPF, WCF, and so on. Finally, you will learn the benefits of using the powerful extensions of Ninject.
Publication date:
September 2013
Publisher
Packt
Pages
142
ISBN
9781782166207

 

Chapter 1. Understanding Dependency Injection

"It's more about a way of thinking and designing code than it is about tools and techniques"

– Mark Seemann

This chapter introduces the Dependency Injection (DI) concepts and describes the advantages of using this pattern. We will also go through a simple example and implement the principles and patterns related to the DI technique to it. After understanding what a DI container is, we will discuss why Ninject is a suitable one.

By the end of this chapter, the reader is expected to have a good understanding of DI and how Ninject can help them as a DI container.

The topics covered in this chapter are:

  • What is Dependency Injection?

  • How can DI help?

  • My first DI application

  • DI Containers

  • Why use Ninject?

 

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".

 

How can DI help?


Every software application is inevitable of change. As your code grows and new requirements arrive, the importance of maintaining your codes becomes more tangible, and it is not possible for a software application to go on if it is not maintainable. One of the design principles that lead to producing a maintainable code is known as Separation of Concerns (SoC). The SoC is a broad concept and is not limited to software design; but in the case of composing software components, we can think of SoC as implementing distinct classes, each of which deals with a single responsibility. In the first example, finding a tool is a different concern from doing the operation itself and separating these two concerns is one of the prerequisites for creating a maintainable code.

Separation of concerns, however, doesn't lead to a maintainable code if the sections that deal with concerns are tightly coupled to each other.

Although there are different types of forceps that Sarah may need during the operation, she doesn't need to mention the exact type of forceps which she requires. She just states that she needs forceps, and it is on her assistant to determine which forceps satisfies her need the best. If the exact type that Sarah needs is temporarily not available, the assistant has the freedom to provide her with another suitable type. If the hospital has bought a new type of forceps that the assistant thinks is more suitable, he or she can easily switch to the new one because he or she knows that Sarah doesn't care about the type of forceps as long as it is suitable. In other words, Sarah is not tightly coupled to a specific type of forceps.

The key principle leading to loose coupling is the following, from the Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software):

"Program to an "interface", not an "implementation"."

When we address our dependencies as abstract elements (an interface or abstract class), rather than concrete classes, we will be able to easily replace the concrete classes without affecting the consumer component:

class Surgeon
{
  private IForceps forceps;

  public Surgeon(IForceps forceps)
  {
    this.forceps = forceps;
  }

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

The Surgeon class is addressing the interface IForceps and does not care about the exact type of the object injected into its constructer. The C# compiler ensures that the argument passed to the forceps parameter always implements the IForceps interface and therefore, existence of the Grab() method is guaranteed. The following code shows how an instance of Surgeon can be created providing with a suitable forceps:

var forceps = assistant.Get<IForceps>();
var surgeon = new Surgeon (forceps);

Because the Surgeon class is programmed to the IForceps interface rather than a certain type of forceps implementation, we can freely instantiate it with any type of forceps that the assistant object decides to provide.

As the previous example shows, loose coupling (surgeon is not dependent on a certain type of forceps) is a result of programming to interface (surgeon depends on IForceps) and separation of concerns, (choosing forceps is the assistant's concern, while the surgeon has other concerns) which increases the code maintainability.

Now that we know loose coupling increases the flexibility and gives freedom of replacing the dependencies easily; let's see what else we get out of this freedom other than maintainability. One of the advantages of being able to replace the concrete classes is testability. As long as the components are loosely coupled to their dependencies, we can replace the actual dependencies with Test Doubles such as mock objects. Test Doubles are simplified version of the real objects that look and behave like them and facilitate testing. The following example shows how to unit test the Surgeon class using a mock forceps as a Test Double:

[Test]
public void CallingOperateCallsGrabOnForceps()
{
  var forcepsMock = new Mock<IForceps>();

  var surgeon = new Surgeon(forcepsMock.Object);
  surgeon.Operate();

  forcepsMock.Verify(f => f.Grab());
}

In this unit test, an instance of the Surgeon class is being created as a System Under Test (SUT), and the mock object is injected into its constructor. After calling the Operate method on the surgeon object, we ask our mock framework to verify whether the Grab operation is called on the mock forceps object as expected.

Maintainability and testability are two advantages of loose coupling, which is in turn a product of Dependency Injection. On the other hand, the way an Injector creates the instances of concrete types, can introduce the third benefit of DI, which is the late binding. An Injector is given a type and is expected to return an object instance of that type. It often uses reflection in order to activate objects. So, the decision of which type to activate can be delayed to the runtime. Late binding gives us the flexibility of replacing the dependencies without recompiling the application. Another benefit of DI is extensibility. Because classes depend on abstractions, we can easily extend their functionality by substituting the concrete dependencies.

       
About the Author
  • Daniel Baharestani

    Daniel Baharestani is an experienced IT professional living in Australia. He has over 10 years of professional experience in design and development of enterprise applications, mostly focused on Microsoft technologies and holds a B.Sc in software engineering. Daniel is currently working at 3P Learning, which is a global leader in online learning with its flagship platform, Mathleticsused by more than 4 million students worldwide. For further details you can visit his website at http://baharestani.com.

    Browse publications by this author
Latest Reviews (4 reviews total)
Gostei muito dos livros que eu comprei.
Totally unreadable... book because 1/3 of it wrong, 1/3 blank, 1/3 right book
Good book supplied by Packt.
Mastering Ninject for Dependency Injection
Unlock this book and the full library FREE for 7 days
Start now