Skip to content

Instantly share code, notes, and snippets.

@plioi
Last active January 27, 2016 00:28
Show Gist options
  • Save plioi/3ee4aaac59dad90e2ff4 to your computer and use it in GitHub Desktop.
Save plioi/3ee4aaac59dad90e2ff4 to your computer and use it in GitHub Desktop.
public class NestedContainerPerCase : CaseBehavior
{
public void Execute(Case context, Action next)
{
TestDependencyScope.Begin();
next();
TestDependencyScope.End();
}
}
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