Пример #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="TOptionCollection">Option 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">Option provider</param>
        /// <returns>ConfigurationPropertyWithOptionBuilder for selected property</returns>
        public static ConfigurationPropertyWithOptionBuilder PropertyWithMultipleOptions <TModel, TOption, TOptionCollection, TConfigurationSet>(this IModelWithProperties <TModel> source, Expression <Func <TModel, TOptionCollection> > expression, IConfigurationSetOptionProvider <TConfigurationSet, TOption> optionProvider) where TConfigurationSet : ConfigurationSet where TOption : new() where TOptionCollection : ICollection <TOption>
        {
            var propertyName = ExpressionHelper.GetPropertyNameFromExpression(expression);
            var model        = new ConfigurationPropertyWithMultipleOptionsModelDefinition <TConfigurationSet, TOption, TOptionCollection>(optionProvider, propertyName, typeof(TModel));

            source.ConfigurationProperties[propertyName] = model;
            return(new ConfigurationPropertyWithOptionBuilder(model));
        }
        private static ConfigurationPropertyWithOptionBuilder PropertyWithMultipleOptionsInternal <TModel, TOption, TOptionCollection, TOptionProvider>(this IModelWithProperties <TModel> source, Expression <Func <TModel, TOptionCollection> > expression, Func <TOptionProvider, IEnumerable <TOption> > optionProvider, Func <TOption, string> keySelector, Func <TOption, string> displaySelector) where TOptionCollection : ICollection <TOption> where TOptionProvider : class where TOption : new()
        {
            var body  = GetExpressionBody(expression);
            var model = new ConfigurationPropertyWithMultipleOptionsModelDefinition <TOptionCollection, TOption, TOptionProvider>(optionProvider, keySelector, displaySelector, body.Member.Name, typeof(TModel));

            source.ConfigurationProperties[body.Member.Name] = model;
            return(new ConfigurationPropertyWithOptionBuilder(model));
        }
        private object GetPropertyValue(object source, ConfigurationPropertyWithMultipleOptionsModelDefinition propertyModel)
        {
            var collection = propertyModel.GetPropertyValue(source) as IEnumerable ?? new List <object>();
            var result     = new List <string>();

            foreach (var item in collection)
            {
                var itemValue = propertyModel.GetKeyFromObject(item);
                result.Add(itemValue);
            }

            return(result);
        }
        private object GetConfigPropertyValueFromInput(JObject source, ConfigurationPropertyWithMultipleOptionsModelDefinition propertyModel)
        {
            var collectionBuilder = propertyModel.GetCollectionBuilder();
            var optionSet         = optionSetFactory.Build(propertyModel);

            foreach (var key in source.GetValue(propertyModel.ConfigurationPropertyName.ToLowerCamelCase()).Select(s => s.ToObject <string>()))
            {
                object option = null;
                if (optionSet.TryGetValue(key, out option))
                {
                    collectionBuilder.Add(option);
                }
            }
            return(collectionBuilder.Collection);
        }
Пример #5
0
        private void UpdateOptions(object source, ConfigurationPropertyWithMultipleOptionsModelDefinition model)
        {
            var optionSet         = optionSetFactory.Build(model);
            var collectionBuilder = model.GetCollectionBuilder();
            var items             = model.GetPropertyValue(source) as IEnumerable;

            foreach (var item in items ?? Enumerable.Empty <object>())
            {
                if (optionSet.TryGetValue(item, out var actualValue))
                {
                    collectionBuilder.Add(actualValue);
                }
            }

            model.SetPropertyValue(source, collectionBuilder.Collection);
        }