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

            var expectedSingletonHandler = new RealCommandCommandHandler();

            // Use the RegisterAll(Type, IEnumerable) overload.
            container.RegisterCollection(typeof(ICommandHandler<RealCommand>), new[] { expectedSingletonHandler });

            container.RegisterDecorator(typeof(ICommandHandler<>), typeof(RealCommandCommandHandlerDecorator));

            // Act
            var handlers = container.GetAllInstances<ICommandHandler<RealCommand>>();

            var decorator = handlers.Single();

            // Assert
            AssertThat.IsInstanceOfType(typeof(RealCommandCommandHandlerDecorator), decorator);

            Assert.IsTrue(object.ReferenceEquals(expectedSingletonHandler,
                ((RealCommandCommandHandlerDecorator)decorator).Decorated));
        }
        public void GetAllInstances_CollectionManuallyRegisteredAndFuncDecoraterRegistered4_ThrowsExpectedException()
        {
            // Arrange
            var container = ContainerFactory.New();

            var expectedSingletonHandler = new RealCommandCommandHandler();

            // Use the Register<T>(Func<T>) method. This is a strange (but legal) way of registering a service,
            // but will not work with a Func-Decorator.
            container.Register<IEnumerable<ICommandHandler<RealCommand>>>(() => new[] { expectedSingletonHandler });

            container.RegisterDecorator(typeof(ICommandHandler<>), typeof(AsyncCommandHandlerProxy<>));

            try
            {
                // Act
                container.GetAllInstances<ICommandHandler<RealCommand>>();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                Assert_ExceptionContainsInfoAboutManualCollectionRegistrationMixedDecoratorsThatTakeAFunc(ex);
            }
        }
        public void GetAllInstances_TypeDecoratedWithFuncDecorator2_InjectsADelegateThatCanCreateThatInstance()
        {
            // Arrange
            var container = ContainerFactory.New();

            var expectedSingletonHandler = new RealCommandCommandHandler();

            // Use the RegisterAll<T>(T[]) overload.
            container.RegisterCollection<ICommandHandler<RealCommand>>(expectedSingletonHandler);

            container.RegisterDecorator(typeof(ICommandHandler<>), typeof(AsyncCommandHandlerProxy<>));

            // Act
            var handlers = container.GetAllInstances<ICommandHandler<RealCommand>>();

            var decorator = (AsyncCommandHandlerProxy<RealCommand>)handlers.Single();

            var handler = decorator.DecorateeFactory();

            // Assert
            AssertThat.IsInstanceOfType(typeof(RealCommandCommandHandler), handler);
        }
        public void GetAllInstances_CollectionManuallyRegisteredAndFuncDecoraterRegistered2_ThrowsExpectedException()
        {
            // Arrange
            var container = ContainerFactory.New();

            var expectedSingletonHandler = new RealCommandCommandHandler();

            // Use the RegisterSingle<IEnumerable<T>> method
            container.RegisterSingleton<IEnumerable<ICommandHandler<RealCommand>>>(new[] { expectedSingletonHandler });

            container.RegisterDecorator(typeof(ICommandHandler<>), typeof(AsyncCommandHandlerProxy<>));

            try
            {
                // Act
                container.GetAllInstances<ICommandHandler<RealCommand>>();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                Assert_ExceptionContainsInfoAboutManualCollectionRegistrationMixedDecoratorsThatTakeAFunc(ex);
            }
        }
        public void GetAllInstances_TypeDecorated4_ReturnsCollectionWithDecorators()
        {
            // Arrange
            var container = ContainerFactory.New();

            var expectedSingletonHandler = new RealCommandCommandHandler();

            // Use the RegisterSingle<IEnumerable<T>> method.
            container.RegisterSingleton<IEnumerable<ICommandHandler<RealCommand>>>(new[] { expectedSingletonHandler });

            container.RegisterDecorator(typeof(ICommandHandler<>), typeof(RealCommandCommandHandlerDecorator));

            // Act
            var handlers = container.GetAllInstances<ICommandHandler<RealCommand>>();

            var decorator = handlers.Single();

            // Assert
            AssertThat.IsInstanceOfType(typeof(RealCommandCommandHandlerDecorator), decorator);

            var decoratedInstance = ((RealCommandCommandHandlerDecorator)decorator).Decorated;

            Assert.IsNotNull(decoratedInstance);

            Assert.IsTrue(object.ReferenceEquals(expectedSingletonHandler, decoratedInstance),
                "Not the same instance. Decorated instance is instance of type: " +
                decoratedInstance.GetType().Name);
        }