Skip to content

Instantly share code, notes, and snippets.

@ThorstenHans
Created September 15, 2011 15:27
Show Gist options
  • Save ThorstenHans/1219551 to your computer and use it in GitHub Desktop.
Save ThorstenHans/1219551 to your computer and use it in GitHub Desktop.
Generic Service Locator
public class ServiceLocator
{
//Some kind of singleton is here...
// only the stuff that matters
public static void For<T>(params Func<T>[] regs)
{
// ...
}
public static T Use<T>()
{
// ..
}
}
// usage
ServiceLocator.Instance.For<IFoo>(ServiceLocator.Use<Foo>(),ServiceLocator.Use<Bar>());
// nice but something like
ServiceLocator.Instance.For<IFoo>(Use<Foo>(),Use<Bar>());
// seems to be more elegant...
// Use<T> is a extension method on object, so its a global method...
// working approach is
SerivceLocator.Instance.For<IFoo>().Use<Foo>().Use<FooBar>().Use<Bar>();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment