Last active
August 29, 2015 14:03
-
-
Save JonDouglas/7a8c15570d6475f69860 to your computer and use it in GitHub Desktop.
ServiceContainer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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