public void SelectConstructorWith2Parameters()
        {
            ConstructorInfo ctor = typeof(ClassWithConstructorParameters).GetMatchingConstructor(Types(typeof(int), typeof(string)));

            var policy = new SpecifiedConstructorSelectorPolicy(ctor,
                new InjectionParameterValue[]
                {
                    new InjectionParameter<int>(37),
                    new InjectionParameter<string>("abc")
                });

            var builderContext = new BuilderContextMock(new NamedTypeBuildKey(typeof(ClassWithConstructorParameters)));

            SelectedConstructor selectedCtor = policy.SelectConstructor(builderContext, builderContext.PersistentPolicies);

            Assert.Equal(ctor, selectedCtor.Constructor);
            Assert.Equal(2, selectedCtor.GetParameterResolvers().Length);

            var resolvers = selectedCtor.GetParameterResolvers();
            Assert.Equal(2, resolvers.Length);
            foreach (var resolverPolicy in resolvers)
            {
                AssertPolicyIsCorrect(resolverPolicy);
            }
        }
        public void CanSelectConcreteConstructorGivenGenericConstructor()
        {
            ConstructorInfo ctor = typeof(LoggingCommand<>).GetTypeInfo().DeclaredConstructors.ElementAt(0);
            var policy = new SpecifiedConstructorSelectorPolicy(
                ctor,
                new InjectionParameterValue[]
                {
                    new ResolvedParameter(typeof(ICommand<>), "concrete")
                });

            var ctx = new BuilderContextMock
                {
                    BuildKey = new NamedTypeBuildKey(typeof(LoggingCommand<User>))
                };

            SelectedConstructor result = policy.SelectConstructor(ctx, new PolicyList());

            ConstructorInfo expectedCtor = typeof(LoggingCommand<User>).GetMatchingConstructor(Types(typeof(ICommand<User>)));
            Assert.Same(expectedCtor, result.Constructor);
        }
Example #3
0
        public void CanSelectConcreteConstructorGivenGenericConstructor()
        {
            ConstructorInfo ctor = typeof(LoggingCommand <>).GetConstructors()[0];
            SpecifiedConstructorSelectorPolicy policy = new SpecifiedConstructorSelectorPolicy(
                ctor,
                new InjectionParameterValue[]
            {
                new ResolvedParameter(typeof(ICommand <>), "concrete")
            });

            BuilderContextMock ctx = new BuilderContextMock();

            ctx.BuildKey = typeof(LoggingCommand <User>);

            SelectedConstructor result = policy.SelectConstructor(ctx);

            ConstructorInfo expectedCtor = typeof(LoggingCommand <User>).GetConstructor(Types(typeof(ICommand <User>)));

            Assert.AreSame(expectedCtor, result.Constructor);
        }
Example #4
0
        public void CanSelectConcreteConstructorGivenGenericConstructor()
        {
            ConstructorInfo ctor   = typeof(LoggingCommand <>).GetTypeInfo().DeclaredConstructors.ElementAt(0);
            var             policy = new SpecifiedConstructorSelectorPolicy(
                ctor,
                new InjectionParameterValue[]
            {
                new ResolvedParameter(typeof(ICommand <>), "concrete")
            });

            var ctx = new BuilderContextMock
            {
                BuildKey = new NamedTypeBuildKey(typeof(LoggingCommand <User>))
            };

            SelectedConstructor result = policy.SelectConstructor(ctx, new PolicyList());

            ConstructorInfo expectedCtor = typeof(LoggingCommand <User>).GetMatchingConstructor(Types(typeof(ICommand <User>)));

            Assert.Same(expectedCtor, result.Constructor);
        }
Example #5
0
        public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies)
        {
            Guard.AssertNotNull(implementationType, "implementationType");
            Guard.AssertNotNull(policies, "policies");

            ConstructorInfo ctor;

            InjectionParameterValue[] parameterValues;

            if (this.parameters.Count == 0)
            {
                ctor = implementationType.GetConstructor(Type.EmptyTypes);

                parameterValues = new InjectionParameterValue[0];
            }
            else
            {
                NamedTypeBuildKey key = new NamedTypeBuildKey(implementationType, name);

                IParameterMatchingConventionsPolicy conventions = policies.Get <IParameterMatchingConventionsPolicy>(key);

                if (conventions == null)
                {
                    conventions = new DefaultMatchingConventionsPolicy();

                    policies.SetDefault <IParameterMatchingConventionsPolicy>(conventions);
                }

                ctor = FindConstructor(implementationType, this.parameters, conventions);

                parameterValues = GetParameterValues(ctor, this.parameters, conventions);
            }

            IConstructorSelectorPolicy policy = new SpecifiedConstructorSelectorPolicy(ctor, parameterValues);

            NamedTypeBuildKey buildKey = new NamedTypeBuildKey(implementationType, name);

            policies.Set <IConstructorSelectorPolicy>(policy, buildKey);
        }
        public void SelectConstructorWithNoParameters()
        {
            ConstructorInfo ctor = typeof(ClassWithSimpleConstructor).GetMatchingConstructor(new Type[0]);

            var policy = new SpecifiedConstructorSelectorPolicy(ctor, new InjectionParameterValue[0]);
            var builderContext = new BuilderContextMock(new NamedTypeBuildKey(typeof(ClassWithSimpleConstructor)));

            SelectedConstructor selectedCtor = policy.SelectConstructor(builderContext, new PolicyList());

            Assert.Equal(ctor, selectedCtor.Constructor);
            Assert.Equal(0, selectedCtor.GetParameterResolvers().Length);
        }