2017-12-15 20 views
0

배경는 C#으로 검증 결과의 연속하는 N 깊이 구조를 구축

I는 ValidationResult 데이터의 다음 세트를 갖는다. List<ValidationResult>입니다.

은 참고

실제로 두 가지 유형이 포함되어 있습니다. 나는 사용자 정의 유효성 검사 결과를 작성하여 ValidationResult을 구현하여 모델의 모든 오류를 수집했습니다. 따라서이 데이터 세트에는 실제로 두 가지 유형의 데이터가 있습니다. 하나는 ValidationResult이고 다른 하나는 CustomValidationResult입니다.

[0] { "Name Field shouldn't be null" } 
[1] { "Money Field should be in range 0 and 100" } 
[2] { "Validation failed at CompanyList" } // This is CustomValidationResult 
    ㄴ [0] { "CompanyName Field shouldn't be null" } // ValidationResult inside of CustomValidationResult. 
    ㄴ [1] { "Validation failed at DepartmentList" } // Belows are same as above 
     ㄴ [0] { "DepartmentName Field shouldn't be null" } 
     ㄴ [1] { "Validation failed at EmployeeList" } 
      ㄴ [0] { "EmployeeName Field shouldn't be null" } 
      ㄴ [1] { "EmployeeEmail Field's format should be Email" } 

Validator.TryValidateObject(model, context, true) 방법 System.ComponentModel.DataAnnotations.Validator에 내장 정적 함수의 결과이다.

어쨌든 개인적인 오류에 쉽게 액세스하기 위해 결과 세트를 더 예쁘게 보이게 만들고 싶습니다. "Key" "Value"구조를 생각 중이므로 유효성 검사 프로세스에서 어떤 속성이 실패했는지 쉽게 알 수 있습니다.

// I want the result to look something like this below. 

[0] { "Name", "Name Field Shouldn't be null" } 
[1] { "Money", "Money Field should be in range 0 and 100" } 
[2] { "CompanyList", "Validation failed at CompanyList" } 
    ㄴ [0] { "CompanyName", "CompanyName Field shouldn't be null" } 
    ㄴ [1] { "DepartmentList", "Validation failed at DepartmentList" } 
     ㄴ [0] { "DepartmentName", "DepartmentName Field shouldn't be null" } 
     ㄴ [1] { "EmployeeList", "Validation failed at EmployeeList" } 
      ㄴ [0] { "EmployeeName", "EmployeeName Field shouldn't be null" } 
      ㄴ [1] { "EmployeeEmail", "EmployeeEmail Field's format should be Email" } 

PROBLEM 그러나 'PROPERTYNAME "이런 N 심층 구조를 형성 할 수있다"ErrorMessage가 "키 값 모델. 나는 무엇을 해야할지를 놓치고있다.

질문

  • 어떻게 이것에 대한 데이터 구조를 모델링해야합니까?
  • 가장 좋은 방법은 무엇입니까?

답변

1

이처럼 보이는 ValidationResultNode을 만들 수 :

class ValidationResultNode 
{ 
    public string PropertyName {get; private set;} 
    public string ErrorMessage {get; private set;} 
    public List<ValidationResultNode> Children {get; private set;} 
    public ValidationResult(string propName, string errMsg) 
    { 
     PropertyName = propName; 
     ErrorMessage = errMsg; 
     Children = new List<ValidationResultNode>(); 
    } 

    // method to add a child error 
    public ValidationResultNode AddChildError(string propName, string errMsg) 
    { 
     var result = new ValidationResultNode(propName, errMsg); 
     Children.Add(result); 
     return result; 
    } 

그것은 기본적으로 중첩 된 목록으로 구현 계층 트리입니다.

Windows Forms를 사용하는 경우 표시하려면 TreeView class을 살펴보십시오. WPF의 경우 WPF TreeView을 살펴보십시오.

+0

나는 이렇게 개체를 만들었습니다. .NET에서 목표를 달성하기위한 멋진 방법이 있기를 희망했습니다. 다소 실제 중첩 된 객체를 만드는 것을 피하고 싶었습니다. 하지만 생각 해보니 처음부터 불가능한 것이 아닙니다. – hina10531