This blog posts header can in the noble spirit of recycling be reused the day an epic dirk will be available to steal on the Isle of Conquest in WoW.
The topic of this blog post is on the contrary an introduction in getting started using the IoC (Inversion of Control) Container Framework Ninject for DI (Dependency Injection). We will focus on how to get started and not dig deeper into advanced (covered in future blog posts) areas or complicate things more than necessary while you still learn how to crawl like a ninja.
DI from an object oriented programming perspective is a technique to make your applications components require it’s dependencies to be passed (injected) from the outside. This is accomplished by not specifying a specific implementation inside the components, instead only the type of service they need in form of a contract. This technique assists in avoiding tightly coupled classes.
An IoC Container can support you in this work and make it easier to change and extend your beloved application architecture. Ninject is the pick of this blog post but there are numerous of IoC Containers, just to name a few:
What makes Ninject different than other IoC Containers? The following statement can be read at the Ninject homepage:
“An obsessive focus on simplicity and ease of use. We’ve seen the value of dependency injection, but were frustrated by the complexity of existing solutions. The goal of Ninject is make dependency injection accessible to all developers and all projects.”
This philosophy together with the amusing Ninja influences and lot of extensions make the open source project Ninject worth looking into.
Let’s visit the Dojo for some coding. We will create a simple entertaining application and use Ninject for some DI.
1 Fire up Visual Studio 2010 and create a new c# Console Application project.
2 Use NuGet and add the latest Ninject core package to your project.
NuGet is a Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects that use the .NET Framework. Please spend five minutes and learn how to use NuGet if it is new to you.
3 Type or paste in the following code in Program.cs