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)

Object Lifetime


One of the responsibilities of a DI container is to manage the lifetime of objects that it creates. It should decide when to create a new instance of the given type and when to use an existing instance. It should also take care of disposing of objects when they are not used anymore. Ninject has a strong support for managing Object Lifetime in different situations. Whenever we define a binding, we can declare the scope of the object instance being created. Within that scope, the object instance will be reused and exist exactly once for each binding. Note that an object is not allowed to have a dependency on an object with shorter lifetime.

Transient scope

In Transient scope, the object lifetime is not managed by Ninject. Whenever we request an instance of a type, a new one will be created. Ninject doesn't take care of keeping the created instance or disposing of it in this scope. This is the default object scope in Ninject. If no scope is explicitly specified, they are transient...