WAQS: Exposing Validate method on the service

7 reasons to use WAQS

WAQS documentation

 

With the same way than for calculated properties, you can use NotApplicableOnClient attribute to specify that a validation method is only applicable on the server on saving and still with the same way, if you use the DAL context on method parameters, it will only be usable from the server too.

However, in some cases we want to check if our data are correct from the client anyway.

For example, imagine that we don’t want to be able to create a new Customer if another one already exists with the same CompanyName and ContactName.

For this, we will use the DAL context on our Validate method.

In the UI, perhaps we want to check it when the user mentioned these information before setting all the other ones.

In order to be able to call it from the client, we will expose the method in the WCF Service.

To do it, we will use an attribute ExposeAsService in our specifications:

[ExposeAsService]
public static Error ValidateCustomerNameUnique(this Customer c, INorthwindWAQSEntities context)
{
     if (c.CompanyName != null && c.ContactName != null &&
          context.Customers.Any(c2 =>
              (c.Id == null ||  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; }


After transforming T4 templates we will find a method ValidateCustomerNameUnique on our service and the client context allows you to use it very easily:



var error = await _context.ValidateCustomerNameUniqueAsync(c);  



 



Note that if you use Transform All T4 templates it’s possible that generated code does not compile. Indeed, the client context will encapsulate the WCF proxy ValidateCustomerNameUnique method. But, because the server compilation does not append yet, the WCF proxy does not include this method. In this case, after compiling the server, you can just transform the proxy template.

This entry was posted in 10369, 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>