Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查ASP.NET样板文件的ICustomValidate中的现有记录_C#_Validation_Asp.net Core_Custom Validators_Asp.net Boilerplate - Fatal编程技术网

C# 检查ASP.NET样板文件的ICustomValidate中的现有记录

C# 检查ASP.NET样板文件的ICustomValidate中的现有记录,c#,validation,asp.net-core,custom-validators,asp.net-boilerplate,C#,Validation,Asp.net Core,Custom Validators,Asp.net Boilerplate,对于ASP.NET样板文件中的ICustomValidate,我们可以验证字段的值 我想知道是否能够并且建议在ICustomValidate中检查添加的学生姓名是否已经存在 例如,在创建新学生时,我们将检查同名学生是否已经存在。我们能否将此逻辑移到ICustomValidate?您可以: public类CreateStudentDto:ICustomValidate { 公共字符串名称{get;set;} public void AddValidationErrors(CustomValidat

对于ASP.NET样板文件中的
ICustomValidate
,我们可以验证字段的值

我想知道是否能够并且建议在
ICustomValidate
中检查添加的学生姓名是否已经存在

例如,在创建新学生时,我们将检查同名学生是否已经存在。我们能否将此逻辑移到
ICustomValidate

您可以:

public类CreateStudentDto:ICustomValidate
{
公共字符串名称{get;set;}
public void AddValidationErrors(CustomValidationContext上下文)
{
使用(var scope=context.IocResolver.CreateScope())
{
使用(var uow=scope.Resolve().Begin())
{
var studentRepository=scope.Resolve();
var name exists=studentRepository.GetAll()
.Where(s=>s.Name==Name)
.Any();
如果(名称存在)
{
var key=“已存在同名学生”;
var errorMessage=context.Localize(“sourceName”,key);
var memberNames=new[]{nameof(Name)};
Add(新的ValidationResult(errorMessage,memberNames));
}
uow.Complete();
}
}
}
}
但这种验证通常在域管理器中完成,例如

在DTO中,建议在不变条件下:

公共类CreateTaskInput:ICustomValidate
{
public int?AssignedPersonId{get;set;}
公共bool sendmailtoassignedperson{get;set;}
public void AddValidationErrors(CustomValidationContext上下文)
{
如果(发送电子邮件给AssignedPerson&(!AssignedPersonId.HasValue | | AssignedPersonId.Value