Last active
January 27, 2016 00:28
-
-
Save plioi/3ee4aaac59dad90e2ff4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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