示例#1
0
        /// <summary>
        /// Gets a mapping for properties from metadata fields on <see cref="ContentData"/> to an instance of T.
        /// </summary>
        /// <typeparam name="T">The type being mapped to.</typeparam>
        /// <returns>An <see cref="Action"/> that maps metadata fields onto an instance of type T.</returns>
        public static Action <ContentData, T> GetMapping <T>() where T : new()
        {
            var properties       = typeof(T).GetProperties();
            var propertyMappings = new List <Action <ContentData, T> >();

            foreach (var propertyInfo in properties)
            {
                var attribute = propertyInfo.GetCustomAttribute <MetadataAttribute>();
                if (attribute == null)
                {
                    continue;
                }
                if (string.IsNullOrWhiteSpace(attribute.FieldName))
                {
                    continue;
                }

                if (StringMapper.IsMappable(propertyInfo.PropertyType))
                {
                    propertyMappings.Add(GetPrimitiveMapping <T>(propertyInfo, attribute.FieldName));
                }
                else if (StringMapper.IsMappableEnumerable(propertyInfo.PropertyType))
                {
                    propertyMappings.Add(GetEnumerableMapping <T>(propertyInfo, attribute.FieldName));
                }
            }

            return((contentData, t) => propertyMappings.ForEach(mapping => mapping(contentData, t)));
        }
        /// <summary>
        /// Gets a mapping from all mapped Smart Form XML field values to the properties on an instance of T.
        /// </summary>
        /// <typeparam name="T">The type being mapped to.</typeparam>
        /// <returns>An <see cref="Action"/> that maps the field values from the Smart Form XML to the instance of T.</returns>
        public static Action <XNode, T> GetMapping <T>() where T : new()
        {
            var properties       = typeof(T).GetProperties();
            var propertyMappings = new List <Action <XNode, T> >();

            foreach (var propertyInfo in properties)
            {
                var attribute = propertyInfo.GetCustomAttribute <SmartFormFieldValueAttribute>();
                if (attribute == null)
                {
                    continue;
                }
                if (string.IsNullOrWhiteSpace(attribute.Xpath))
                {
                    continue;
                }

                if (StringMapper.IsMappable(propertyInfo.PropertyType))
                {
                    propertyMappings.Add(GetSingleMapping <T>(propertyInfo, attribute.Xpath));
                }
                else if (StringMapper.IsMappableEnumerable(propertyInfo.PropertyType))
                {
                    propertyMappings.Add(GetEnumerableMapping <T>(propertyInfo, attribute.Xpath));
                }
            }

            return((xml, t) => propertyMappings.ForEach(mapping => mapping(xml, t)));
        }
示例#3
0
        /// <summary>
        /// Gets a mapping from a metadata field on <see cref="ContentData"/> to a property on an instance of T.
        /// </summary>
        /// <typeparam name="T">The type that is being mapped to.</typeparam>
        /// <param name="propertyInfo">The property on T.</param>
        /// <param name="fieldName">The name of the metadata field to map.</param>
        /// <returns>An <see cref="Action"/> that maps a single metadata field on <see cref="ContentData"/> to a single property on an instance of T.</returns>
        private static Action <ContentData, T> GetPrimitiveMapping <T>(PropertyInfo propertyInfo, string fieldName) where T : new()
        {
            var mapToPropertyType = StringMapper.GetMapping(propertyInfo.PropertyType);
            var setProperty       = ExpressionUtil.GetPropertySetter <T>(propertyInfo);

            return((contentData, t) =>
            {
                var metadata = GetMetadata(contentData, fieldName);
                var value = mapToPropertyType(metadata.Text);

                setProperty(t, value);
            });
        }
示例#4
0
        /// <summary>
        /// Gets a mapping from a metadata field with multiple values on <see cref="ContentData"/> to a property on an instance of T.
        /// </summary>
        /// <typeparam name="T">The type that is being mapped to.</typeparam>
        /// <param name="propertyInfo">The property on T.</param>
        /// <param name="fieldName">The name of the metadata field with multiple values to map.</param>
        /// <returns>An <see cref="Action"/> that maps a single metadata field on <see cref="ContentData"/> to a single property on an instance of T.</returns>
        private static Action <ContentData, T> GetEnumerableMapping <T>(PropertyInfo propertyInfo, string fieldName) where T : new()
        {
            var mapToPropertyType = StringMapper.GetEnumerableMapping(propertyInfo.PropertyType);
            var setProperty       = ExpressionUtil.GetPropertySetter <T>(propertyInfo);

            return((contentData, t) =>
            {
                var metadata = GetMetadata(contentData, fieldName);
                var rawValues = metadata.Text.Split(metadata.Separator[0]);
                var values = mapToPropertyType(rawValues);

                setProperty(t, values);
            });
        }
        /// <summary>
        /// Gets a mapping from multiple matching elements Smart Form XML elements to a property on an instance of T.
        /// </summary>
        /// <typeparam name="T">The type being mapped to.</typeparam>
        /// <param name="propertyInfo">The property on T being mapped to.</param>
        /// <param name="xpath">The xpath for the field values being mapped.</param>
        /// <returns>An <see cref="Action"/> that maps an multiple matching fields from the Smart Form XML to a property on an instance of T.</returns>
        private static Action <XNode, T> GetEnumerableMapping <T>(PropertyInfo propertyInfo, string xpath) where T : new()
        {
            var mapToPropertyType = StringMapper.GetEnumerableMapping(propertyInfo.PropertyType);
            var setProperty       = ExpressionUtil.GetPropertySetter <T>(propertyInfo);

            return((xml, t) =>
            {
                var elements = xml.XPathSelectElements(xpath).ToList();
                if (!elements.Any())
                {
                    return;
                }

                var rawValues = elements.Select(GetXmlInnerText).ToList();
                var values = mapToPropertyType(rawValues);

                setProperty(t, values);
            });
        }
        /// <summary>
        /// Gets a mapping from a single Smart Form XML element to a property on an instance of T.
        /// </summary>
        /// <typeparam name="T">The type being mapped to.</typeparam>
        /// <param name="propertyInfo">The property on T being mapped to.</param>
        /// <param name="xpath">The xpath for the field values being mapped.</param>
        /// <returns>An <see cref="Action"/> that maps a field value from the Smart Form XML to a property on an instance of T.</returns>
        private static Action <XNode, T> GetSingleMapping <T>(PropertyInfo propertyInfo, string xpath) where T : new()
        {
            var mapToPropertyType = StringMapper.GetMapping(propertyInfo.PropertyType);
            var setProperty       = ExpressionUtil.GetPropertySetter <T>(propertyInfo);

            return((xml, t) =>
            {
                var element = xml.XPathSelectElement(xpath);
                if (element == null)
                {
                    return;
                }

                var text = GetXmlInnerText(element);
                var value = mapToPropertyType(text);

                setProperty(t, value);
            });
        }
        private static Action <XNode, T> GetPropertyMapping <T>(PropertyInfo propertyInfo, string xpath) where T : new()
        {
            var mapToPropertyType = StringMapper.GetMapping(propertyInfo.PropertyType);
            var setProperty       = ExpressionUtil.GetPropertySetter <T>(propertyInfo);

            return((xml, t) =>
            {
                var attrResult = (IEnumerable)xml.XPathEvaluate(xpath);
                var attr = attrResult.Cast <XAttribute>().FirstOrDefault();

                if (attr == null)
                {
                    return;
                }

                var value = mapToPropertyType(attr.Value);

                setProperty(t, value);
            });
        }