WAQS: Metadata definition

7 reasons to use WAQS

WAQS documentation


WAQS uses edmx metadata for validation: MaxLength (for string and byte array) and Nullable.

However, edmx metadata often are insufficient: no MinValue, no MaxValue, no MinLength, no Pattern and even less dynamic metadata.

In specifications, you can fix it using some static methods to define metadata.

These methods must start by “Define”, without any parameter and returning void.

public static void DefineOrderDetailMetadata()
    Metadata<OrderDetail>.DefineMinValue(od => od.Discount, 0);
    Metadata<OrderDetail>.DefineMaxValue(od => od.Discount, 1);

With this way you can define static metadata but also dynamic one:

public static void DefineCustomerMetadata()
    Metadata<Customer>.DefinePattern(c => c.PostalCode, c => c.Country != null && 
      c.Country.ToUpper() == "FRANCE" ? @"^(\d{2}|(2(A|B)))\d{3}$" : null);
    Metadata<Customer>.IsNullable(c => c.City, c => c.PostalCode == null);

WAQS will generate some validation code using these metadata and edmx ones.

The SaveChanges method (generated  by WAQS) verifies this business rules before Saving changes into the DB (I will explain in a future post how it works).

In the client side, there is a metadata validation too and there are two ways to use them with WPF and one with PCL.


The first way is to use WPF DataErrorInfo. WAQS generates the DataErrorInfo logic. So, in your binding, you can just set ValidatesOnDataErrors to true

<TextBox Text="{Binding OrderDetail.Discount, ValidatesOnDataErrors=true}" />

Then, if you write something wrong the control border will be red (by default).

However, I don’t find neither DataErrorInfo nor NotifyDataErrorInfo (for WPF 4.5 only and not used by WAQS) enough good. Indeed, we can’t easily have many errors for example or mixing some errors and some warnings.

For this, WAQS proposes another way. It proposes to use a collection of errors for each scalar properties of an entity.

WAQS for WPF proposes this property as a new PropertyDescriptor on entity type (which is better for developer experience IMO) and, in addition, it generates a behavior to show errors.

So, in our sample, you can use the following XAML code:

<TextBox Text="{Binding OrderDetail.Discount}"
         controls:ErrorsBehaviors.Errors="{Binding OrderDetail.DiscountErrors}" />

With this way, the border has the color of this higher error (red if error, orange if warning) with a tooltip which list the different errors:


Note that, as very often with WAQS, you have a partial method in ErrorsBehaviors class to change the style.

static partial void SetControlStyle(Control control, ObservableCollection<Error> errors, Criticity maxCriticity, 
ref bool done);

In addition, you can also use other Metadata like MaxLength to bind them (and so avoid possible errors):

<TextBox Text="{Binding Customer.CompanyName}" MaxLength="{Binding Customer.CompanyNameMaxLength}" />


With PCL, we can’t use behavior of PropertyDescriptors. So WAQS just generate some properties (DiscountErrors in the OrderDetail and CompanyNameMaxLength in Customer in our sample).

This entry was posted in 16868, 18204, 8708. Bookmark the permalink.

Leave a Reply

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