Skip to content

Instantly share code, notes, and snippets.

@Vanlalhriata
Created April 15, 2016 11:55
Show Gist options
  • Save Vanlalhriata/45fb96dd09551144a8ae0035c53a891f to your computer and use it in GitHub Desktop.
Save Vanlalhriata/45fb96dd09551144a8ae0035c53a891f to your computer and use it in GitHub Desktop.
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