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;
}
}