Book Image

Mastering Ninject for Dependency Injection

By : Daniel Baharestani
Book Image

Mastering Ninject for Dependency Injection

By: Daniel Baharestani

Overview of 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.
Table of Contents (12 chapters)

Preface

Mastering Ninject for Dependency Injection demonstrates how Ninject facilitates the implementation of Dependency Injection to solve common design problems of real-life applications in a simple and easy-to-understand format. This book will teach you everything you need in order to implement Dependency Injection using Ninject in a real-life project. Not only does it teach the Ninject core framework features which are essential for implementing DI, but it also explores the power of Ninject's most useful extensions, and demonstrates how to apply them in a real-life application.

What this book covers

Chapter 1, Understanding Dependency Injection, introduces Dependency Injection concepts and describes the advantages of using this technique. We will also go through a simple example and implement the principles and patterns related to DI techniques. After understanding what a DI container is, we will discuss why Ninject is a suitable choice.

Chapter 2, Getting Started with Ninject, teaches the user how to add Ninject to a practical project and how to use the basic features of this framework. The chapter starts with an example demonstrating how to set up and use Ninject in a Hello World project. Then, we will talk about how Ninject resolves dependencies and how it manages object lifetime. We will also cover the code-based configuration using Ninject modules and XML-based configuration. The final section of this chapter describes how to configure a large application which includes hundreds of services using Ninject conventions. By the end of this chapter, the user will be able to set up and use the basic features of Ninject.

Chapter 3, Meeting Real-world Requirements, introduces more advanced features of Ninject which are necessary in order to implement DI in real-world situations. The chapter starts with an introduction to some patterns and antipatterns related to Ninject. We will then go through real examples and see how Ninject can solve such kind of problems. By the end of this chapter, the user is expected to know almost all of the significant features of Ninject.

Chapter 4, Ninject in Action, shows how to set up different types of applications using Ninject. We will implement a concrete scenario using a variety of application types, including but not limited to, WPF, ASP .NET MVC, and WCF, to see how to set up and use Ninject for injecting the dependencies. By the end of this chapter, the user should be able to set up and use Ninject for all kinds of described applications.

Chapter 5, Doing More with Extensions, will show how Interception is a solution for cross-cutting concerns, and how to use Mocking Kernel as a test asset. While the core library of Ninject is kept clean and simple, Ninject is a highly extensible DI container, and it is possible to extend its power by using extension plugins. We will also see how Ninject can be extended.

What you need for this book

The examples of the book are written in Microsoft Visual Studio 2012; however, the target framework is set to .NET 4.0 so that they can be easily built using MSBuild and .NET Framework 4.0, even if you do not have Visual Studio 2012.

In the ASP.NET MVC application, we used MVC 3, and Microsoft SQL Server Compact 4.0 is used for SQL Data Layer.

You need an Internet connection to download required references and online packages, such as Ninject and its extensions. Having NuGet package manager on your system facilitates installing of referenced packages, but it is not required, as wherever we need to install such packages, the instruction for manually downloading and referencing the binaries is also provided.

We have also used NUnit for our Unit Tests, which is freely available for download via NuGet or NUnit website.

Who this book is for

This book is for all software developers and architects who are willing to create maintainable, loosely coupled, extensible, and testable applications. Because Ninject targets the .NET platform, this book is not suitable for software developers of other platforms. You should be comfortable with object oriented principals, and have a fair understanding of inheritance and abstraction. Being familiar with design patterns and general concept of unit testing is also a great help, but no knowledge of Dependency Injection is assumed. Although Ninject can be used in any .NET programming languages, the examples of this book are all in C#, so the reader is assumed to be familiar with this language.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "The following example shows how to use the ILogger interface."

A block of code is set as follows:

[Inject]
public ILogger Logger {get; set;}

public void DoSomething()
{
    Logger.Debug("Doing something...");
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

 kernel.Bind(x => x
    .FromThisAssembly()
    .SelectAllClasses()
    .InNamespaces("Northwind.Controllers")
    .BindBase());

Any command-line input or output is written as follows:

2013-05-23 05:04:40 INFO  LogSamples.Consumer - Doing something...

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The first one is called when the hyperlink Create New is clicked using HTTP GET method ".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.