public void Visit_ConstantValue()
        {
            // Set up EntLib.
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer("abc"));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsType <ConstantParameterValue>(registrationParam);

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(string));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.NotNull(result);

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(string)).WithParameter(result);
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.Equal("abc", resolved.CtorParameter);
        }
        public void Visit_ResolvedTypedNamedValue()
        {
            // Set up EntLib.
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved <ISampleService>("named")));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsType <ContainerResolvedParameter>(registrationParam);

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(ISampleService));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.NotNull(result);

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(ISampleService)).WithParameter(result);
            var named = new SampleServiceImpl();

            builder.RegisterInstance(named).Named <ISampleService>("named");
            var notNamed = new SampleServiceImpl();

            builder.RegisterInstance(notNamed).As <ISampleService>();
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.Same(named, resolved.CtorParameter);
        }
        public void Visit_ConstantValue()
        {
            // Set up EntLib.
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer("abc"));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsInstanceOf <ConstantParameterValue>(registrationParam, "The parameter should have been seen by EntLib as a constant value.");

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(string));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.IsNotNull(result, "After visiting the registration value, the generated parameter should be set.");

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(string)).WithParameter(result);
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.AreEqual("abc", resolved.CtorParameter, "The constructor parameter was not properly set.");
        }
        public void Visit_ResolvedEnumerableValue()
        {
            // Set up EntLib.
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable <ISampleService>(itemNames)));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsAssignableFrom <ContainerResolvedEnumerableParameter>(registrationParam);

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(IEnumerable <ISampleService>));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.NotNull(result);

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(IEnumerable <ISampleService>)).WithParameter(result);
            var first = new SampleServiceImpl();

            builder.RegisterInstance(first).Named <ISampleService>("first");
            var second = new SampleServiceImpl();

            builder.RegisterInstance(second).Named <ISampleService>("second");
            var third = new SampleServiceImpl();

            builder.RegisterInstance(third).Named <ISampleService>("third");
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.IsAssignableFrom <IEnumerable <ISampleService> >(resolved.CtorParameter);
            var services = ((IEnumerable <ISampleService>)resolved.CtorParameter).ToArray();

            Assert.Same(first, services[0]);
            Assert.Same(second, services[1]);
            Assert.Same(third, services[2]);
        }
        public void Visit_ResolvedEnumerableValue()
        {
            // Set up EntLib.
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable <ISampleService>(itemNames)));
            var registrationParam = registration.ConstructorParameters.First();

            Assert.IsInstanceOf <ContainerResolvedEnumerableParameter>(registrationParam, "The parameter should have been seen by EntLib as a resolved enumerable parameter.");

            // Visit the parameter to get the Autofac equivalent.
            var ctorParam = this.GetCtorParam <RegisteredServiceConsumer>(typeof(IEnumerable <ISampleService>));
            var visitor   = new AutofacParameterBuilderVisitor(ctorParam);

            visitor.Visit(registrationParam);
            var result = visitor.AutofacParameter;

            Assert.IsNotNull(result, "After visiting the registration value, the generated parameter should be set.");

            // Verify the converted parameter resolves correctly.
            var builder = new ContainerBuilder();

            builder.RegisterType <RegisteredServiceConsumer>().UsingConstructor(typeof(IEnumerable <ISampleService>)).WithParameter(result);
            var first = new SampleServiceImpl();

            builder.RegisterInstance(first).Named <ISampleService>("first");
            var second = new SampleServiceImpl();

            builder.RegisterInstance(second).Named <ISampleService>("second");
            var third = new SampleServiceImpl();

            builder.RegisterInstance(third).Named <ISampleService>("third");
            var container = builder.Build();
            var resolved  = container.Resolve <RegisteredServiceConsumer>();

            Assert.IsInstanceOf <IEnumerable <ISampleService> >(resolved.CtorParameter, "The constructor parameter was not the right type.");
            var services = ((IEnumerable <ISampleService>)resolved.CtorParameter).ToArray();

            Assert.AreSame(first, services[0], "The first enumerable service was not resolved properly.");
            Assert.AreSame(second, services[1], "The second enumerable service was not resolved properly.");
            Assert.AreSame(third, services[2], "The third enumerable service was not resolved properly.");
        }