работа с маппером

This commit is contained in:
kotcheshir73 2022-03-20 19:27:50 +04:00
parent 9c76b680b7
commit 34fe571fdc
15 changed files with 78 additions and 98 deletions

View File

@ -16,11 +16,6 @@ namespace ToolsModule.ManagmentMapping
/// </summary>
public string PropertyNameFromModel { get; set; }
/// <summary>
/// Сложное свойство (свойствое в другом классе-свойстве)
/// </summary>
public bool IsDifficle { get; set; } = false;
/// <summary>
/// Можно копировать поле даже при доступе без прав
/// </summary>

View File

@ -105,80 +105,65 @@ namespace ToolsModule.ManagmentMapping
private static object GetValueFromCustomAttribute(MapConfigurationAttribute customAttribute, Type typeFrom, object obj)
{
object value = obj;
if (customAttribute.IsDifficle)
var props = customAttribute.PropertyNameFromModel.Split('.');
foreach (var prop in props)
{
var props = customAttribute.PropertyNameFromModel.Split('.');
foreach (var prop in props)
{
if (prop == "ToString")
{
value = value.ToString();
break;
}
else if (prop == "Count")
{
value = (value as ICollection)?.Count;
break;
}
else if (prop == "Method")
{
int index = 0;
while (true)
{
if (props[index++] == "Method")
break;
if (index == props.Length)
{
break;
}
}
var methodName = props[index].Split('[')?[0];
if (string.IsNullOrEmpty(methodName))
{
break;
}
var parameters = props[index].Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries)?[1].Split(',');
var objs = new object[parameters.Length];
for (int i = 0; i < parameters.Length; ++i)
{
var type = parameters[i].Split(':')[0];
switch (type)
{
case "Enum":
objs[i] = Enum.Parse(customAttribute.MethodParams[i], parameters[i].Split(':')[1]);
break;
}
}
value = typeFrom.InvokeMember(methodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, value, objs);
break;
}
var bindingProperty = value.GetType().GetProperty(prop);
if (bindingProperty != null)
{
value = bindingProperty.GetValue(value);
if (value is null)
{
break;
}
}
else
{
value = null;
break;
}
}
}
else
{
if (customAttribute.PropertyNameFromModel == "ToString")
if (prop == "ToString")
{
value = value.ToString();
break;
}
var bindingProperty = typeFrom.GetProperty(customAttribute.PropertyNameFromModel);
else if (prop == "Count")
{
value = (value as ICollection)?.Count;
break;
}
else if (prop == "Method")
{
int index = 0;
while (true)
{
if (props[index++] == "Method")
break;
if (index == props.Length)
{
break;
}
}
var methodName = props[index].Split('[')?[0];
if (string.IsNullOrEmpty(methodName))
{
break;
}
var parameters = props[index].Split(new char[] { '[', ']' }, StringSplitOptions.RemoveEmptyEntries)?[1].Split(',');
var objs = new object[parameters.Length];
for (int i = 0; i < parameters.Length; ++i)
{
var type = parameters[i].Split(':')[0];
switch (type)
{
case "Enum":
objs[i] = Enum.Parse(customAttribute.MethodParams[i], parameters[i].Split(':')[1]);
break;
}
}
value = typeFrom.InvokeMember(methodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, value, objs);
break;
}
var bindingProperty = value.GetType().GetProperty(prop);
if (bindingProperty != null)
{
value = bindingProperty.GetValue(obj);
value = bindingProperty.GetValue(value);
if (value is null)
{
break;
}
}
else
{
value = null;
break;
}
}
return value;

View File

@ -21,10 +21,10 @@ namespace DepartmentContract.ViewModels
public Guid AcademicPlanRecordId { get; set; }
[ViewModelControlListProperty("Дисциплина")]
[MapConfiguration("AcademicPlanRecord.Discipline.ToString", IsDifficle = true)]
[MapConfiguration("AcademicPlanRecord.Discipline.ToString")]
public string DisciplineName { get; set; }
[MapConfiguration("AcademicPlanRecord.Semester", IsDifficle = true)]
[MapConfiguration("AcademicPlanRecord.Semester")]
public Semester Semester { get; set; }
[ViewModelControlListProperty("Семестр", ColumnWidth = 80)]
@ -34,7 +34,7 @@ namespace DepartmentContract.ViewModels
public Guid TimeNormId { get; set; }
[ViewModelControlListProperty("Норма времени")]
[MapConfiguration("TimeNorm.ToString", IsDifficle = true)]
[MapConfiguration("TimeNorm.ToString")]
public string TimeNormName { get; set; }
[ViewModelControlListProperty("Часы", ColumnWidth = 80)]

View File

@ -26,7 +26,7 @@ namespace DepartmentContract.ViewModels
public Guid DisciplineId { get; set; }
[ViewModelControlListProperty("Дисциплина")]
[MapConfiguration("Discipline.ToString", IsDifficle = true)]
[MapConfiguration("Discipline.ToString")]
public string DisciplineName { get; set; }
[ViewModelControlElementProperty("Преподается на кафедре", ControlType.ControlBool, MustHaveValue = true)]

