当前位置:网站首页>ASP.NET CORE Study07
ASP.NET CORE Study07
2022-06-28 12:19:00 【人类群星闪耀时】
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;
}
}
边栏推荐
- In less than an hour, apple destroyed 15 startups
- How to get a generic type
- Truly understand triode beginner level chapter (Classic) "suggestions collection"
- [Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
- Two writing methods of JNI function
- 杰理之wif 干扰蓝牙【篇】
- Vivo手机的权限管理
- UGUI使用小技巧(六)Unity实现字符串竖行显示
- Leetcode 48. 旋转图像(可以,已解决)
- 【Unity编辑器扩展基础】、EditorGUILayout (一)
猜你喜欢

pwn入门(1)二进制基础

不到一小时,苹果摧毁了15家初创公司

Custom title bar view

深度学习又有新坑了!悉尼大学提出全新跨模态任务,用文本指导图像进行抠图...

【vi/vim】基本使用及命令汇总

已知两个点和中间一个比例的点,求该点坐标

开源项目维权成功案例: spug 开源运维平台成功维权

多维度监控:智能监控的数据基础

Source code analysis of ArrayList

Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
随机推荐
Asynctask experience summary
开源项目维权成功案例: spug 开源运维平台成功维权
Self use demo of basic component integration of fluent
Three ways to implement LRU cache (recommended Collection)
【C语言】二叉树的实现及三种遍历
期货开户有门槛吗,如何网上安全的开通期货账户
[unity Editor Extension Foundation], editorguilayout (I)
内部振荡器、无源晶振、有源晶振有什么区别?
从SimpleKV到Redis
PrecomputedTextCompat用法及原理
[C language] use of file read / write function
设置Canvas的 overrideSorting不生效
Leetcode 48. 旋转图像(可以,已解决)
Prefix and (2D)
What are the common modes of financial products in 2022?
【C语言】结构体嵌套二级指针的使用
Unity加载设置:Application.backgroundLoadingPriority
Ugui uses tips (VI) unity to realize vertical line display of string
Source code analysis of ArrayList
【C语言】如何很好的实现复数类型