← Back to API Reference

RuleBatch

Batch evaluation for 10+ rules with shared compilation context.

public sealed class RuleBatch : IRuleEngine

Methods

AddRule(Rule) / AddRules(IEnumerable<Rule>)

Adds rules to the batch.

var batch = new RuleBatch()
    .AddRule(new Rule { Expression = "x > 0", Description = "Positive" })
    .AddRule(new Rule { Expression = "x < 100", Description = "Under limit" })
    .AddRules(GetRulesFromDatabase());

Compile(RuleParameter, string[]?, AssemblyReferenceProvider?)

Single compile pass for all rules, from one parameter.

var compileParam = new RuleParameter("customer", typeof(Customer));
batch.Compile(compileParam);

Evaluate(RuleParameter) / EvaluateParallel(RuleParameter)

Evaluates all rules. Async variants EvaluateAsync(RuleParameter) and EvaluateParallelAsync(RuleParameter) take the same single parameter.

var results = batch.EvaluateParallel(parameter);
foreach (var result in results)
{
    Console.WriteLine($"{result.RuleDescription}: {(result.Success ? "PASS" : "FAIL")}");
}

Loading Rules

Source Method
Manual batch.AddRule(new Rule { ... })
List batch.AddRules(existingRules)
JSON batch.AddRules(JsonRuleLoader.LoadWorkflowFromFile("rules.json").Rules)
Database batch.AddRules(dbContext.Rules.Where(r => r.IsActive).ToList())


Back to top

MIT License. Built with Roslyn + Typed Delegates.