WAQS: Validation rules with navigation properties

7 reasons to use WAQS

WAQS documentation


Imagine that we want to be sure that an order does not exist without any detail.

This basic feature is not as easy as it seems because of concurrency.

Indeed, if it’s easy for insertion, it’s more complex if you want to remove some OrderDetails or if you want to change their OrderId.

In this case, the process is the following:

  • open a transaction
  • query the DB to get get deleted/with OrderId changed OrderDetails’ OrderId with a DB lock
  • save changes
  • check if orders with ids we got at step 2 still have some details
  • commit or rollback the transaction.

So this code is not so easy and many junior developers could forget concurrency aspect.

In addition, even if they write it with a good way, there is another issue: the developer intention is lost reading this code because of technical code. And this makes maintainability more complex.

With WAQS, you can write this validation rule with another way:

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

Based on this super easy code with no technical aspect and because we use a navigation property, WAQS generates the complex code we just explained.


Note that in the current version, this just works with navigation property many. But implementing it with navigation property one is in my backlog.

Published Sat, Dec 28 2013 15:56 by Matthieu MEZIL

Leave a Comment

If you can't read this number refresh your screen
Enter the numbers above: