public void LifetimeScopeDispose_WithInstanceExplicitlyRegisteredForDisposal_DisposesThatInstance() { // Arrange var container = new Container(); var lifestyle = new ThreadScopedLifestyle(); // Transient container.Register <ICommand, DisposableCommand>(); container.RegisterInitializer <DisposableCommand>(instance => { Scope scope = lifestyle.GetCurrentScope(container); // The following line explictly registers the transient DisposableCommand for disposal when // the lifetime scope ends. scope.RegisterForDisposal(instance); }); DisposableCommand command; // Act using (ThreadScopedLifestyle.BeginScope(container)) { command = container.GetInstance <DisposableCommand>(); } // Assert Assert.IsTrue(command.HasBeenDisposed, "The transient instance was expected to be disposed, because it was registered for disposal."); }
public void GetCurrentLifetimeScope_InsideANestedLifetimeScope_ReturnsTheInnerMostScope() { // Arrange var container = new Container(); var lifestyle = new ThreadScopedLifestyle(); using (var outerScope = ThreadScopedLifestyle.BeginScope(container)) { using (var innerScope = ThreadScopedLifestyle.BeginScope(container)) { Assert.IsFalse(object.ReferenceEquals(outerScope, innerScope), "Test setup failed."); // Act var currentScope = lifestyle.GetCurrentScope(container); // Assert Assert.IsTrue(object.ReferenceEquals(innerScope, currentScope)); } } }