Have you built an application using an IoC container? If you have you'll know how easy it makes creating decoupled, component oriented software. It's certainly revolutionised the way I think about application design. One frustration though, with following this approach is that most application frameworks don't assume an IoC container, or if they do it's not the one that you've decided to use.
I've been reasonably happy using the MVC Framework on recent projects, but even though one of its stated aims is to support IoC centric development, you still have to jump through a few hoops to make it play nicely with a container. Many other frameworks don't provide any hook for service location so you end up having to write some kind of adaptor in order for them to work with IoC style software.
This is why I'm really excited by this announcement by Glen Block about a common IServiceLocator. It's proposed as a common interface for any IoC container; a meeting point between frameworks, containers and application components. It's really simple and, as Glen says, simply about locating services, not registering them:
namespace Microsoft.Practices.ServiceLocation
{public interface IServiceLocator : IServiceProvider{object GetInstance(Type serviceType);
object GetInstance(Type serviceType, string key);IEnumerable<object> GetAllInstances(Type serviceType);
TService GetInstance<TService>();TService GetInstance<TService>(string key);
IEnumerable<TService> GetAllInstances<TService>();}}
There's also a static ServiceLocator class that provides a single point of access to your particular service locator:
namespace Microsoft.Practices.ServiceLocation
{public static class ServiceLocator{private static ServiceLocatorProvider currentProvider;public static IServiceLocator Current{get { return currentProvider(); }}public static void SetLocatorProvider(ServiceLocatorProvider newProvider){currentProvider = newProvider;}}}
Now all that's required is for this to get widespread adoption. Microsoft can lead the way here. For starters I'd really like to see the MVC Framework team adopt it.
Sorry Glenn, I spelt your name with only one n. Thanks to Nicholas Blumhardt for pointing that out!
ReplyDelete