Wednesday, 27 February 2013

WCF RIA Validations

How RIA Services Implement Validations

RIA Services have a built in way to validate entity up to the Silverlight UI.The validation logic used is common in the client as well as the server side.

Whenever an entity is changed , the ChangeSet is validated immediately , if the ChangeSet contains validation error , the validation results are passed  and shown at the client in a ValidationErrorContainer. In this case the ChangeSet does not travel to the server , thus doesn't call any service.

If the ChangeSet is a valid one that is there are no validation errors , the service is called. There may be cases where some business rules need to be implemented. For example , In an employee management application , whether the employee is eligible for a long leave is a decision to be taken based on the attendance data of the employee and this information cannot be shared with the client.  So, we implement the LongLeave rule in the server side.

Based on the business rule either the entity is set as valid or invalid. In case an entity is set as invalid at the server side , here we can show the errors either in a message box or an validation error container.

Following diagram shows how the RIA Services work in an MVVM way:





Data Annotation Validation Attributes

The namespace used for validation is System.ComponentModel.DataAnnotations.The class used to validate entities is the ValidationAttribute class in the above namespace.Any class that derives from ValidationAttribute class is a validator.

In RIA Services both property level and object level validations can be done.Validation attributes can be placed on the properties of metadata class. These validation attributes will be used for server side validation by WCF RIA Services,and they will also be used client side because the client generated entities will have those attributes on their properties.

Following are the common property level validator:
a. RequiredAttribute
b.StringLengthAttribute 
c.RangeAttribute
d.RegularExpressionAttribute



Custom Validation Attributes


Cross property validation


ValidationContext:


Validations during Submit Operation


Async Validation using Invoke Methods


References







No comments:

Post a Comment