Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Ninject for Dependency Injection

You're reading from   Mastering Ninject for Dependency Injection For .NET developers and architects, this is the ultimate guide to the principles of Dependency Injection and how to use the automating features of Ninject in the most effective way. Packed with examples, diagrams, and illustrations.

Arrow left icon
Product type Paperback
Published in Sep 2013
Publisher Packt
ISBN-13 9781782166207
Length 142 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Daniel Baharestani Daniel Baharestani
Author Profile Icon Daniel Baharestani
Daniel Baharestani
Arrow right icon
View More author details
Toc

DI Containers


A DI container is an injector object that injects the dependencies into a dependent object. As we have seen in the previous example, we don't necessarily need a DI container in order to implement Dependency Injection. However, in more complex scenarios, a DI container can save a lot of time and effort by automating most of the tasks that we had to do manually. In real world applications, a single dependant class can have many dependencies, each of which have their own dependencies that forms a large graph of dependencies. A DI container should resolve the dependencies, and this is where the decision of selecting a concrete class for the given abstraction should be made. This decision is made by a mapping table, which is either based on a configuration file or is programmatically defined by the developer. We can see an example for both here:

<bind service="ILogger" to="ConsoleLogger" /> 

This one is an example of code-based configuration:

Bind<ILogger>().To<ConsoleLogger>();

We can also define conditional rules instead of just mapping a service to a concrete type. We will discuss this feature in detail in Chapter 2, Getting Started with Ninject.

A container has the responsibility of dealing with the lifetime of the created objects. It should know how long an object should be kept alive, when to dispose of it, in what condition to return the existing instance, and in what condition to create a new one.

Note

DI Containers are also known as IoC Containers.

There are other DI Container besides Ninject. You can find a list of them in Scott Hanselman's blog (http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx). Unity, Castle Windsor, StructureMap, Spring.NET, and Autofac are a few of them:

 

Unity

Castle Windsor

StructureMap

Spring.NET

Autofac

License

MS-PL

Apache 2

Apache 2

Apache 2

MIT

Description

Build on the "kernel" of ObjectBuilder.

Well documented and used by many.

Written by Jeremy D. Miller.

Written by Mark Pollack.

Written by Nicholas Blumhardt and Rinat Abdullin.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image