Skip to content

Instantly share code, notes, and snippets.

@JonDouglas
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonDouglas/7a8c15570d6475f69860 to your computer and use it in GitHub Desktop.
Save JonDouglas/7a8c15570d6475f69860 to your computer and use it in GitHub Desktop.
ServiceContainer
public static class ServiceContainer
{
static readonly Dictionary<Type, Lazy<object>> services = new Dictionary<Type, Lazy<object>>();
public static void Register<T>(Func<T> function)
{
services[typeof(T)] = new Lazy<object>(() => function());
}
public static T Resolve<T>()
{
return (T)Resolve(typeof(T));
}
public static object Resolve(Type type)
{
Lazy<object> service;
if (services.TryGetValue(type, out service))
{
return service.Value;
}
throw new Exception("Service not found!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment