Show / Hide Table of Contents

Use the common rule results

Use the static CommonResults class within your rules to get result instances/tasks of result instances. You should use the CommonResults class as follows.

using static CSF.Validation.Rules.CommonResults;

public class MustBeFour : IRule<int>
{
    public ValueTask<RuleResult> GetResultAsync(int validated,
                                                RuleContext context,
                                                CancellationToken token = default)
    {
        return validated == 4 ? PassAsync() : FailAsync();
    }
}

Optimisation as well as convenience

When omitting the optional parameters (or passing only null) to the following methods, the CommonResults class offers an optimisation. In these scenarios flyweight instances will be returned instead of allocating new RuleResult instances.

  • Pass()
  • PassAsync()
  • Fail()
  • FailAsync()
  • Error()
  • ErrorAsync()

This will improve throughput, particularly for validators which include a large number of rules which do not need to return data when they pass or fail.

  • Improve this Doc
In This Article
Back to top Generated by DocFX