/// <summary> /// Adds a new mapping for the supplied <see cref="Expression{Func{T, TProperty}}"/>. /// </summary> /// <typeparam name="T">The <see cref="Type"/>.</typeparam> /// <typeparam name="TProperty">The <see cref="Type"/> of the property.</typeparam> /// <param name="parserContext">The <see cref="IParserContext"/>.</param> /// <param name="source">The source object on which the <see cref="Expression{Func{T, TProperty}}"/> operates.</param> /// <param name="propertySelector">A <see cref="Expression"/> which selects the property which is used for the mapping.</param> /// <param name="parameterName">The parameter name which identifies the parameter in the query string.</param> /// <param name="valueProcessor">An instance of the <see cref="IValueProcessor"/> class.</param> /// <exception cref="ArgumentNullException">The value of '<paramref name="parserContext"/>', '<paramref name="source"/>', '<paramref name="propertySelector"/>', '<paramref name="parameterName"/>' and '<paramref name="valueProcessor"/>' cannot be null. </exception> public static void AddPropertyMapping <T, TProperty>([NotNull] this IParserContext parserContext, T source, [NotNull] Expression <Func <T, TProperty> > propertySelector, [NotNull] String parameterName, [NotNull] IValueProcessor valueProcessor) { if (parserContext == null) { throw new ArgumentNullException(nameof(parserContext)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } if (propertySelector == null) { throw new ArgumentNullException(nameof(propertySelector)); } if (String.IsNullOrEmpty(parameterName)) { throw new ArgumentNullException(nameof(parameterName)); } if (valueProcessor == null) { throw new ArgumentNullException(nameof(valueProcessor)); } parserContext.AddPropertyMapping(source, propertySelector, parameterName, valueProcessor); }
/// <summary> /// Imports the <see cref="IParserContext"/>-Mappings for the <see cref="Type"/> of the supplied sources. /// </summary> /// <param name="parserContext">The <see cref="IParserContext"/> to fill with mappings.</param> /// <param name="sourceObjects">A list of source objects which <see cref="Type"/>`s are used to construct the <see cref="LambdaExpression"/>.</param> /// <exception cref="ArgumentNullException">The value of '<paramref name="parserContext"/>' and '<paramref name="sourceObjects"/>' cannot be null. </exception> /// <exception cref="InvalidOperationException">No element satisfies the condition in <paramref name="predicate" />.-or-More than one element satisfies the condition in <paramref name="predicate" />.-or-The source sequence is empty.</exception> /// <exception cref="ArgumentException">The supplied property path does not contain any parts.</exception> /// <exception cref="ConstructedPropertyPathDoesNotContainAnyPartsException">Thee supplied property path '<paramref name="propertyPath"/>' does not contain any constructible parts for Type '<paramref name="sourceType"/>'. </exception> public static void ImportConfiguration([NotNull] this IParserContext parserContext, [NotNull] params Object[] sourceObjects) { if (parserContext == null) { throw new ArgumentNullException(nameof(parserContext)); } if (sourceObjects == null) { throw new ArgumentNullException(nameof(sourceObjects)); } var configuration = QueryfyConfigurationSection.GetXmlConfiguration(); if (configuration == null) { throw new ConfigurationErrorsException(); } if (configuration.ParserConfiguration != null && configuration.ParserConfiguration.TypeMappings != null) { foreach (var source in sourceObjects) { var closuredSource = source; var parserTypeMappings = configuration.ParserConfiguration.TypeMappings.Single(m => m.SourceType == closuredSource.GetType()); foreach (var propertyMapping in parserTypeMappings.PropertyMappings) { var lambda = LambdaExpressionDeserializer.Deserialize(closuredSource.GetType(), propertyMapping.PropertyPath); var propertyType = lambda.GetPropertyInfo(); var valueProcessor = parserContext.ValueProcessorFactory.GetUrlValueProcessor(propertyType.PropertyType, propertyMapping.ValueProcessorType); parserContext.AddPropertyMapping(closuredSource, lambda, propertyMapping.ParameterName, valueProcessor); } } } }
/// <summary> /// Adds a new mapping for the supplied <see cref="Expression{Func{T, TProperty}}"/>. /// </summary> /// <param name="parserContext">The <see cref="IParserContext"/>.</param> /// <typeparam name="T">The <see cref="Type"/>.</typeparam> /// <typeparam name="TProperty">The <see cref="Type"/> of the property.</typeparam> /// <param name="source">The source object on which the <see cref="Expression{Func{T, TProperty}}"/> operates.</param> /// <param name="propertySelector">A <see cref="Expression{Func{T, TProperty}}"/> which selects the property which is used for the mapping.</param> /// <exception cref="ArgumentNullException">The value of '<paramref name="parserContext"/>', '<paramref name="source"/>' and '<paramref name="propertySelector"/>' cannot be null. </exception> public static void AddPropertyMapping <T, TProperty>([NotNull] this IParserContext parserContext, [NotNull] T source, [NotNull] Expression <Func <T, TProperty> > propertySelector) { if (parserContext == null) { throw new ArgumentNullException(nameof(parserContext)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } if (propertySelector == null) { throw new ArgumentNullException(nameof(propertySelector)); } parserContext.AddPropertyMapping(source, propertySelector); }