Asynchronous Validation

Occasionally I’ve wanted to execute Validation Rules that take a significant duration to execute (anything more than half a second for example). Normally these involve some cross-tier communication, e.g. database access, web service call etc.  Examples of these types of validation include:

  1. Validating that a field is unique – for example when entering a new Inventory Item which requires a unique code. This could also require a unique combination of field values, for example an item name that must be unique during its effective lifetime specified by a from/to date.
  2. Validating stock levels for a selected product.

It could be argued that these types of validation are best performed by the business layer on the application server after the user has committed the transaction, i.e. pressed the Save/Submit button. Of course the rules must be validated at that point anyway – since the business layer on your application server should never trust any data being sent to it. But that doesn’t stop us using the same rules to provide timely warnings to the user prior them submitting a form full of data.

The following code shows an sample ValidationRule designed to execute asynchronously.

/// <summary>
/// Sample validation rule that executes asynchronously by default.
/// </summary>
public class SampleAsyncRule : ValidationRule
private int _milliSecondsToDelay;
public SampleAsyncRule(int milliSecondsToDelay)
_milliSecondsToDelay = milliSecondsToDelay;
IsAsync = true; // Setting IsAsync to true ensures the Validate method is executed on a background thread.
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
// This call could be replaced with a cal to the application service tier via a web service, remoting etc.
Random random = new Random();
if (random.Next(100) < 50)
return new ValidationResult(ContentSeverity.Error, "Async validation has determined an error.");
return ValidationResult.ValidResult;

When validating a registered rule the FormValidator checks the ValidationRule.IsAsync flag. If set to true it executes the Validate method on the rule using an asynchronous delegate call. The rule is interpreted as having returned a “pending” ValidationResult which will put the Validator in an indeterminate state (assuming it was previously in a Valid state). When the async delegate completes a callback method on the Validator is fired which extracts the real ValidationResult and removes the temporary “pending” result.

protected override void ValidateInternal(object validationSource)
foreach (Spencen.Validation.Rules.ValidationRule rule in RegisteredRules[validationSource])
Spencen.Validation.ValidationResult result;
if (rule.IsAsync)
AsyncValidateCaller caller = new AsyncValidateCaller(rule.BeginValidate);
IAsyncResult asyncResult = caller.BeginInvoke(validationSource, 
ValidateCallback, caller); result = new AsyncValidationResult(asyncResult); } else { result = rule.Validate(validationSource, CultureInfo.CurrentCulture); } ExtractErrors(result); } OnValidated(EventArgs.Empty); }

2 thoughts on “Asynchronous Validation”

  1. hi, im also trying to achieve the same in my app. could you please upload the code if it’s possible.

  2. Hi kedar,
    This post was describing one small part of the prototype validation framework I was building at the time. I’ve never had the oppurtunity to complete this framework – although it did/does seem quite promising. I could upload the code but there is quite a bit of it and the only sample that I have is tied in with a bunch of other libaries I was working on that I’m not ready to release. So if you really want it I can give you just the Validation libarary – but the only samples on using it will be from there few blog posts on validation.

Comments are closed.