lwl1569  

教程的方法:(fields多个参数的数据解析和排序放到一起,参数传递直接传string)
0. 参数传递(从URL到代码查询):参考解析URL中的批量信息中最后的方法

    public class TouristRouteResourceParamaters
    {
        public string Fields { get; set; }
    }
  1. 方法实现
    1.1 字典映射,参考字典映射
    1.2 数据塑形方法实现
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace FakeXiecheng.API.Helper
{
    public static class ObjectExtensions
    {
        public static ExpandoObject ShapeData<TSource>(this TSource source, string fields)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            ExpandoObject dataShapedObject = new ExpandoObject();

            if (string.IsNullOrWhiteSpace(fields))
            {
                // all public properties should be in the ExpandoObject 
                PropertyInfo[] propertyInfos = typeof(TSource).GetProperties(BindingFlags.IgnoreCase | BindingFlags.Public |BindingFlags.Instance);

                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    // get the value of the property on the source object
                    object propertyValue = propertyInfo.GetValue(source);

                    // add the field to the ExpandoObject
                    ((IDictionary<string, object>)dataShapedObject).Add(propertyInfo.Name, propertyValue);
                }

                return dataShapedObject;
            }

            // the field are separated by ",", so we split it.
            string[] fieldsAfterSplit = fields.Split(',');

            foreach (string field in fieldsAfterSplit)
            {
                // trim each field, as it might contain leading 
                // or trailing spaces. Can't trim the var in foreach,
                // so use another var.
                string propertyName = field.Trim();

                // use reflection to get the property on the source object
                // we need to include public and instance, b/c specifying a 
                // binding flag overwrites the already-existing binding flags.
                PropertyInfo propertyInfo = typeof(TSource).GetProperty(propertyName,
                    BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                if (propertyInfo == null)
                {
                    throw new Exception($"Property {propertyName} wasn't found " +
                        $"on {typeof(TSource)}");
                }

                // get the value of the property on the source object
                object propertyValue = propertyInfo.GetValue(source);

                // add the field to the ExpandoObject
                ((IDictionary<string, object>)dataShapedObject).Add(propertyInfo.Name, propertyValue);
            }

            // return the list
            return dataShapedObject;
        }
    }
}
  1. 总结:从上面的代码中,可以发现,数据塑形与排序不同,是没有直接用到字典映射的。这与本系统的字典映射有关,也与字典映射本职有关。
    字典映射的作用是将对象A与对象B映射起来,本系统的映射是string To List。string与前端交互,与数据库交互的数据是List,他们之间是一对一或多的关系,借助字典映射翻译。
    下面的思维导图是本系统[FromeQuery]请求的处理逻辑

    所以塑形不需要字典映射,排序需要字典映射,理论上,筛选也需要字典映射,但是显然实例并没有做到,之所以没有出错,是因为映射是一对一,没有一对多。
posted on 2021-09-29 15:16  lwl1569  阅读(62)  评论(0编辑  收藏  举报