Skip to content

Instantly share code, notes, and snippets.

@donaldgray
Created November 19, 2015 14:17
Show Gist options
  • Save donaldgray/18e6ccd29c78f17a7dd8 to your computer and use it in GitHub Desktop.
Save donaldgray/18e6ccd29c78f17a7dd8 to your computer and use it in GitHub Desktop.
/// <summary>
/// This is just a helper class to make resolving dependencies manually a bit tidier
/// </summary>
public static class WebApiDependencyResolver
{
/// <summary>
/// Use configured WebApi DependencyResolver to resolve dependency of type T.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T ResolveDependency<T>()
{
var resolver = GlobalConfiguration.Configuration.DependencyResolver;
var type = typeof(T);
return (T)resolver.GetService(type);
}
/// <summary>
/// Resolve dependency of type T using IDependencyScope.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="scope">The scope.</param>
/// <returns></returns>
public static T ResolveDependency<T>(this IDependencyScope scope)
{
var type = typeof(T);
return (T)scope.GetService(type);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment