2021-03-26 20:09:52 +04:00
|
|
|
|
using ModelTools.Attributes;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace ModelTools.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Маппер сущностей
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Mapper
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Преобразование из одного класса в другой
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="From"></typeparam>
|
|
|
|
|
/// <typeparam name="To"></typeparam>
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static To MapToClass<From, To>(From obj) where To : class => FillObject(obj, (To)Activator.CreateInstance(typeof(To)));
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Преобразование из одного класса в другой
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="From"></typeparam>
|
|
|
|
|
/// <typeparam name="To"></typeparam>
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
/// <param name="newObject"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static To MapToClass<From, To>(From obj, To newObject) where To : class => FillObject(obj, newObject);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Заполнение объекта
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="From"></typeparam>
|
|
|
|
|
/// <typeparam name="To"></typeparam>
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
/// <param name="newObject"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private static To FillObject<From, To>(From obj, To newObject)
|
|
|
|
|
where To : class
|
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newObject == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var typeFrom = typeof(From);
|
|
|
|
|
var typeTo = typeof(To);
|
|
|
|
|
var properties = typeTo.GetProperties();
|
|
|
|
|
foreach (var property in properties)
|
|
|
|
|
{
|
|
|
|
|
var customAttribute = property.GetCustomAttribute<MapConfigurationAttribute>();
|
|
|
|
|
if (customAttribute != null)
|
|
|
|
|
{
|
|
|
|
|
object value = obj;
|
|
|
|
|
if (customAttribute.IsDifficle)
|
|
|
|
|
{
|
|
|
|
|
var props = customAttribute.PropertyNameFromModel.Split('.');
|
|
|
|
|
foreach (var prop in props)
|
|
|
|
|
{
|
|
|
|
|
var bindingProperty = value.GetType().GetProperty(prop);
|
|
|
|
|
if (bindingProperty != null)
|
|
|
|
|
{
|
2021-03-27 23:50:29 +04:00
|
|
|
|
value = bindingProperty.GetValue(value);
|
2021-03-26 20:09:52 +04:00
|
|
|
|
if (value is null)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
value = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var bindingProperty = typeFrom.GetProperty(customAttribute.PropertyNameFromModel);
|
|
|
|
|
if (bindingProperty != null)
|
|
|
|
|
{
|
|
|
|
|
value = bindingProperty.GetValue(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (value is null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
property.SetValue(newObject, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newObject;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|