英文:
FluentValidation as a Composite Key
问题
我有一个表格,它有一个复合键。
GroupCode GroupRec oDescription
T 123 ......
T 321 ......
K 123 ......
M 123 ......
该键是 GroupCode & GroupRec
。
我想将这个复合键应用到 FluentValidation 中。我应该如何做?
我有以下代码作为验证器:
private readonly IApplicationDBContext _context;
public CreateGroupingCommandValidator(IApplicationDBContext context)
{
_context = context;
RuleFor(v => v.GroupCode)
.MaximumLength(50).WithMessage("最大长度为50。")
.MustAsync(BeUniqueName).WithMessage("已存在。")
.NotEmpty().WithMessage("必填。");
RuleFor(v => v.GroupRec)
.MaximumLength(50).WithMessage("最大长度为50。")
.MustAsync(BeUniqueName).WithMessage("已存在。")
.NotEmpty().WithMessage("必填。");
RuleFor(v => v.oDescription)
.MaximumLength(50).WithMessage("最大长度为50。");
}
private async Task<bool> BeUniqueName(string codename, CancellationToken cancellationToken)
{
return await _context.Mrekcs.AllAsync(x => x.GroupCode != codename, cancellationToken);
}
我应该如何将 BeUniqueName
修改成一个复合键?
请指教。谢谢。
英文:
I have a table that have a composite key on it.
GroupCode GroupRec oDescription
T 123 ......
T 321 ......
K 123 ......
M 123 ......
The key is GroupCode & GroupRec
.<br>
I want to apply the composite key to a FluentValidation. How can I do that?
I have the following code as a validator:<br>
private readonly IApplicationDBContext _context;
public CreateGroupingCommandValidator(IApplicationDBContext context)
{
_context = context;
RuleFor(v => v.GroupCode)
.MaximumLength(50).WithMessage("Maximum length is 50.")
.MustAsync(BeUniqueName).WithMessage("Already exists.")
.NotEmpty().WithMessage("Required.");
RuleFor(v => v.GroupRec)
.MaximumLength(50).WithMessage("Maximum length is 50.")
.MustAsync(BeUniqueName).WithMessage("Already exists.")
.NotEmpty().WithMessage("Required.");
RuleFor(v => v.oDescription)
.MaximumLength(50).WithMessage("Maximum length is 50.");
}
private async Task<bool> BeUniqueName(string codename, CancellationToken cancellationToken)
{
return await _context.Mrekcs.AllAsync(x => x.GroupCode != codename, cancellationToken);
}
How can I modify the BeUniqueName
into a composite key?<br>
Please advice. Thank you.
答案1
得分: 2
以下是您要翻译的内容:
假设您已经实现了一个验证唯一复合键的函数,如下所示:
private async Task<bool> BeUniqueCompositeKey(string groupCode, string groupRec, CancellationToken cancellationToken)
{
return await _context.Mrekcs.AllAsync(x => x.GroupCode != groupCode && x.GroupRec != groupRec, cancellationToken);
}
在GroupCode
的FluentValidation规则中,您可以通过将异步委托函数传递给MustAsync
来实现,如下所示:
RuleFor(v => v.GroupCode)
.MaximumLength(50)
.WithMessage("最大长度为50。")
.NotEmpty()
.WithMessage("必填项。")
.MustAsync(async (root, groupCode, context) =>
{
return await BeUniqueCompositeKey(groupCode, root.GroupRec, new CancellationToken());
})
.WithMessage("重复的复合键:GroupCode & GroupRec");
英文:
Assume that you have implemented a function to validate the unique composite key as below:
private async Task<bool> BeUniqueCompositeKey(string groupCode, string groupRec, CancellationToken cancellationToken)
{
return await _context.Mrekcs.AllAsync(x => x.GroupCode != groupCode && x.GroupRec != groupRec, cancellationToken);
}
In the FluentValidation rule for GroupCode
, you can implement by passing a delegate async function to MustAsync
as below:
RuleFor(v => v.GroupCode)
.MaximumLength(50)
.WithMessage("Maximum length is 50.")
.NotEmpty()
.WithMessage("Required.")
.MustAsync(async (root, groupCode, context) =>
{
return await BeUniqueCompositeKey(groupCode, root.GroupRec, new CancellationToken());
})
.WithMessage("Duplicate Composite key: GroupCode & GroupRec");
答案2
得分: 0
你可以将你的规则拆分出来,让 BeUniqueName
接受你的模型,而不是一个 string
private readonly IApplicationDBContext _context;
public CreateGroupingCommandValidator(IApplicationDBContext context)
{
_context = context;
RuleFor(v => v.GroupCode)
.MaximumLength(50).WithMessage("最大长度为50。")
.NotEmpty().WithMessage("必填。");
RuleFor(v => v.GroupRec)
.MaximumLength(50).WithMessage("最大长度为50。")
.NotEmpty().WithMessage("必填。");
RuleFor(v => v)
.MustAsync(BeUniqueName).WithMessage("已存在。")
RuleFor(v => v.oDescription)
.MaximumLength(50).WithMessage("最大长度为50。");
}
private async Task<bool> BeUniqueName(CreateGrouping grouping, CancellationToken cancellationToken)
{
return await _context.Mrekcs.AllAsync(x => x.GroupCode != grouping.GroupCode && x.GroupRec != grouping.GroupRec, cancellationToken);
}
英文:
You can split out your rules and let BeUniqueName
take in your model as opposed to a string
private readonly IApplicationDBContext _context;
public CreateGroupingCommandValidator(IApplicationDBContext context)
{
_context = context;
RuleFor(v => v.GroupCode)
.MaximumLength(50).WithMessage("Maximum length is 50.")
.NotEmpty().WithMessage("Required.");
RuleFor(v => v.GroupRec)
.MaximumLength(50).WithMessage("Maximum length is 50.")
.NotEmpty().WithMessage("Required.");
RuleFor(v => v)
.MustAsync(BeUniqueName).WithMessage("Already exists.")
RuleFor(v => v.oDescription)
.MaximumLength(50).WithMessage("Maximum length is 50.");
}
private async Task<bool> BeUniqueName(CreateGrouping grouping, CancellationToken cancellationToken)
{
return await _context.Mrekcs.AllAsync(x => x.GroupCode != grouping.GroupCode && x.GroupRec != grouping.GroupRec, cancellationToken);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论