Purpose:
Ever read about dependency injection and wonder: Hey, how can I implement that easily into my .Net project yet still not pay a fortune to do it? Open Source is your friend in this instance.
Download + the install:
It's as simple as going to the source link below and clicking their download icon. They offer framework dependencies for 3.5 and newer, i'll go with 4.0 for my purposes. Adding the reference to your program is fairly straight forward like any other reference you've added in the past.
Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Ninject; using Ninject.Modules; namespace NinjectTutor { interface IVegetable { void Chomp(string target); } class Carrot : IVegetable { public void Chomp(string target) { Console.WriteLine("{0} What's up doc?", target); } } interface IWeapon { void Hit(string target); } class Shotgun : IWeapon { public void Hit(string target) { Console.WriteLine("{0} huh huh huh huh", target); } } class Fudd { readonly IWeapon weapon; public Fudd(IWeapon weapon) { this.weapon = weapon; } public void Attack(string target) { this.weapon.Hit(target); } } class Wabbit { readonly IVegetable veggie; public Wabbit(IVegetable veggie) { this.veggie = veggie; } public void Harass(string target) { this.veggie.Chomp(target); } } class Program { static void Main(string[] args) { Ninject.IKernel kernel = new StandardKernel(); kernel.Bind<IVegetable>().To<Carrot>(); var bugs = kernel.Get<Wabbit>(); bugs.Harass("Eh"); kernel.Bind<IWeapon>().To<Shotgun>(); var elmer = kernel.Get<Fudd>(); elmer.Attack("kill da wabbit! kill da wabbit!"); Console.WriteLine("\nPress Any Key to Exit."); Console.ReadKey(); } } }
and the result:
Source(s):
I easily burnt an hour trying to do the download through NuGet to do the install of Ninject into VS2013 express. If anyone can solve it please post below to help others. My best current guess is user rights related.
No comments:
Post a Comment