Exemplo n.º 1
0
        /// <summary>
        /// Gets ConfigurationPropertyModelBuilder for property with multiple option
        /// Overides existing configuration from property
        /// </summary>
        /// <typeparam name = "TModel" > Source model type</typeparam>
        /// <typeparam name = "TOption" > Option type</typeparam>
        /// <typeparam name = "TValue" > Option value type</typeparam>
        /// <typeparam name = "TValueCollection" > Value Collection type</typeparam>
        /// <typeparam name = "TConfigurationSet" > ConfigurationSet to provide available options</typeparam>
        /// <param name = "source" > model with property</param>
        /// <param name = "expression" > property selector</param>
        /// <param name = "optionProvider" > Options provider</param>
        /// <returns>ConfigurationPropertyWithOptionBuilder for selected property</returns>
        public static ConfigurationPropertyWithOptionValueBuilder PropertyWithMultipleOptionValues <TModel, TOption, TValue, TValueCollection, TConfigurationSet>(this IModelWithProperties <TModel> source, Expression <Func <TModel, TValueCollection> > expression, IConfigurationSetOptionValueProvider <TConfigurationSet, TOption, TValue> optionProvider) where TConfigurationSet : ConfigurationSet where TOption : new() where TValueCollection : ICollection <TValue>
        {
            var propertyName = ExpressionHelper.GetPropertyNameFromExpression(expression);
            var model        = new ConfigurationPropertyWithMultipleOptionValuesModelDefinition <TConfigurationSet, TOption, TValue, TValueCollection>(optionProvider, propertyName, typeof(TModel));

            source.ConfigurationProperties[propertyName] = model;
            return(new ConfigurationPropertyWithOptionValueBuilder(model));
        }
        private object GetConfigMultipleOptionPropertyValueFromInput(JObject source, ConfigurationPropertyWithMultipleOptionValuesModelDefinition propertyModel, ConfigurationIdentity configIdentity, IEnumerable <ConfigurationSet> requiredConfigurationSets)
        {
            var collectionBuilder = propertyModel.GetCollectionBuilder();
            var optionSet         = optionSetFactory.Build(propertyModel, configIdentity, requiredConfigurationSets);

            foreach (var key in source.GetValue(propertyModel.ConfigurationPropertyName.ToLowerCamelCase()).Select(s => s.ToObject(propertyModel.PropertyType)))
            {
                if (optionSet.ContainsKey(key))
                {
                    collectionBuilder.Add(key);
                }
            }
            return(collectionBuilder.Collection);
        }
        private void UpdateMultipleOptions(object source, ConfigurationPropertyWithMultipleOptionValuesModelDefinition model, IEnumerable <ConfigurationSet> configurationSets, ConfigurationIdentity configIdentity)
        {
            var optionSet         = optionSetFactory.Build(model, configIdentity, configurationSets);
            var collectionBuilder = model.GetCollectionBuilder();
            var items             = model.GetPropertyValue(source) as IEnumerable;

            foreach (var item in items ?? Enumerable.Empty <object>())
            {
                if (optionSet.ContainsKey(item))
                {
                    collectionBuilder.Add(item);
                }
            }
            model.SetPropertyValue(source, collectionBuilder.Collection);
        }