コード例 #1
0
        /// <summary>
        /// Returns the <see cref="TypeRegistration"/> container configuration model to register <see cref="ExceptionPolicyEntry"/> items with the container.
        /// </summary>
        /// <param name="namePrefix"></param>
        /// <returns>A <see cref="TypeRegistration"/></returns>
        public TypeRegistration GetRegistration(string namePrefix)
        {
            string registrationName = BuildChildName(namePrefix, Name);

            return(new TypeRegistration <ExceptionPolicyEntry>(
                       () =>
                       new ExceptionPolicyEntry(
                           Type,
                           PostHandlingAction,
                           Container.ResolvedEnumerable <IExceptionHandler>(from hd in ExceptionHandlers select BuildChildName(registrationName, hd.Name)),
                           Container.Resolved <IExceptionHandlingInstrumentationProvider>(namePrefix)))
            {
                Name = registrationName,
                Lifetime = TypeRegistrationLifetime.Transient
            });
        }
コード例 #2
0
 private TypeRegistration CreateLogWriterStructureHolderRegistration()
 {
     return
         (new TypeRegistration <LogWriterStructureHolder>(() =>
                                                          new LogWriterStructureHolder(
                                                              Container.ResolvedEnumerable <ILogFilter>(LogFilters.Select(lfd => lfd.Name)),
                                                              TraceSources.Select(tsd => tsd.Name).ToArray(),
                                                              Container.ResolvedEnumerable <LogSource>(TraceSources.Select(tsd => tsd.Name)),
                                                              Container.Resolved <LogSource>(AllTraceSourceKey),
                                                              Container.Resolved <LogSource>(NoMatchesTraceSourceKey),
                                                              Container.Resolved <LogSource>(ErrorsTraceSourceKey),
                                                              DefaultCategory,
                                                              TracingEnabled,
                                                              LogWarningWhenNoCategoriesMatch,
                                                              RevertImpersonation))
     {
         Lifetime = TypeRegistrationLifetime.Transient,
         IsDefault = true
     });
 }
        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]);
        }
コード例 #4
0
        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_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.");
        }