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

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> /// </summary>
public string PropertyNameFromModel { get; set; } public string PropertyNameFromModel { get; set; }
/// <summary>
/// Сложное свойство (свойствое в другом классе-свойстве)
/// </summary>
public bool IsDifficle { get; set; } = false;
/// <summary> /// <summary>
/// Можно копировать поле даже при доступе без прав /// Можно копировать поле даже при доступе без прав
/// </summary> /// </summary>

View File

@ -105,80 +105,65 @@ namespace ToolsModule.ManagmentMapping
private static object GetValueFromCustomAttribute(MapConfigurationAttribute customAttribute, Type typeFrom, object obj) private static object GetValueFromCustomAttribute(MapConfigurationAttribute customAttribute, Type typeFrom, object obj)
{ {
object value = obj; object value = obj;
if (customAttribute.IsDifficle) var props = customAttribute.PropertyNameFromModel.Split('.');
foreach (var prop in props)
{ {
var props = customAttribute.PropertyNameFromModel.Split('.'); if (prop == "ToString")
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")
{ {
value = value.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) if (bindingProperty != null)
{ {
value = bindingProperty.GetValue(obj); value = bindingProperty.GetValue(value);
if (value is null)
{
break;
}
}
else
{
value = null;
break;
} }
} }
return value; return value;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -23,14 +23,14 @@ namespace DepartmentContract.ViewModels
public Guid EducationDirectionId { get; set; } public Guid EducationDirectionId { get; set; }
[ViewModelControlListProperty("Шифр", ColumnWidth = 80)] [ViewModelControlListProperty("Шифр", ColumnWidth = 80)]
[MapConfiguration("EducationDirection.Cipher", IsDifficle = true)] [MapConfiguration("EducationDirection.Cipher")]
public string EducationDirectionCipher { get; set; } public string EducationDirectionCipher { get; set; }
[ViewModelControlListProperty("Профиль")] [ViewModelControlListProperty("Профиль")]
[MapConfiguration("EducationDirection.Profile", IsDifficle = true)] [MapConfiguration("EducationDirection.Profile")]
public string EducationDirectionProfile { get; set; } public string EducationDirectionProfile { get; set; }
[MapConfiguration("EducationDirection.ShortName", IsDifficle = true)] [MapConfiguration("EducationDirection.ShortName")]
public string EducationDirectionShortName { get; set; } public string EducationDirectionShortName { get; set; }
[ViewModelControlListProperty("Группа")] [ViewModelControlListProperty("Группа")]
@ -46,10 +46,10 @@ namespace DepartmentContract.ViewModels
[ViewModelControlElementProperty("Номер группы", ControlType.ControlInt, MustHaveValue = true, MinValue = 0, MaxValue = 4)] [ViewModelControlElementProperty("Номер группы", ControlType.ControlInt, MustHaveValue = true, MinValue = 0, MaxValue = 4)]
public int GroupNumber { get; set; } 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; } 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; } public int StudentAcademCount { get; set; }
[ViewModelControlListProperty("Количество студентов")] [ViewModelControlListProperty("Количество студентов")]
@ -59,7 +59,7 @@ namespace DepartmentContract.ViewModels
public Guid? LecturerId { get; set; } public Guid? LecturerId { get; set; }
[ViewModelControlListProperty("Куратор")] [ViewModelControlListProperty("Куратор")]
[MapConfiguration("Lecturer.ToString", IsDifficle = true)] [MapConfiguration("Lecturer.ToString")]
public string Lecturer { get; set; } 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")] [ViewModelControlElementProperty("Группа", ControlType.ControlGuid, MustHaveValue = false, ReadOnly = false, ControlTypeObject = "DepartmentWindowsDesktop.EntityControls.ControlStudentGroupList, DepartmentWindowsDesktop")]
public Guid? StudentGroupId { get; set; } public Guid? StudentGroupId { get; set; }
[MapConfiguration("StudentGroup.ToString", IsDifficle = true)] [MapConfiguration("StudentGroup.ToString")]
public string StudentGroupName { get; set; } public string StudentGroupName { get; set; }
[ViewModelControlElementProperty("Идентификатор универа", ControlType.ControlString, MustHaveValue = true, ReadOnly = true)] [ViewModelControlElementProperty("Идентификатор универа", ControlType.ControlString, MustHaveValue = true, ReadOnly = true)]

View File

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

View File

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