Created
August 22, 2010 17:30
-
-
Save mowen/544027 to your computer and use it in GitHub Desktop.
Revisions
-
mowen created this gist
Aug 22, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Experiments { public class BadService : IService { public string ProvideService() { return "This is the bad service."; } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Experiments { public class ConsumerA { private IService _service; public ConsumerA(IService service) { _service = service; } public string ProvideService() { return _service.ProvideService(); } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Experiments { public class ConsumerB { private IService _service; public ConsumerB(IService service) { _service = service; } public string ProvideService() { return _service.ProvideService(); } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Experiments { public interface IService { string ProvideService(); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Experiments { public class NiceService : IService { public string ProvideService() { return "This is the nice service."; } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Experiments; using Ninject; using Ninject.Modules; using CommonServiceLocator.NinjectAdapter; using Microsoft.Practices.ServiceLocation; namespace NinjectTestConsole { class Program { static void Main(string[] args) { var kernel = new StandardKernel(new ServiceModule()); ServiceLocator.SetLocatorProvider(() => new NinjectServiceLocator(kernel)); var consumerA = kernel.Get<ConsumerA>(); Console.WriteLine("Consumer A: {0}", consumerA.ProvideService()); var consumerB = kernel.Get<ConsumerB>(); Console.WriteLine("Consumer B: {0}", consumerB.ProvideService()); var niceService = ServiceFactory.CreateService("NiceService"); Console.WriteLine("Nice Service: {0}", niceService.ProvideService()); var badService = ServiceFactory.CreateService("BadService"); Console.WriteLine("Bad Service: {0}", badService.ProvideService()); Console.ReadKey(); } } class ServiceModule : NinjectModule { public override void Load() { Bind<IService>().To<BadService>().WhenInjectedInto<ConsumerA>(); Bind<IService>().To<NiceService>().WhenInjectedInto<ConsumerB>(); Bind<IService>().To<BadService>().Named("BadService"); Bind<IService>().To<NiceService>().Named("NiceService"); } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Practices.ServiceLocation; namespace Experiments { public class ServiceFactory { public static IService CreateService(string serviceName) { return ServiceLocator.Current.GetInstance<IService>(serviceName); } } }