在C#中为一系列验证结果构建一个N深度结构
Build a N depth structure for a series of validation results in C#
背景
我有以下一组 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" }
问题
但是"PropertyName","ErrorMessage"key-value模型不能像这样形成N-depth的结构。我不知所措。
问题
- 我应该如何为此建立数据结构模型?
- 最佳做法是什么?
您可以构建一个如下所示的 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 表单,请查看 TreeView class if you want to display this. For WPF, look into the WPF TreeView。
背景
我有以下一组 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" }
问题
但是"PropertyName","ErrorMessage"key-value模型不能像这样形成N-depth的结构。我不知所措。
问题
- 我应该如何为此建立数据结构模型?
- 最佳做法是什么?
您可以构建一个如下所示的 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 表单,请查看 TreeView class if you want to display this. For WPF, look into the WPF TreeView。