Skip to content

Instantly share code, notes, and snippets.

@mowen
Created August 22, 2010 17:30
Show Gist options
  • Select an option

  • Save mowen/544027 to your computer and use it in GitHub Desktop.

Select an option

Save mowen/544027 to your computer and use it in GitHub Desktop.

Revisions

  1. mowen created this gist Aug 22, 2010.
    15 changes: 15 additions & 0 deletions BadService.cs
    Original 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.";
    }
    }
    }
    22 changes: 22 additions & 0 deletions ConsumerA.cs
    Original 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();
    }
    }
    }
    22 changes: 22 additions & 0 deletions ConsumerB.cs
    Original 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();
    }
    }
    }
    12 changes: 12 additions & 0 deletions IService.cs
    Original 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();
    }
    }
    15 changes: 15 additions & 0 deletions NiceService.cs
    Original 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.";
    }
    }
    }
    47 changes: 47 additions & 0 deletions Program.cs
    Original 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");
    }
    }
    }
    16 changes: 16 additions & 0 deletions ServiceFactory.cs
    Original 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);
    }
    }
    }