View File

@ -22,11 +22,11 @@ namespace DepartmentContract.ViewModels
public Guid? EducationDirectionId { get; set; }
[ViewModelControlListProperty("Направление")]
[MapConfiguration("EducationDirection.Cipher", IsDifficle = true)]
[MapConfiguration("EducationDirection.Cipher")]
public string EducationDirectionCipher { get; set; }
[ViewModelControlListProperty("Профиль")]
[MapConfiguration("EducationDirection.Profile", IsDifficle = true)]
[MapConfiguration("EducationDirection.Profile")]
public string EducationDirectionProfile { get; set; }
[ViewModelControlListProperty("Дата начала", ColumnWidth = 120)]

View File

@ -29,7 +29,7 @@ namespace DepartmentContract.ViewModels
public Guid EmployeeId { get; set; }
[ViewModelControlListProperty("Сотрудник")]
[MapConfiguration("Employee.LastName", IsDifficle = true)]
[MapConfiguration("Employee.LastName")]
public string EmployeeName { get; set; }
[ViewModelControlElementProperty("Тип", ControlType.ControlEnum, MustHaveValue = true)]

View File

@ -41,7 +41,7 @@ namespace DepartmentContract.ViewModels
public Guid LecturerId { get; set; }
[ViewModelControlListProperty("Руководитель ОПОП", ColumnWidth = 120)]
[MapConfiguration("Lecturer.LastName", IsDifficle = true)]
[MapConfiguration("Lecturer.LastName")]
public string LecturerName { get; set; }
[ViewModelControlElementProperty("Уровень обучения", ControlType.ControlEnum, MustHaveValue = true)]

View File

@ -20,14 +20,14 @@ namespace DepartmentContract.ViewModels
public Guid EmployeeId { get; set; }
[ViewModelControlListProperty("Сотрудник")]
[MapConfiguration("Employee.ToString", IsDifficle = true)]
[MapConfiguration("Employee.ToString")]
public string EmployeeName { get; set; }
[ViewModelControlElementProperty("Должность", ControlType.ControlGuid, MustHaveValue = true, ReadOnly = false, ControlTypeObject = "DepartmentWindowsDesktop.EntityControls.ControlPostList, DepartmentWindowsDesktop")]
public Guid PostId { get; set; }
[ViewModelControlListProperty("Должность")]
[MapConfiguration("Post.ToString", IsDifficle = true)]
[MapConfiguration("Post.ToString")]
public string PostName { get; set; }
[ViewModelControlListProperty("Ставка", ColumnWidth = 80, DefaultCellStyleFormat = "N1")]

View File

@ -20,14 +20,14 @@ namespace DepartmentContract.ViewModels
public Guid LecturerId { get; set; }
[ViewModelControlListProperty("Преподаватель")]
[MapConfiguration("Lecturer.ToString", IsDifficle = true)]
[MapConfiguration("Lecturer.ToString")]
public string LecturerName { get; set; }
[ViewModelControlElementProperty("Должность", ControlType.ControlGuid, MustHaveValue = true, ReadOnly = false, ControlTypeObject = "DepartmentWindowsDesktop.EntityControls.ControlPostList, DepartmentWindowsDesktop")]
public Guid PostId { get; set; }
[ViewModelControlListProperty("Должность")]
[MapConfiguration("Post.ToString", IsDifficle = true)]
[MapConfiguration("Post.ToString")]
public string PostName { get; set; }
[ViewModelControlListProperty("Ставка", ColumnWidth = 80, DefaultCellStyleFormat = "N1")]

View File

@ -29,14 +29,14 @@ namespace DepartmentContract.ViewModels
public Guid? LecturerAcademicDegreeId { get; set; }
[ViewModelControlListProperty("Уч. степень")]
[MapConfiguration("LecturerAcademicDegree.LecturerAcademicDegreeName", IsDifficle = true)]
[MapConfiguration("LecturerAcademicDegree.LecturerAcademicDegreeName")]
public string LecturerAcademicDegree { get; set; }
[ViewModelControlElementProperty("Уч. звание", ControlType.ControlGuid, MustHaveValue = false, ReadOnly = false, ControlTypeObject = "DepartmentWindowsDesktop.EntityControls.ControlLecturerAcademicRankList, DepartmentWindowsDesktop")]
public Guid? LecturerAcademicRankId { get; set; }
[ViewModelControlListProperty("Уч. звание")]
[MapConfiguration("LecturerAcademicRank.LecturerAcademicRankName", IsDifficle = true)]
[MapConfiguration("LecturerAcademicRank.LecturerAcademicRankName")]
public string LecturerAcademicRank { get; set; }
[ViewModelControlListProperty("Фамилия")]

View File

