The Common Service Locator (CSL) library serves as a shared interface for service location, aimed at both application and framework developers. By providing an abstraction layer over IoC (Inversion of Control) containers and service locators, the CSL library allows applications to access these capabilities indirectly, without the need for hard references. This design ensures that third-party applications and frameworks can leverage IoC and service location features without being locked into a specific implementation. Ultimately, the goal is to promote flexibility and interoperability across various development ecosystems.
vb2ae.ServiceLocator.MSDependencyInjection is an implementation of the Common Service Locator (CSL) for the Microsoft.Extensions.DependencyInjection library. Currently, it uses version 9 of Microsoft.Extensions.DependencyInjection, enabling it to support all Keyed services effectively.
The ConfigureServices method is where we are registering the Objects for dependency injection
private IServiceProvider Build()
{
if (_built)
throw new InvalidOperationException("Build can only be called once.");
_built = true;
_defaultBuilder.ConfigureServices((context, services) =>
{
services.AddSingleton<IService, ServiceImpl>();
services.AddTransient<ICar, Ford>();
services.AddTransient<ICar, Toyota>();
services.AddTransient<ICar, Chevy>();
services.AddKeyedTransient<IPet, Dog>("Dog");
services.AddKeyedTransient<IPet, Cat>("Cat");
});
_services = _defaultBuilder.Build().Services;
CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new vb2ae.ServiceLocator.MSDependencyInjection.MSDependencyInjectionServiceLocator(_services));
return _services;
}
The line of code below registers vb2ae.ServiceLocator.MSDependencyInjection with the CommonServiceLocator
CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new vb2ae.ServiceLocator.MSDependencyInjection.MSDependencyInjectionServiceLocator(_services));
return _services;
var serviceType = typeof(IService);
var result = CommonServiceLocator.ServiceLocator.Current.GetService(serviceType);
This returns a ServiceImpl
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IService>();
This returns a ServiceImpl
var serviceType = typeof(IPet);
var key = "Cat";
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType, key);
This returns an instance of Cat
var key = "Dog";
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IPet>(key);
This returns an instance of Dog
var serviceType = typeof(IService);
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType);
This returns a ServiceImpl
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance<IService>();
This returns a ServiceImpl
var result = CommonServiceLocator.ServiceLocator.Current.GetAllInstances(typeof(IPet));
Will return an IEnumerable that contains a Cat and Dog
var result = CommonServiceLocator.ServiceLocator.Current.GetAllInstances<ICar>()
Will return an IEnumerable that contains a Ford, Chevy and Toyota
Passing a null in as the key will return the first of the Type registered
var serviceType = typeof(IPet);
var result = CommonServiceLocator.ServiceLocator.Current.GetInstance(serviceType, null);
Will return the First IPet registered for DependencyInjection in this case an instance of the Dog class
All examples are based on what is registered in the How to register your services section