I came across this post from Mark Seemann "Service Locator 2 Released". This post caught my attention as it was a while ago that I thought of it. The "service locator" mainly reminds me to the period I was still working as a consultant. Being a technical architect at that time I was deep into the J2EE patterns like the service locator.
Martin Fowler has an excellent read on Inversion of Control Containers and the Dependency Injection pattern, and how it contrast with the Service Locator alternative.
But back to the Service Locator 2. What is it and why would you use it?
Service Locator is an application block which integrates with Enterprise Library. It's a general-purpose, provider-driven abstract factory, which can be used to create instances of component servers, following the Service Locator design pattern. It supports configurable as well as run-time dependency injection.
If you're using any of the application blocks and the Enterprise Library than this is something for you. The 2.0 release of the Service Locator for Enterprise Library is a complete rewrite, targeting Microsoft patterns & practices Enterprise Library for .NET Framework 2.0 - January 2006. The platform is .NET 2.0. The project files targets Visual Studio 2005.
The highlights of this release include:
- Run-time injection. Service Locator 2 supports both static, configuration-based dependency injection, and dynamic, run-time injection. Static injection is the simple, specialized case where you only need to map from an abstract to a concrete type, but don't need to inject a particular instance of a concrete type. In some cases, e.g. when using dynamic mock objects, it is necessary to be able to inject instances that are first created at run-time. Service Locator 2 supports both cases.
- Ease of use. It is easy to get started with using Service Locator 2: Just make a project reference to Service Locator 2, and start creating dependency instances using the ServiceFactory class.
- Optional configuration. Depending on scenarios, configuration of Service Locator 2 is optional. Service Locator 2 uses a pipeline of strategies to create service instances. If a strategy can create the requested service without use of application configuration, no configuration is necessary. This makes it easy to get started. Configuration can be added later as necessary, which will always be the case if it is necessary to map from an abstract to a concrete type.
- Integration. Service Locator 2 integrates with Enterprise Library, plugging in to the Enterprise Libary configuration application, and using Object Builder for its implementation.