@ -20,7 +20,7 @@ namespace DepartmentContract.ViewModels
public Guid OrderSyncHistoryId { get; set; }
[ViewModelControlListProperty("Дата", ColumnWidth = 100, DefaultCellStyleFormat = "dd.MM.yyyy")]
[MapConfiguration("OrderSyncHistory.SyncDate", IsDifficle = true)]
[MapConfiguration("OrderSyncHistory.SyncDate")]
public DateTime SyncDate { get; set; }
[ViewModelControlListProperty("Описание")]

View File

@ -23,14 +23,14 @@ namespace DepartmentContract.ViewModels
public Guid EducationDirectionId { get; set; }
[ViewModelControlListProperty("Шифр", ColumnWidth = 80)]
[MapConfiguration("EducationDirection.Cipher", IsDifficle = true)]
[MapConfiguration("EducationDirection.Cipher")]
public string EducationDirectionCipher { get; set; }
[ViewModelControlListProperty("Профиль")]
[MapConfiguration("EducationDirection.Profile", IsDifficle = true)]
[MapConfiguration("EducationDirection.Profile")]
public string EducationDirectionProfile { get; set; }
[MapConfiguration("EducationDirection.ShortName", IsDifficle = true)]
[MapConfiguration("EducationDirection.ShortName")]
public string EducationDirectionShortName { get; set; }
[ViewModelControlListProperty("Группа")]
@ -46,10 +46,10 @@ namespace DepartmentContract.ViewModels
[ViewModelControlElementProperty("Номер группы", ControlType.ControlInt, MustHaveValue = true, MinValue = 0, MaxValue = 4)]
public int GroupNumber { get; set; }
[MapConfiguration("Method.GetStudnetsByState[Enum:Учится]", IsDifficle = true, MethodParams = new Type[] { typeof(StudentState) })]
[MapConfiguration("Method.GetStudnetsByState[Enum:Учится]", MethodParams = new Type[] { typeof(StudentState) })]
public int StudentActualCount { get; set; }
[MapConfiguration("Method.GetStudnetsByState[Enum:Академ]", IsDifficle = true, MethodParams = new Type[] { typeof(StudentState) })]
[MapConfiguration("Method.GetStudnetsByState[Enum:Академ]", MethodParams = new Type[] { typeof(StudentState) })]
public int StudentAcademCount { get; set; }
[ViewModelControlListProperty("Количество студентов")]
@ -59,7 +59,7 @@ namespace DepartmentContract.ViewModels
public Guid? LecturerId { get; set; }
[ViewModelControlListProperty("Куратор")]
[MapConfiguration("Lecturer.ToString", IsDifficle = true)]
[MapConfiguration("Lecturer.ToString")]
public string Lecturer { get; set; }
}
}

View File

@ -25,7 +25,7 @@ namespace DepartmentContract.ViewModels
[ViewModelControlElementProperty("Группа", ControlType.ControlGuid, MustHaveValue = false, ReadOnly = false, ControlTypeObject = "DepartmentWindowsDesktop.EntityControls.ControlStudentGroupList, DepartmentWindowsDesktop")]
public Guid? StudentGroupId { get; set; }
[MapConfiguration("StudentGroup.ToString", IsDifficle = true)]
[MapConfiguration("StudentGroup.ToString")]
public string StudentGroupName { get; set; }
[ViewModelControlElementProperty("Идентификатор универа", ControlType.ControlString, MustHaveValue = true, ReadOnly = true)]

View File

@ -21,7 +21,7 @@ namespace SecurityContract.ViewModels
public Guid RoleId { get; set; }
[ViewModelControlListProperty("Роль", ColumnWidth = 100)]
[MapConfiguration("Role.ToString", IsDifficle = true, AllowCopyWithoutRigth = false)]
[MapConfiguration("Role.ToString", AllowCopyWithoutRigth = false)]
public string RoleName { get; set; }
[ViewModelControlElementProperty("Операция", ControlType.ControlEnum, MustHaveValue = true)]

View File

@ -20,14 +20,14 @@ namespace SecurityContract.ViewModels
public Guid UserId { get; set; }
[ViewModelControlListProperty("Пользователь")]
[MapConfiguration("User.ToString", IsDifficle = true, AllowCopyWithoutRigth = false)]
[MapConfiguration("User.ToString", AllowCopyWithoutRigth = false)]
public string UserName { get; set; }
[ViewModelControlElementProperty("Роль", ControlType.ControlGuid, MustHaveValue = true, ReadOnly = false, ControlTypeObject = "SecurityWindowsDesktop.EntityControls.ControlRoleList, SecurityWindowsDesktop")]
public Guid RoleId { get; set; }
[ViewModelControlListProperty("Роль")]
[MapConfiguration("Role.ToString", IsDifficle = true, AllowCopyWithoutRigth = false)]
[MapConfiguration("Role.ToString", AllowCopyWithoutRigth = false)]
public string RoleName { get; set; }
}
}