private async ValueTask <RuleResult[]> HandleAsync(IValidationRule <T> rule, T[] items, CancellationToken cancellationToken) { // Creates final handler that executes the rule itself. async ValueTask <ValidationResult[]> finalHandler() { return(await rule.ValidateAsync(items, cancellationToken)); } // Creates the handler chain by reversing the original list of handlers then aggregating them with the final handler as the seed. var handlerChain = _handlers .Reverse() .Aggregate((ValidationRuleHandlerDelegate)finalHandler, (next, handler) => () => handler.HandleAsync(rule.GetType(), items, next, cancellationToken)); var validationResult = await handlerChain(); return(validationResult .Select(result => new RuleResult(rule.GetType(), result)) .ToArray()); }