Enhanced C#
Language of your choice: library documentation
Static Public Member Functions | List of all members
Loyc.ServiceProvider Class Reference

Adds extension methods to modernize .NET's simple built-in service locator. More...

Source file:


Adds extension methods to modernize .NET's simple built-in service locator.

.NET has had a litte-known built-in simple service locator since .NET 1.1 called System.ComponentModel.Design.ServiceContainer, which implements both IServiceContainer and IServiceProvider. IServiceContainer lets you add services (associating a Type object with a factory or a singleton):

interface IServiceContainer {
    void AddService(Type serviceType, object serviceInstance);
    void AddService(Type serviceType, ServiceCreatorCallback callback);
delegate object ServiceCreatorCallback(IServiceContainer container, Type serviceType);

Meanwhile, IServiceProvider is a simple service locator:

public interface IServiceProvider
    object GetService(Type serviceType);

This class adds three extension methods to make IServiceContainer and IServiceProvider generic, so you can write code like

   var services = new ServiceContainer();
   services.AddService<IFoo>(p => new Foo());
   // elsewhere in your code...
   IFoo newFoo = services.GetService<IFoo>();

Remember that service locators are generally an antipattern! However, if you have one of those rare, legitimate needs to use one, you should prefer the IServiceProvider interface built into .NET (if it is enough for your needs) so that your lower-level code avoids taking an unnecessary dependency on an IoC framework.

See also: http://core.loyc.net/essentials/ambient-service-pattern.html

Thanks to Roger's blog for the idea of these extension methods.

Static Public Member Functions

static TInterface GetService< TInterface > (this IServiceProvider provider)
static void AddService< TInterface > (this IServiceContainer container, Func< IServiceProvider, TInterface > factory)
static void RemoveService< TInterface > (this IServiceContainer container)