Created
April 15, 2016 11:55
-
-
Save Vanlalhriata/45fb96dd09551144a8ae0035c53a891f to your computer and use it in GitHub Desktop.
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 ServiceLocator | |
{ | |
public static Dictionary<Type, object> serviceContainer = null; | |
public static void AddService<T>(T serviceInstance) | |
{ | |
if (null == serviceContainer) | |
serviceContainer = new Dictionary<Type, object>(); | |
try | |
{ | |
serviceContainer.Add(typeof(T), (object)serviceInstance); | |
} | |
catch | |
{ | |
AppHelper.Logger.Log(LogCategory.Exception, Priority.Medium, "KinectService.ServiceLocator: Attempted to add duplicate service: " + typeof(T).Name); | |
} | |
} | |
public static T GetService<T>() | |
{ | |
try | |
{ | |
var service = (T)serviceContainer[typeof(T)]; | |
return service; | |
} | |
catch | |
{ | |
AppHelper.Logger.Log(LogCategory.Exception, Priority.Medium, "KinectService.ServiceLocator: Service not available: " + typeof(T).Name); | |
return default(T); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment