Class ParallelRuleExecutor
An implementation of IExecutesAllRules which executes validation rules in parallel.
Inheritance
Implements
Inherited Members
Namespace: CSF.Validation.RuleExecution
Assembly: CSF.Validation.dll
Syntax
public class ParallelRuleExecutor : IExecutesAllRules
Remarks
This class uses a fairly naive algorithm to achieve parallelism. It could be described as such:
- Get all of the available rules, via GetRulesWhichMayBeExecuted(). If no rules are found in this way then exit and return all of the results which have been found across all iterations of this algorithm.
- Sort those available rules into two lists, one list of rules which may be run in parallel & another list of those which cannot be run in parallel. The sorting process uses IsEligibleToBeExecutedInParallel. These lists become the rules that are pending for execution.
- Add all of the rules which may be run in parallel to an "in progress" list of tasks & empty the pending list for rules which may be run in parallel.
- As each of the in-progress parallel rules completes remove it from the in-progress list and record its result.
- Once all in-progress list is empty, try again to find new rules which may executed in parallel, possibly refilling the "can be run in parallel" pending list. If any rules eligible to be run in parallel are found in this way, return to step 3 with regard to these newly-found rules. This is a process similar to steps 1 & 2, except that a result of finding no new rules does not terminate the algorithm. Any new non-parallelisable rules that are found are added to the existing "cannot be run in parallel" pending list collection
- Now that we have run out of available rules which may be executed in parallel, execute all rules in the "cannot be run in parallel" pending list in sequence & record their results. As this occurs, clear the "cannot be run in parallel" pending list.
- Return to step 1 to attempt to find additional available rules to execute.
Step 5 (above) is not strictly neccesary but is included as an attempt at optimisation. It ensures that rules which may be run in parallel are prioritised over rules which may not be run in parallel. This means that non-parallelisable rules will generally be left toward the end of each iteration, ensuring that as many rules as possible (which were eligible to be run in parallel) are run in parallel.
Constructors
| Improve this Doc View SourceParallelRuleExecutor(IExeucutesSingleRule)
Initialises a new instance of ParallelRuleExecutor.
Declaration
public ParallelRuleExecutor(IExeucutesSingleRule ruleExecutor)
Parameters
Type | Name | Description |
---|---|---|
IExeucutesSingleRule | ruleExecutor | A service which executes rules. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
Methods
| Improve this Doc View SourceExecuteAllRulesAsync(IRuleExecutionContext, CancellationToken)
Execute all of the specified validation rules and return their results.
Declaration
public async Task<IReadOnlyCollection<ValidationRuleResult>> ExecuteAllRulesAsync(IRuleExecutionContext executionContext, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
IRuleExecutionContext | executionContext | The validation rule execution context. |
System.Threading.CancellationToken | cancellationToken | An optional cancellation token. |
Returns
Type | Description |
---|---|
System.Threading.Tasks.Task<System.Collections.Generic.IReadOnlyCollection<ValidationRuleResult>> | A task containing a collection of the results from the executed validation rules. |