public void AsyncScopedLifestyleDispose_WithInstanceExplicitlyRegisteredForDisposal_DisposesThatInstance()
        {
            // Arrange
            var container = ContainerFactory.New();

            var scopedLifestyle = new AsyncScopedLifestyle();

            // Transient
            container.Register <DisposableCommand>();

            container.RegisterInitializer <DisposableCommand>(instance =>
            {
                Scope scope = scopedLifestyle.GetCurrentScope(container);

                // The following line explicitly registers the transient DisposableCommand for disposal when
                // the execution context scope ends.
                scope.RegisterForDisposal(instance);
            });

            DisposableCommand command;

            // Act
            using (AsyncScopedLifestyle.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 GetCurrentAsyncScopedLifestyle_DisposingTheMiddleScopeBeforeInnerScope_ReturnsOuterScope()
        {
            // Arrange
            var container = new Container();
            var lifestyle = new AsyncScopedLifestyle();

            var instanceToDispose = new DisposableCommand();

            container.Register <DisposableCommand>(lifestyle);

            using (Scope outerScope = AsyncScopedLifestyle.BeginScope(container))
            {
                Scope middleScope = AsyncScopedLifestyle.BeginScope(container);

                Scope innerScope = AsyncScopedLifestyle.BeginScope(container);

                middleScope.Dispose();
                innerScope.Dispose();

                // Act
                Scope actualScope = lifestyle.GetCurrentScope(container);

                // Assert
                Assert.AreSame(outerScope, actualScope,
                               "Since the middle scope is already disposed, the current scope should be the outer.");
            }
        }
        public void GetCurrentAsyncScopedLifestyle_AfterMiddleScopeDisposedWhileInnerScopeNotDisposed_ReturnsOuterScope()
        {
            // Arrange
            var container = new Container();
            var lifestyle = new AsyncScopedLifestyle();

            var instanceToDispose = new DisposableCommand();

            container.Register <DisposableCommand>(lifestyle);

            using (Scope outerScope = AsyncScopedLifestyle.BeginScope(container))
            {
                Scope middleScope = AsyncScopedLifestyle.BeginScope(container);

                Scope innerScope = AsyncScopedLifestyle.BeginScope(container);

                middleScope.Dispose();

                // Act
                Scope actualScope = lifestyle.GetCurrentScope(container);

                // Assert
                Assert.AreSame(outerScope, actualScope);
            }
        }
Example #4
0
        /// <inheritdoc />
        /// <summary>
        /// </summary>
        public IDependencyScope BeginScope()
        {
            var beginScope = _scopeOption == DependencyResolverScopeOption.RequiresNew ||
                             _scopedLifestyle.GetCurrentScope(_container) == null;

            return(new SimpleInjectorDependencyResolver(_container, beginScope));
        }
Example #5
0
        public void OnPerformed(PerformedContext filterContext)
        {
            var scope = lifestyle.GetCurrentScope(_container);

            if (scope != null)
            {
                scope.Dispose();
            }
        }
        public void GetCurrentScope_WithCurrentScopeSet_ReturnsTheSetScope()
        {
            var lifestyle     = new AsyncScopedLifestyle();
            var container     = new Container();
            var expectedScope = new Scope(container);

            // Act
            lifestyle.SetCurrentScope(expectedScope);

            // Assert
            var actualScope = lifestyle.GetCurrentScope(container);

            Assert.AreSame(expectedScope, actualScope);
        }