Beispiel #1
0
        private static void AddComponentRegistrationToContainer(ContainerBuilder containerBuilder, IEnumerable <ITypeRegistration> customRegistration)
        {
            if (customRegistration?.Any() == false)
            {
                return;
            }
            var fullCtorFinder = new FullConstructorFinder();

            foreach (var item in customRegistration)
            {
                if (item is InstanceTypeRegistration instanceTypeRegistration)
                {
                    containerBuilder.Register(c => instanceTypeRegistration.Value)
                    .As(instanceTypeRegistration.AbstractionTypes.ToArray());
                }
                else if (item is TypeRegistration typeRegistration)
                {
                    containerBuilder.RegisterType(typeRegistration.InstanceType)
                    .As(typeRegistration.AbstractionTypes.ToArray())
                    .FindConstructorsWith(fullCtorFinder);
                }
                else if (item is FactoryRegistration factoryRegistration)
                {
                    containerBuilder.Register(c => factoryRegistration.Factory.Invoke())
                    .As(factoryRegistration.AbstractionTypes.ToArray());
                }
            }
        }
        private static void AddComponentRegistrationToContainer(ContainerBuilder containerBuilder, IEnumerable <ITypeRegistration> customRegistration)
        {
            if (customRegistration?.Any() == false)
            {
                return;
            }
            var fullCtorFinder = new FullConstructorFinder();

            foreach (var item in customRegistration)
            {
                if (item is InstanceTypeRegistration instanceTypeRegistration)
                {
                    AddLifetime(
                        containerBuilder
                        .Register(c => instanceTypeRegistration.Value)
                        .As(instanceTypeRegistration.AbstractionTypes.ToArray()),
                        instanceTypeRegistration.Lifetime);
                }
                else if (item is TypeRegistration typeRegistration)
                {
                    foreach (var serviceType in typeRegistration.AbstractionTypes)
                    {
                        if (serviceType.IsGenericTypeDefinition)
                        {
                            var registration = containerBuilder
                                               .RegisterGeneric(typeRegistration.InstanceType)
                                               .As(serviceType);
                            if (typeRegistration.Mode == TypeResolutionMode.Full)
                            {
                                registration = registration.FindConstructorsWith(new FullConstructorFinder());
                            }
                            AddLifetime(registration, typeRegistration.Lifetime);
                        }
                        else
                        {
                            var registration = containerBuilder
                                               .RegisterType(typeRegistration.InstanceType)
                                               .As(serviceType);
                            if (typeRegistration.Mode == TypeResolutionMode.Full)
                            {
                                registration = registration.FindConstructorsWith(new FullConstructorFinder());
                            }
                            AddLifetime(registration, typeRegistration.Lifetime);
                        }
                    }
                }
                else if (item is FactoryRegistration factoryRegistration)
                {
                    AddLifetime(
                        containerBuilder
                        .Register(c =>
                    {
                        if (factoryRegistration.Factory != null)
                        {
                            return(factoryRegistration.Factory.Invoke());
                        }
                        else if (factoryRegistration.ScopedFactory != null)
                        {
                            return(factoryRegistration.ScopedFactory.Invoke(new AutofacScope(c)));
                        }
                        throw new InvalidOperationException("FactoryRegistration has not been correctly configured (both Factory and ScopedFactory are null).");
                    })
                        .As(factoryRegistration.AbstractionTypes.ToArray()),
                        factoryRegistration.Lifetime);
                }
            }
        }