Skip to content

Instantly share code, notes, and snippets.

@gabrieljoelc
Created October 29, 2013 19:56
Show Gist options
  • Save gabrieljoelc/7221467 to your computer and use it in GitHub Desktop.
Save gabrieljoelc/7221467 to your computer and use it in GitHub Desktop.
public class ServiceLocatorStub : ServiceLocatorImplBase
{
private readonly IDictionary<Type, ICollection<object>>
registeredTypes = new Dictionary<Type, ICollection<object>>();
private ServiceLocatorStub()
{}
public ServiceLocatorStub AddInstance<TService>(TService instance)
{
ICollection<object> instanceCollection;
if(!registeredTypes.TryGetValue(typeof(TService), out instanceCollection))
{
instanceCollection = new List<object>();
registeredTypes[typeof (TService)] = instanceCollection;
}
instanceCollection.Add(instance);
return this;
}
public static ServiceLocatorStub Create()
{
var fakeServiceLocator = new ServiceLocatorStub();
ServiceLocator.SetLocatorProvider(() => fakeServiceLocator);
return fakeServiceLocator;
}
protected override object DoGetInstance(Type serviceType, string key)
{
return registeredTypes[serviceType].FirstOrDefault();
}
protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
return registeredTypes[serviceType];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment