FluentValidation作为复合键

huangapple go评论71阅读模式
英文:

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 &amp; 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 =&gt; v.GroupCode)
            .MaximumLength(50).WithMessage(&quot;Maximum length is 50.&quot;)
            .MustAsync(BeUniqueName).WithMessage(&quot;Already exists.&quot;)
            .NotEmpty().WithMessage(&quot;Required.&quot;);

        RuleFor(v =&gt; v.GroupRec)
            .MaximumLength(50).WithMessage(&quot;Maximum length is 50.&quot;)
            .MustAsync(BeUniqueName).WithMessage(&quot;Already exists.&quot;)
            .NotEmpty().WithMessage(&quot;Required.&quot;);

        RuleFor(v =&gt; v.oDescription)
            .MaximumLength(50).WithMessage(&quot;Maximum length is 50.&quot;);
    }

    private async Task&lt;bool&gt; BeUniqueName(string codename, CancellationToken cancellationToken)
    {
        return await _context.Mrekcs.AllAsync(x =&gt; 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");

演示 @ .NET Fiddle

英文:

Assume that you have implemented a function to validate the unique composite key as below:

private async Task&lt;bool&gt; BeUniqueCompositeKey(string groupCode, string groupRec, CancellationToken cancellationToken)
{
	return await _context.Mrekcs.AllAsync(x =&gt; x.GroupCode != groupCode &amp;&amp; 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 =&gt; v.GroupCode)
		.MaximumLength(50)
		.WithMessage(&quot;Maximum length is 50.&quot;)
		.NotEmpty()
		.WithMessage(&quot;Required.&quot;)
		.MustAsync(async (root, groupCode, context) =&gt; 
		{
			return await BeUniqueCompositeKey(groupCode, root.GroupRec, new CancellationToken());
		})
		.WithMessage(&quot;Duplicate Composite key: GroupCode &amp; GroupRec&quot;);

Demo @ .NET Fiddle

答案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 =&gt; v.GroupCode)
            .MaximumLength(50).WithMessage(&quot;Maximum length is 50.&quot;)
            .NotEmpty().WithMessage(&quot;Required.&quot;);

        RuleFor(v =&gt; v.GroupRec)
            .MaximumLength(50).WithMessage(&quot;Maximum length is 50.&quot;)
            .NotEmpty().WithMessage(&quot;Required.&quot;);

        RuleFor(v =&gt; v)
            .MustAsync(BeUniqueName).WithMessage(&quot;Already exists.&quot;)

        RuleFor(v =&gt; v.oDescription)
            .MaximumLength(50).WithMessage(&quot;Maximum length is 50.&quot;);
    }

    private async Task&lt;bool&gt; BeUniqueName(CreateGrouping grouping, CancellationToken cancellationToken)
    {
        return await _context.Mrekcs.AllAsync(x =&gt; x.GroupCode != grouping.GroupCode &amp;&amp; x.GroupRec != grouping.GroupRec, cancellationToken);
    }

huangapple
  • 本文由 发表于 2023年5月30日 08:37:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360979.html
  • asp.net-core
  • c#
  • clean-architecture
  • fluentvalidation
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定