Common Service Locator Library
Common Service Locator Library
introduction
Yesterday Glenn Block introduced
a Common Service Locator library
which is described as “a shared
interface that applications and
frameworks can reference in
order to leverage
IoC containers / service location
mechanisms without taking
hard dependencies”.
The idea for the library is based
on a post that Jeremy Miller
wrote of having a shared interface
across several IoC implementation.
The benefit of such an interface is
obvious - no dependency on a
particular IoC implementation.
The result was the birth of a new library – the common service locator library.
The Common Service Locator Library
The library currently holds the IServiceLocator interface which describe the
following contract:
public interface IServiceLocator : IServiceProvider
{
IEnumerable<TService> GetAllInstances<TService>();
IEnumerable<object> GetAllInstances(Type serviceType);
TService GetInstance<TService>();
TService GetInstance<TService>(string key);
object GetInstance(Type serviceType);
object GetInstance(Type serviceType, string key);
}
That contract was agreed by the following IoC containers frameworks creators:
Unity, Spring.Net, Castle Windsor, Structure Map and Ninject.
Other parts of the library include a static ServiceLocator class which
exposes the current container, a ServiceLocatorImplBase which is an abstract class
for building your own service locator and a ServiceLocatorProvider which is the
method to get the current container by the ServiceLocator static class.
Conclusion
The new library suppose to solve the problem of hard dependencies on
IoC containers / service location mechanisms. The library’s birth can demonstrate
the power of the community which see the need and create a library that is agreed
upon. I join the hope of Glenn Block that the new library will encourage products
and frameworks to start leveraging IoC / Service location frameworks.
Download the Common Service Locator library here.
I downloaded it yesterday…