ASP.NET Core 关系映射

举报
Echo_Wish 发表于 2022/08/17 09:14:23 2022/08/17
【摘要】 Get新知识:IPropertyMappingServicePropertyMappingService 服务提供类 的提取接口。 public interface IPropertyMappingService { Dictionary<string, PropertyMappingValue> GetPropertyMapping<TSource, TDest...

Get新知识:

IPropertyMappingService
PropertyMappingService 服务提供类 的提取接口。

    public interface IPropertyMappingService
    {
        Dictionary<string, PropertyMappingValue> GetPropertyMapping<TSource, TDestination>();
        bool ValidMappingExixtsFor<TSource, TDestination>(string fields);
    }

IPropertyMapping
PropertyMapping 类 的提取接口,便于使用,只需要调用接口,asp.net core会注入一个实例供于使用。

    public interface IPropertyMapping
    {
    }

IPropertyCheckService
PropertyCheckService 服务提供类 的提取接口

    public interface IPropertyCheckService
    {
        bool TypeHasProperities<T>(string fields);
    }

PropertyMappingValue
提供DTO的字段值的存放,以及排序方式,DestinationProperties 存放DTO类的全部字段值名。

    public class PropertyMappingValue
    {
        public IEnumerable<String> DestinationProperties { get; set; }
        public bool Revert { get; set; }
        public PropertyMappingValue(IEnumerable<string> destinationProperties, bool revert = false)
        {
            DestinationProperties = destinationProperties ?? throw new ArgumentNullException(nameof(destinationProperties));
            Revert = revert;
        }
    }

PropertyMapping
用于 实体类的字段 和 DTO类的字段 的映射关系的存放,即 MappingDictionary 属性, string 是实体类的字段值名,PropertyMappingValue 是 DTO类的字段值名。

    public class PropertyMapping<TSource, TDestination> : IPropertyMapping
    {
        public Dictionary<string, PropertyMappingValue> MappingDictionary { get;private set; }

        public PropertyMapping(Dictionary<string, PropertyMappingValue> mappingDictionary)
        {
            MappingDictionary = mappingDictionary ?? throw new ArgumentNullException(nameof(mappingDictionary));
        }

    }

PropertyMappingService
服务提供类,提供 获取属性映射关系的方法

    public class PropertyMappingService : IPropertyMappingService
    {
    	// 只读字段,映射关系的配置,就是说明需要映射的两个类之间的字段对应关系
        // 这里是 Company 和 CompanyDto 类之间的具体的字段对应关系的配置
        private readonly Dictionary<string, PropertyMappingValue> _companyProperMapping =
            new Dictionary<string, PropertyMappingValue>(StringComparer.OrdinalIgnoreCase)
            {
                { "Id", new PropertyMappingValue(new List<String>{ "Id"})},
                { "Companyame", new PropertyMappingValue(new List<String>{ "Name"})},
                { "Country", new PropertyMappingValue(new List<String>{ "Country"})},
                { "Industry", new PropertyMappingValue(new List<String>{ "Industry"})},
                { "Product", new PropertyMappingValue(new List<String>{ "Product"})},
                { "Introduction", new PropertyMappingValue(new List<String>{ "Introduction"})}
            };
        // 这里是 Employee 和 EmployeeDto 类之间的具体的字段对应关系的配置
        private readonly Dictionary<string, PropertyMappingValue> _employeeProperMapping =
            new Dictionary<string, PropertyMappingValue>(StringComparer.OrdinalIgnoreCase)
            {
                { "Id", new PropertyMappingValue(new List<String>{ "Id"})},
                { "CompanyId", new PropertyMappingValue(new List<String>{ "CompanyId"})},
                { "EmployeeNo", new PropertyMappingValue(new List<String>{ "EmployeeNo"})},
                { "Name", new PropertyMappingValue(new List<String>{ "First", "LastName"})},
                { "GenderDisplay", new PropertyMappingValue(new List<String>{ "Gender"})},
                { "Age", new PropertyMappingValue(new List<String>{ "DataOfBirth"}, true)}
            };
		// 存放PropertyMapping 的集合
        private readonly IList<IPropertyMapping> _propertyMappings = new List<IPropertyMapping>();
        // 构造函数,
        public PropertyMappingService()
        {
            _propertyMappings.Add(new PropertyMapping<EmployeeDTO, Employee>(_employeeProperMapping));
            _propertyMappings.Add(new PropertyMapping<CompanyDTO, Company>(_companyProperMapping));
        }

        public Dictionary<string, PropertyMappingValue> GetPropertyMapping<TSource, TDestination>()
        {
        	// 从集合等序列中筛选 指定类型的元素,这里是筛选 PropertyMapping 泛型类型的元素
            // 返回存放符合条件元素的集合
            var matchingMapping = _propertyMappings.OfType<PropertyMapping<TSource, TDestination>>();
            if (matchingMapping.Count() == 1)
            {
                return matchingMapping.First().MappingDictionary;
            }
            throw new Exception($"无法找到唯一的映射关系{typeof(TSource)}和{typeof(TDestination)}");
        }

        public bool ValidMappingExixtsFor<TSource, TDestination>(string fields)
        {
            if (string.IsNullOrWhiteSpace(fields))
            {
                return true;
            }
            var propertyMapping = GetPropertyMapping<TSource, TDestination>();
            var fieldAfterSplit = fields.Split(',');
            foreach (var field in fieldAfterSplit)
            {
                var trimmedFields = field.Trim();
                var indexOfFirstSpace = trimmedFields.IndexOf(" ");
                var propertyName = indexOfFirstSpace == -1 ? trimmedFields : trimmedFields.Remove(indexOfFirstSpace);
                if (!propertyMapping.ContainsKey(propertyName))
                {
                    return false;
                }
            }
            return true;
        }
    }
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。