WAQS: Transactional validation business rules

7 reasons to use WAQS

WAQS documentation

 

If you really want to be sure that the couple (CompanyName, ContactName) on customers is unique and if you don’t want to express it in the DB to not spit your business logic between the DB and the code for example, you have to use a transaction for concurrency aspects.

To express it with WAQS, you can use a Validate method that not returns an error but that return a Func<Error>:

public static Func<Error> ValidateCustomerNameUnique(this Customer c, INorthwindWAQSEntities context)
{
     return () =>
     {
         if (c.CompanyName != null && c.ContactName != null &&
context.Customers.Any(c2 => c2.Id != c.Id &&
c2.CompanyName == c.CompanyName && c2.ContactName == c.ContactName))
             return new Error
{
Criticity = Criticity.Error,
Message = "A customer with the same company name and the same contact name already exist"
};
         return null;
     }; }


Using it, WAQS will generate a SaveChanges method that opens a transaction, saves changes, checks that every added / modified customers have an unique couple (CompanyName, ContactName) and finally commit (if check passed) or rollback (if check failed) the transaction.



Note that in this case, you can’t use ExposeAsService attribute anymore. It is just usable and used on server SaveChanges method.



However, it can be combined with ValidateOnServer attribute.

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>