From 96beff6a1cad4ca19a652340e85e3b9c54726b2a Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Mon, 13 Mar 2023 15:01:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A8=A1=E5=9E=8B=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E6=8E=A5=E5=8F=A3=E5=AE=9E=E7=8E=B0=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ModelValidation/IModelValidate.cs | 7 +++++ .../ModelValidation/IModelValidateResult.cs | 19 +++++++++++++ .../ModelValidation/ObjectValidateExtend.cs | 4 +-- Falcon.SugarApi/ModelValidation/Readme.md | 27 +++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 Falcon.SugarApi/ModelValidation/IModelValidate.cs create mode 100644 Falcon.SugarApi/ModelValidation/IModelValidateResult.cs create mode 100644 Falcon.SugarApi/ModelValidation/Readme.md diff --git a/Falcon.SugarApi/ModelValidation/IModelValidate.cs b/Falcon.SugarApi/ModelValidation/IModelValidate.cs new file mode 100644 index 0000000..fcd4c92 --- /dev/null +++ b/Falcon.SugarApi/ModelValidation/IModelValidate.cs @@ -0,0 +1,7 @@ +namespace Falcon.SugarApi.ModelValidation +{ + /// + /// 标记接口。表示该对象需要进行模型验证 + /// + public interface IModelValidate { } +} diff --git a/Falcon.SugarApi/ModelValidation/IModelValidateResult.cs b/Falcon.SugarApi/ModelValidation/IModelValidateResult.cs new file mode 100644 index 0000000..c4118fa --- /dev/null +++ b/Falcon.SugarApi/ModelValidation/IModelValidateResult.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Falcon.SugarApi.ModelValidation +{ + /// + /// 对模型进行验证 + /// + public static class IModelValidateResult + { + /// + /// 验证对象是否通过模型验证 + /// + /// 要验证的对象 + /// 如果为null则通过,否则返回验证结果列表 + public static List? Validate(this IModelValidate model) + => model.TryModelValidation(out var results) ? null : results; + } +} diff --git a/Falcon.SugarApi/ModelValidation/ObjectValidateExtend.cs b/Falcon.SugarApi/ModelValidation/ObjectValidateExtend.cs index 34dd8c2..4c6ace9 100644 --- a/Falcon.SugarApi/ModelValidation/ObjectValidateExtend.cs +++ b/Falcon.SugarApi/ModelValidation/ObjectValidateExtend.cs @@ -14,10 +14,10 @@ namespace Falcon.SugarApi.ModelValidation /// 要验证的模型 /// 验证结果 /// 通过验证True,否则False - public static bool TryModelValidation(this object model, out List results) { + public static bool TryModelValidation(this object model,out List results) { var context = new ValidationContext(model); results = new List(); - return Validator.TryValidateObject(model, context, results, true); + return Validator.TryValidateObject(model,context,results,true); } } } diff --git a/Falcon.SugarApi/ModelValidation/Readme.md b/Falcon.SugarApi/ModelValidation/Readme.md new file mode 100644 index 0000000..3932a9f --- /dev/null +++ b/Falcon.SugarApi/ModelValidation/Readme.md @@ -0,0 +1,27 @@ +## ģ֤ + +### ֤ʽһֱӵTryModelValidation + +~~~C# +if(!data.TryModelValidation(out var errors)) { + //֤ʧ +} + //֤ͨ + +~~~ + +### ֤ʽҪ֤ģʵIModelValidateӿڣýӿΪԽӿڣκʵ֡ȻýӿڵValidateȡ֤ + +~~~C# +public class Model:IModelValidate{} + +var data=new Model(); + + +var errors=data.Validate(); +if(errors!=null){ + //֤ʧ +} + //֤ͨ + +~~~