Silverlight's Validation Fix - InformationWeek

InformationWeek is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them.Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Software // Enterprise Applications
01:40 PM

Silverlight's Validation Fix

Latest version offers a better way to check data accuracy By Dan Wahlin

Data validation--the process of guaranteeing to an application that every data value is accurate--is a crucial part of any application. But for developers using Microsoft Silverlight, this has been a problem: There wasn't a good way to ensure that data, such as a user ID or e-mail address, is entered correctly.

That's changed with Silverlight 4, thankfully.

Before now, the typical way of validating data was to throw exceptions within property-setter blocks when data was invalid. Controls bound to a property could be notified of a data validation exception by setting ValidatesOnExceptions to True in the data binding. While this works, Silverlight 4's IDataErrorInfo provides a better way to validate data.

Let's start with IDataErrorInfo's members. The IDataErrorInfo interface contains two members, including Error, which can return error information about the overall object, and a default indexer property that can be used to write validation logic for different properties within an object. You implement the IDataErrorInfo interface on a client-side entity class in your Silverlight project. When the Error property isn't used--often the case--you can simply return null.

The bulk of validation work is done in the default indexer property. It's called by controls that are bound to object properties as data changes to see if a particular bound property is valid or not. Assume your code has two basic properties in it that need to be validated. This can be done in the default indexer property added into the class as a result of implementing IDataErrorInfo as in this code. (See code in box.)

The validation operations performed in the default indexer property are often straightforward. For example, a switch statement can check which property needs to be validated. From there, each case statement is used to perform validation logic, including checking that the Name property isn't null or empty and that the Age property has a value greater than zero.

What's nice about this approach is that the validation logic is put in one place and, in more real-world situations, the logic can even be moved to an external validation object that provides some reuseable methods to check for null strings, check string lengths, validate numeric ranges, etc.

The end result of implementing the IDataErrorInfo interface is that controls bound to properties can check to see if data is valid or not and then display an error message as appropriate. This is done by adding the ValidatesOnDataErrors property to the data binding.

The downside of IDataErrorInfo is that it doesn't perform asynchronous validation. In some situations, you may need to call back to a server to check that a user ID or e-mail address is unique within the system or that a tax code matches an existing code stored in a database. IDataErrorInfo doesn't support this type of scenario directly.

Dan Wahlin is a Microsoft MVP and Silverlight consultant. You can write to us at [email protected]

We welcome your comments on this topic on our social media channels, or [contact us directly] with questions about the site.
Comment  | 
Print  | 
More Insights
InformationWeek Is Getting an Upgrade!

Find out more about our plans to improve the look, functionality, and performance of the InformationWeek site in the coming months.

Becoming a Self-Taught Cybersecurity Pro
Jessica Davis, Senior Editor, Enterprise Apps,  6/9/2021
Ancestry's DevOps Strategy to Control Its CI/CD Pipeline
Joao-Pierre S. Ruth, Senior Writer,  6/4/2021
IT Leadership: 10 Ways to Unleash Enterprise Innovation
Lisa Morgan, Freelance Writer,  6/8/2021
White Papers
Register for InformationWeek Newsletters
Current Issue
Planning Your Digital Transformation Roadmap
Download this report to learn about the latest technologies and best practices or ensuring a successful transition from outdated business transformation tactics.
Flash Poll