public void RegisterTypeRegistration_Default_WithEnumerationParameter()
        {
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration = new TypeRegistration<RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable<ISampleService>(itemNames)));
            registration.IsDefault = true;
            registration.Lifetime = TypeRegistrationLifetime.Transient;

            var builder = new ContainerBuilder();
            builder.RegisterTypeRegistration(registration);
            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.");
        }
        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_ResolvedTypedValue()
        {
            // Set up EntLib.
            var registration      = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved <ISampleService>()));
            var registrationParam = registration.ConstructorParameters.First();

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

            // 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.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(ISampleService)).WithParameter(result);
            var service = new SampleServiceImpl();

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

            Assert.AreSame(service, 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.");
        }
        public void RegisterTypeRegistration_Named_WithParameters()
        {
            var registration = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved <ISampleService>()));

            registration.Name     = "named-service";
            registration.Lifetime = TypeRegistrationLifetime.Transient;

            var dependency = new SampleServiceImpl();
            var builder    = new ContainerBuilder();

            builder.RegisterTypeRegistration(registration);
            builder.RegisterInstance(dependency).As <ISampleService>();
            var container = builder.Build();

            var instance = container.ResolveNamed <RegisteredServiceConsumer>("named-service");

            Assert.Same(dependency, instance.CtorParameter);
            var instance2 = container.ResolveNamed <RegisteredServiceConsumer>("named-service");

            Assert.NotSame(instance, instance2);
        }
        public void RegisterTypeRegistration_Named_WithParameters()
        {
            var registration = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved <ISampleService>()));

            registration.Name     = "named-service";
            registration.Lifetime = TypeRegistrationLifetime.Transient;

            var dependency = new SampleServiceImpl();
            var builder    = new ContainerBuilder();

            builder.RegisterTypeRegistration(registration);
            builder.RegisterInstance(dependency).As <ISampleService>();
            var container = builder.Build();

            var instance = container.ResolveNamed <RegisteredServiceConsumer>("named-service");;

            Assert.AreSame(dependency, instance.CtorParameter, "The service implementation parameter constructor should have been invoked with the appropriate argument.");
            var instance2 = container.ResolveNamed <RegisteredServiceConsumer>("named-service");;

            Assert.AreNotSame(instance, instance2, "The lifetime was not set on the registration.");
        }
        public void RegisterTypeRegistration_Default_WithEnumerationParameter()
        {
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable <ISampleService>(itemNames)));

            registration.IsDefault = true;
            registration.Lifetime  = TypeRegistrationLifetime.Transient;

            var builder = new ContainerBuilder();

            builder.RegisterTypeRegistration(registration);
            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 RegisterTypeRegistration_Default_WithEnumerationParameter()
        {
            var itemNames = new string[]
            {
                "first",
                "second",
                "third"
            };
            var registration = new TypeRegistration <RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.ResolvedEnumerable <ISampleService>(itemNames)));

            registration.IsDefault = true;
            registration.Lifetime  = TypeRegistrationLifetime.Transient;

            var builder = new ContainerBuilder();

            builder.RegisterTypeRegistration(registration);
            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.");
        }
        public void RegisterTypeRegistration_Named_WithParameters()
        {
            var registration = new TypeRegistration<RegisteredServiceConsumer>(() => new RegisteredServiceConsumer(EntLibContainer.Resolved<ISampleService>()));
            registration.Name = "named-service";
            registration.Lifetime = TypeRegistrationLifetime.Transient;

            var dependency = new SampleServiceImpl();
            var builder = new ContainerBuilder();
            builder.RegisterTypeRegistration(registration);
            builder.RegisterInstance(dependency).As<ISampleService>();
            var container = builder.Build();

            var instance = container.ResolveNamed<RegisteredServiceConsumer>("named-service"); ;
            Assert.AreSame(dependency, instance.CtorParameter, "The service implementation parameter constructor should have been invoked with the appropriate argument.");
            var instance2 = container.ResolveNamed<RegisteredServiceConsumer>("named-service"); ;
            Assert.AreNotSame(instance, instance2, "The lifetime was not set on the registration.");
        }