WAQS: Global validation

7 reasons to use WAQS

WAQS documentation

 

As I explained in this post and this one, WAQS helps to define entities validation.

Sometimes you want a global entity validation or a global context validation. So WAQS also generate a method ValidateOnClient that uses the entity state:

public virtual IEnumerable<Error> ValidateOnClient()
{
    if (ChangeTracker.State == ObjectState.Added || ChangeTracker.State == ObjectState.Modified && ChangeTracker.ModifiedProperties.Contains("Discount"))
    {
        Error error = ValidateDiscountMinValue(Discount);
        if (error != null)
            yield return error;
        error = ValidateDiscountMaxValue(Discount);
        if (error != null)
            yield return error;
    }
       
    if ((ChangeTracker.State & ObjectState.Deleted) == 0 && ChangeTracker.OriginalValues.ContainsKey("Order"))
    {
        Error error = ValidateOrderRequired(Order);
        if (error != null)
            yield return error;
    }
       
    if ((ChangeTracker.State & ObjectState.Deleted) == 0 && ChangeTracker.OriginalValues.ContainsKey("Product"))
    {
        Error error = ValidateProductRequired(Product);
        if (error != null)
            yield return error;
    }
       
    List<Error> errors = null;
    GetCustomValidation(ref errors);
    if (errors != null)
        foreach (var er in errors)
            yield return er;
}
partial void GetCustomValidation(ref List<Error> errors);


 



Note that you also have a method ValidateOnClient in client context which aggregates all its entities ValidateOnClient and when you call the SaveChanges in the client context, you have an optional parameter validate (false by default) that validates all context entities before calling the server to save changes in DB.

This entry was posted in 16868. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>