Esempio n. 1
0
        public void a_required_parameter_found_when_name_in_configuration_is_in_lower_case()
        {
            Section section = new Section().FromType<SimpleSection>();
            section.AddParameter(new Parameter
                {
                    Name = "intprop",
                    Required = true.ToString()
                }
                .AddValue(new ParameterValue {Value = "1"}));

            section.AddParameter(new Parameter
                    {
                        Name = "strprop",
                        Required = true.ToString()
                    }
                    .AddValue(new ParameterValue {Value = "str"}))
                ;

            IConfigurationService svc = Configure.With(cfg =>
                {
                    cfg.ThrowIfParameterMemberMissing = true;
                    cfg.AddSection(section);
                }

                );

            Assert.DoesNotThrow(() => svc.GetSection<SimpleSection>());
        }
 private IModelBinder GetModelBinderForSection(Section section, INConfigSettings settings)
 {
     var binder = new ConfigurationHelper()
         .GetConfigurationProperty<Section, IModelBinder>(section, x => x.ModelBinder, settings.ModelBinder,
                                                          x => settings.ModelBinder);
     return binder;
 }
        private void BuildParametersProviders(Section section, INConfigSettings settings, SectionProvider provider, Type sectionType)
        {
            var properties = sectionType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (Parameter parameter in section.Parameters.Values)
            {
                var parameterPropertyInfo = properties.SingleOrDefault(x => x.Name.ToLower() == parameter.Name.ToLower());

                if (parameterPropertyInfo == null && settings.ThrowIfParameterMemberMissing)
                {
                    throw new MemberNotFouldForParameter(parameter);
                }

                this.BuildParameterProvider(settings, provider, parameter, parameterPropertyInfo);
            }
        }
        public ISectionProvider Convert(Section section, INConfigSettings settings)
        {
            try
            {
                Type sectionType = Type.GetType(section.TypeName, true);

                var binder = this.GetModelBinderForSection(section, settings);

                var provider = new SectionProvider { SectionType = sectionType, ModelBinder = binder };

                this.BuildParametersProviders(section, settings, provider, sectionType);

                return provider;
            }
            catch (Exception e)
            {
                throw new SessionToProviderConvertionFailed(section, e);
            }
        }
        public void ConvertSection()
        {
            Section section = new Section
                {
                    TypeName = typeof (SimpleSection).AssemblyQualifiedName,
                    ModelBinder = null,
                };
            var strPropParam = new Parameter
                {
                    Name = "StrProp",
                    Values = new List<ParameterValue>
                        {
                            new ParameterValue{Value = "aa"}
                        },
                };

            var intPropParam = new Parameter
                {
                    Name = "IntProp",
                    Values = new List<ParameterValue>
                        {
                            new ParameterValue
                                {
                                    Value = "11"
                                }
                        },
                };

            section.Parameters.Add(strPropParam.Name, strPropParam);
            section.Parameters.Add(intPropParam.Name, intPropParam);

            var sectionProvider = new SectionToProviderConverter().Convert(section, new NConfigSettings());

            var sectionInstance = sectionProvider.Get(new Dictionary<string, string>());

            Assert.IsInstanceOf<SimpleSection>(sectionInstance);
            Assert.AreEqual(11, ((SimpleSection)sectionInstance).IntProp);
            Assert.AreEqual("aa", ((SimpleSection)sectionInstance).StrProp);
        }
