Skip to content

Instantly share code, notes, and snippets.

@plioi
Created January 26, 2016 22:23
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 plioi/02efa966a306f26b9ab2 to your computer and use it in GitHub Desktop.
Save plioi/02efa966a306f26b9ab2 to your computer and use it in GitHub Desktop.
public static class TestDependencyScope
{
private static IContainer _currentNestedContainer;
public static void Begin()
{
if (_currentNestedContainer != null)
throw new Exception("Cannot begin test dependency scope. Another dependency scope is still in effect.");
_currentNestedContainer = IoC.Container.GetNestedContainer();
}
public static IContainer CurrentNestedContainer
{
get
{
if (_currentNestedContainer == null)
throw new Exception($"Cannot access the {nameof(CurrentNestedContainer)}. There is no dependency scope in effect.");
return _currentNestedContainer;
}
}
public static void End()
{
if (_currentNestedContainer == null)
throw new Exception("Cannot end test dependency scope. There is no dependency scope in effect.");
_currentNestedContainer.Dispose();
_currentNestedContainer = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment