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

            container.RegisterSingle <IRepository, RepositoryThatDependsOnLogger>();

            // Since InterceptWith alters the Expression of ILogger, this would make it harder for
            // the Expression visitor of RegisterWithContext to find and alter this expression. So this is
            // an interesting test.
            container.InterceptWith <FakeInterceptor>(type => type == typeof(IContextualLogger));

            container.RegisterWithContext <IContextualLogger>(context => new ContextualLogger(context));

            // Act
            var repository = container.GetInstance <IRepository>();

            // Assert
            Assert_IsIntercepted(repository.Logger);
            Assert.AreEqual(typeof(IRepository), repository.Logger.Context.ServiceType);
            Assert.AreEqual(typeof(RepositoryThatDependsOnLogger), repository.Logger.Context.ImplementationType);
        }
        public void GetInstance_ResolvingConditionallyDecoratedInstanceWithConditionalPredicate_AppliesDecoratorsBasedOnPredicate()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.Options.EnableContextualDecoratorSupport();

            container.Register <ICommandHandler <RealCommand>, NullCommandHandler <RealCommand> >();

            container.RegisterContextualDecorator(typeof(ICommandHandler <>), typeof(CommandHandlerDecorator <>),
                                                  parameter => parameter.Name.StartsWith("cached"));

            // Act
            // Consumer has a constructor argument named 'dependency'
            var consumer = container.GetInstance <Consumer <ICommandHandler <RealCommand> > >();

            // CachedConsumer has a constructor argument named 'cachedDependency'
            var cachedConsumer = container.GetInstance <CachedConsumer <ICommandHandler <RealCommand> > >();

            // Assert
            AssertThat.IsInstanceOfType(typeof(NullCommandHandler <RealCommand>), consumer.Dependency);
            AssertThat.IsInstanceOfType(typeof(CommandHandlerDecorator <RealCommand>), cachedConsumer.Dependency);
        }
        public void InterceptWithGenericArgAndPredicate_RequestingAConcreteType_WillNotBeIntercepted()
        {
            // Arrange
            var logger = new FakeLogger();

            var container = ContainerFactory.New();

            container.RegisterInstance <ILogger>(logger);

            container.InterceptWith <InterceptorThatLogsBeforeAndAfter>(type => type == typeof(ICommand));

            container.RegisterInitializer <InterceptorThatLogsBeforeAndAfter>(i => i.BeforeText = "Start ");
            container.RegisterInitializer <CommandThatLogsOnExecute>(c => c.ExecuteLogMessage   = "Executing");
            container.RegisterInitializer <InterceptorThatLogsBeforeAndAfter>(i => i.AfterText  = " Done");

            // Act
            var command = container.GetInstance <CommandThatLogsOnExecute>();

            command.Execute();

            // Assert
            Assert.AreEqual("Executing", logger.Message);
            Assert.IsTrue(command.GetType() == typeof(CommandThatLogsOnExecute));
        }