Esempio n. 6
0
        public void TestObjectModelConfiguration()
        {
            IWindsorContainer container = new WindsorContainer()
                .Register(Component.For<IService>().Instance(new ServiceImpl(2)))
                .Register(Component.For<IService>().Instance(new ServiceImpl(1)).Named("1"))
                .Register(Component.For<IService>().Instance(new ServiceImpl(2)).Named("2"));

            Section section = new Section().FromType<ComplexTestSection>()
                .AddParameter(new Parameter().FromExpression<ComplexTestSection, int>(x => x.Num)
                    .AddValue(new ParameterValue{Value = "1"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue{Value = "2"}
                        .WithTextMatchReference("environment", "production")
                        .WithTextMatchReference("appType", "onlineClient")))
                .AddParameter(new Parameter().FromExpression<ComplexTestSection, string>(x => x.Name)
                    .AddValue(new ParameterValue{Value = "name 1"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue{Value = "name 2"}
                        .WithTextMatchReference("environment", "production")
                        .WithTextMatchReference("appType", "onlineClient")))
                .AddParameter(new Parameter().FromExpression<ComplexTestSection, IList<int>>(x => x.Numbers)
                    .AddValue(new ParameterValue{Value = "1"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue{Value = "2"}
                        .WithTextMatchReference("environment", "production")
                        .WithTextMatchReference("appType", "onlineClient"))
                    .AddValue(new ParameterValue{Value = "1"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue{Value = "2"}
                        .WithTextMatchReference("environment", "production")
                        .WithTextMatchReference("appType", "onlineClient")))
                .AddParameter(new Parameter().FromExpression<ComplexTestSection, IEnumerable<int>>(x => x.EnumerableNumbers)
                    .AddValue(new ParameterValue{Value = "1"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue{Value = "2"}
                        .WithTextMatchReference("environment", "production")
                        .WithTextMatchReference("appType", "onlineClient"))
                    .AddValue(new ParameterValue{Value = "1"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue{Value = "2"}
                        .WithTextMatchReference("environment", "production")
                        .WithTextMatchReference("appType", "onlineClient")))
                .AddParameter(new Parameter().FromExpression<ComplexTestSection, TestEnum>(x => x.EnumValue)
                    .AddValue(new ParameterValue{Value = "Value1"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer")))
                .AddParameter(new Parameter().FromExpression<ComplexTestSection, IDictionary<int, string>>(x => x.Dictionary)
                    .AddValue(new ParameterValue{Value = "1:one"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue{Value = "2:two"}
                        .WithTextMatchReference("environment", "production")
                        .WithTextMatchReference("appType", "onlineClient"))
                    .AddValue(new ParameterValue{Value = "3:three"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue{Value = "4:four"}
                        .WithTextMatchReference("environment", "production")
                        .WithTextMatchReference("appType", "onlineClient")))
                .AddParameter(new Parameter().FromExpression<ComplexTestSection, IService>(x => x.SVC).WithTranslator("Windsor")
                    .AddValue(new ParameterValue{Value = string.Empty}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer")))
                .AddParameter(new Parameter().FromExpression<ComplexTestSection, IEnumerable<IService>>(x => x.SVCs).WithTranslator("Windsor")
                    .AddValue(new ParameterValue{Value = "1"}
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                    .AddValue(new ParameterValue { Value = "2" }
                        .WithTextMatchReference("environment", "development")
                        .WithTextMatchReference("appType", "onlineServer"))
                        );

            IConfigurationService svc = Configure.With(cfg=>
                cfg.RuntimeContext((context) =>
                {
                    context.Add("environment", "development");
                    context.Add("appType", "onlineServer");
                })
                .AddWindsorTranslatorProvider(container)
                .AddSection(section)
            );

            var testSection = svc.GetSection<ComplexTestSection>();
            Assert.IsNotNull(testSection);
            Assert.IsNotNull(testSection.Name);
            Assert.IsNotNull(testSection.Num);
            Assert.AreEqual(TestEnum.Value1, testSection.EnumValue);
            Assert.IsNotNull(testSection.Numbers);
            Assert.IsNotNull(testSection.Dictionary);
            Assert.IsNotNull(testSection.EnumerableNumbers);
            Assert.IsTrue(testSection.EnumerableNumbers.Any());
            Assert.IsTrue(testSection.SVCs.Any());
            Assert.AreEqual(2, testSection.SVCs.Count());
            Assert.AreEqual(2, testSection.SVC.Get());
            Assert.AreEqual(1, testSection.SVCs.First().Get());
        }
 public SessionToProviderConvertionFailed(Section section, Exception inner)
     : base(string.Format("Failed converting section {0}, see inner exception for more detailes.", section.TypeName),
         inner)
 {
     Section = section;
 }
Esempio n. 8
0
        private XNode BuildNodeFromSection(Section section)
        {
            var sectionNode = new XElement(SectionName);
            sectionNode.SetAttributeValueOrThrow(TypeNameAttribute, section.TypeName);
            sectionNode.SetAttributeValueIfNotNullOrEmpty(ModelBinderName, section.ModelBinder);

            foreach (var parameter in section.Parameters)
            {
                XNode parameterNode = BuildNodeFromParameter(parameter);
                sectionNode.Add(parameterNode);
            }
            return sectionNode;
        }
Esempio n. 9
0
        private Section BuildSectionFromNode(XElement sectionNode)
        {
            Section section = new Section
                {
                    TypeName = sectionNode.GetAttributeValueOrThrow(TypeNameAttribute),
                    ModelBinder = sectionNode.GetAttributeValueOrNull(ModelBinderName)
                };

            foreach (XElement parameterNode in sectionNode.Elements(ParameterName))
            {
                Parameter parameter = BuildParameterFromNode(parameterNode);
                section.Parameters.Add(parameter.Name, parameter);
            }

            return section;
        }
Esempio n. 10
0
 public ConvertSectionException(Section section, Exception exception)
     : base(string.Format("Failed converting section {0}, see inner exception for more details",section.TypeName), exception)
 {
     Section = section;
 }