Skip to content

10 Validate methods

Matthieu MEZIL (MSFT) edited this page May 31, 2015 · 4 revisions

You previously use metadata for validation.

This kind of validation is easy to use, even without WAQS.
However, sometimes you want something more complex.

For example, imagine that you don't want to allow orders with no detail.
Writing this validation rule is not as easy as it seems.
Indeed, you have to:

  • Check in memory when you add an order
  • Use the DB to check if the order still have details when you remove one detail or one you change a detail order.

For this last point, due to concurrency, you have to use a SQL transaction.

Thus the code becomes more complex to write but also more complex to read and the developer intention could be hidden in technical code.

For this, WAQS proposes to use Validate specification methods.

In order to check in orders has details, add this method to OrderSpecifications class.

public static Error ValidateOrderHasOrderLines(this Order o)
{
    if (!o.OrderDetails.Any())
        return new Error { Criticity = Criticity.Error, Message = "Order must have detail" };
    return null;
} 

This method is very easy to write, read and understand.
And from this very easy method, WAQS will generate the complex code mention above

Then Update solution generated code.

Now to test it, runs the client application twice and edit the same customer.
Select the same order.
Delete first part of order details in the first window and the last one in the second window.
Click on Save on the first window.
Click on Save on the second window.

You should have the following error message in the second window

As always, you can download the source here.