NInject Example
July 2, 2013 Leave a comment
I started looking at NInject do handle the dependency injection that I normally do by hand. To that end, I spun up the project that I used for my last foray into the world of dependency injection:
- public interface IAnimal
- {
- void Talk();
- void Eat();
- }
And
- public class Elephant: IAnimal
- {
- public void Talk()
- {
- Console.WriteLine("Trumpet");
- }
- public void Eat()
- {
- Console.WriteLine("Yummy Peanuts");
- }
- }
- public class Seal: IAnimal
- {
- public void Talk()
- {
- Console.WriteLine("Ark Ark");
- }
- public void Eat()
- {
- Console.WriteLine("Yummy Fish");
- }
- }
Mow, instead of using Activator.CreateInstance, I am going to use NInject to do the mapping, To that end, I installed NInject via NuGet
I then coded up the example for the on-line tutorial using the StandardKernal class like so
- static void Main(string[] args)
- {
- IKernel kernel = new StandardKernel();
- IAnimal animal = kernel.Get<Elephant>();
- animal.Talk();
- animal.Eat();
- Console.ReadKey();
- }
and sure enough
So this is good stuff. I think NInject and MOQ are my two favorite new tools in the toolbox…