Beispiel #1
0
        public void WhenMultipleConstructorsWithTheSameLengthResolvable_ExceptionIsThrown()
        {
            var constructors = GetBindingsForAllConstructorsOf <TwoConstructors>();
            var target       = new MostParametersConstructorSelector();

            Assert.Throws <DependencyResolutionException>(() => target.SelectConstructorBinding(constructors));
        }
Beispiel #2
0
        public void ChoosesCorrectConstructor()
        {
            var constructors = GetBindingsForAllConstructorsOf <ThreeConstructors>();
            var target       = new MostParametersConstructorSelector();

            var chosen = target.SelectConstructorBinding(constructors, Enumerable.Empty <Parameter>());

            Assert.Equal(2, chosen.TargetConstructor.GetParameters().Length);
        }
Beispiel #3
0
        public void IgnoresInvalidConstructor()
        {
            var constructors = GetBindingsForAllConstructorsOf <OneValidConstructorOneInvalid>();
            var target       = new MostParametersConstructorSelector();

            var chosen = target.SelectConstructorBinding(constructors, Enumerable.Empty <Parameter>());

            Assert.Single(chosen.TargetConstructor.GetParameters());
        }
Beispiel #4
0
        public void ChoosesCorrectConstructor()
        {
            var constructors = GetBindingsForAllConstructorsOf <ThreeConstructors>();
            var target       = new MostParametersConstructorSelector();

            var chosen = target.SelectConstructorBinding(constructors);

            Assert.IsNotNull(chosen);
            Assert.AreEqual(2, chosen.TargetConstructor.GetParameters().Length);
        }
Beispiel #5
0
        public void DoesNotAcceptEmptyBindings()
        {
            var target = new MostParametersConstructorSelector();

            Assert.Throws <ArgumentOutOfRangeException>(() => target.SelectConstructorBinding(new ConstructorParameterBinding[] { }));
        }
Beispiel #6
0
        public void DoesNotAcceptNullBindings()
        {
            var target = new MostParametersConstructorSelector();

            Assert.Throws <ArgumentNullException>(() => target.SelectConstructorBinding(null));
        }
Beispiel #7
0
        public void DoesNotAcceptEmptyBindings()
        {
            var target = new MostParametersConstructorSelector();

            Assert.Throws <ArgumentOutOfRangeException>(() => target.SelectConstructorBinding(new BoundConstructor[] { }, Enumerable.Empty <Parameter>()));
        }
        public void Execute(ResolveRequestContext context, Action <ResolveRequestContext> next)
        {
            next(context);
            if (context.Instance == null || context.Instance.IsProxy())
            {
                return;
            }
            if (!(context.Registration.Activator is ReflectionActivator ||
                  context.Registration.Activator is DelegateActivator ||
                  context.Registration.Activator is InstanceActivator))
            {
                return;
            }
            var limitType = context.Instance.GetType();

            if (!limitType.GetTypeInfo().CanInherited())
            {
                return;
            }
            if (excepts.Any(x => limitType.Name.Matches(x)) || excepts.Any(x => limitType.Namespace.Matches(x)))
            {
                return;
            }
            var services = context.Registration.Services.Select(x => ((IServiceWithType)x).ServiceType).ToList();

            if (!services.All(x => x.GetTypeInfo().CanInherited()) || services.All(x => x.GetTypeInfo().IsNonAspect()))
            {
                return;
            }
            var aspectValidator = new AspectValidatorBuilder(context.Resolve <IAspectConfiguration>()).Build();

            if (services.All(x => !aspectValidator.Validate(x, true)) && !aspectValidator.Validate(limitType, false))
            {
                return;
            }
            var  proxyTypeGenerator = context.Resolve <IProxyTypeGenerator>();
            Type proxyType; object instance;
            var  interfaceType = services.FirstOrDefault(x => x.GetTypeInfo().IsInterface);

            if (interfaceType == null)
            {
                var baseType = services.FirstOrDefault(x => x.GetTypeInfo().IsClass) ?? limitType;
                proxyType = proxyTypeGenerator.CreateClassProxyType(baseType, limitType);

                //Autofac.Core.Activators.Reflection.ReflectionActivator
                var constructorSelector   = new MostParametersConstructorSelector();
                var constructorFinder     = new DefaultConstructorFinder(type => type.GetTypeInfo().DeclaredConstructors.ToArray());
                var availableConstructors = constructorFinder.FindConstructors(proxyType);

                if (availableConstructors.Length == 0)
                {
                    throw new NoConstructorsFoundException(proxyType, $"No constructors on type '{proxyType}' can be found with the constructor finder '{constructorFinder}'.");
                }

                var binders = new ConstructorBinder[availableConstructors.Length];
                for (var idx = 0; idx < availableConstructors.Length; idx++)
                {
                    binders[idx] = new ConstructorBinder(availableConstructors[idx]);
                }

                var allBindings     = GetAllBindings(binders, context, context.Parameters);
                var selectedBinding = constructorSelector.SelectConstructorBinding(allBindings, context.Parameters);
                instance = selectedBinding.Instantiate();
            }
            else
            {
                proxyType = proxyTypeGenerator.CreateInterfaceProxyType(interfaceType, limitType);
                instance  = Activator.CreateInstance(proxyType, new object[] { context.Resolve <IAspectActivatorFactory>(), context.Instance });
            }

            var propertyInjector = context.Resolve <IPropertyInjectorFactory>().Create(instance.GetType());

            propertyInjector.Invoke(instance);
            context.Instance = instance;
        }
Beispiel #9
0
        public void DoesNotAcceptEmptyBindings()
        {
            var target = new MostParametersConstructorSelector();

            target.SelectConstructorBinding(new ConstructorParameterBinding[] { });
        }
Beispiel #10
0
        public void DoesNotAcceptNullBindings()
        {
            var target = new MostParametersConstructorSelector();

            target.SelectConstructorBinding(null);
        }