If you're familiar with ASP.NET validation, you'll know that it's really easy to add this to a page. For example, if you have a TextBox, and you want to ensure that the user enters something in it, you just add a RequiredFieldValidator control...

When the user clicks the submit button, if the TextBox is empty, they will get a message box pop up telling them "You must enter your name," the word "required" will appear next to the TextBox, and a bulleted list of errors (in this case only the one) will be shown where the ValidationSummary control is placed. You can easily change this if you don't want all three of these (I switch off the bulleted list and keep the other two, but that's just my preference).

This is great in that it gives both client-side and server-side validation, without you needing to write any code. The problem is that it munges the layers. Validation logic should really be in the logic layer (duh), and not in the presentation layer.

The RIA services toolkit gives you an alternative. Let's assume that the required name field above maps to a Name property on one of the entity objects in our model. Then the RIA method that inserts/updates the object could contain code like this...

Notice that there isn't any validation logic here, it's all in the RIA service, which is where it belongs. The format of this control is exactly the same as the RequiredFieldValidator, except that this is a generic validator, so doesn't include the actual logic for validation. You would use the same code for any kind of validation, and (unlike ASP.NET validators, where you need one for each validation type), you only need one DomainValidator per control.

What happens now is that if the user tries to submit the form without entering a name, the code in the RIA service throws a ValidationException, which is caught by the DomainValidator. This displays the error messages just as before.

So we have achieved a clear separation of the logic from the presentation, and done it with very little change. But wait, it gets even better

Although the validation code we wrote in the RIA service was only three lines, it would be nice if we didn't even need to write this much. After all, we're busy and lazy, right? The good news is that for most basic validation, you don't even need to write any code, you just need one simple attribute.

To show this, we need to go back to when we generated the RIA service itself. To add one to your project, you choose a new Domain Service Class. This opens up a window where you can configure the service...

Notice that little checkbox at the bottom? If you check that, then you will get a second file generated when you click OK. Along with the DomainService1.cs file (which is the code for the actual RIA service), you get a file called DomainService1.metadata.cs which contains (wait for it...) metadata! OK, so what the heck is metadata? Simply put, it's information about your service, and basically contains class definitions for the objects in the service.

The clever bit is that you can decorate these classes (I love that expression!) with attributes that will do the validation for you. So, if we open the metadata file for our object, we can simply add a [Required] attribute to the Name property...

1: [Required]

2: publicstring Name { get; set; }

Now you don't need the C# code in the RIA service, and the validation will be done for you. That's all there is to it! Actually, that's not quite true. To do the job properly, you really need to add an error message there, as you may have more than one validation on a property, and you need to be able to give a meaningful message back. You can do this by modifying the attribute like this...

What we end up with is a very concise way of performing validation, as most of it can be done by adding simple attributes to the metadata. As mentioned before, this is all in the RIA service, which is our business logic layer, so the structure of the application remains clear, unlike with the ASP.NET validators.

The one downside to this is that (as far as I know), you can't do client-side validation this way. I really like client-side validation as it's a fast way to inform the user that something needs attention. As the validation code now all lives in the RIA service, it all happens on the server, so the user has to wait for a round trip before they see any warnings. Also, I haven't found a way to have the message box pop up when there is a validation error. I tried enabling this in the ValidationSummary control, but that generated a run-time exception when the validation fired. I would love to find a way around this, but still feel that the combination of the DomainValidator and metadata attributes gives such a powerful validation method, that it's worth the sacrifice.