Skip to content

Instantly share code, notes, and snippets.

@mowen

mowen/BadService.cs

Created Aug 22, 2010
Embed
What would you like to do?
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.";
}
}
}
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();
}
}
}
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();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Experiments
{
public interface IService
{
string ProvideService();
}
}
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.";
}
}
}
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");
}
}
}
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.