Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.