Software // Enterprise Applications
News
10/28/2010
01:40 PM
Connect Directly
RSS
E-Mail
50%
50%

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 iweekletters@techweb.com.

Comment  | 
Print  | 
More Insights
Building A Mobile Business Mindset
Building A Mobile Business Mindset
Among 688 respondents, 46% have deployed mobile apps, with an additional 24% planning to in the next year. Soon all apps will look like mobile apps and it's past time for those with no plans to get cracking.
Register for InformationWeek Newsletters
White Papers
Current Issue
InformationWeek Tech Digest - July 22, 2014
Sophisticated attacks demand real-time risk management and continuous monitoring. Here's how federal agencies are meeting that challenge.
Flash Poll
Video
Slideshows
Twitter Feed
InformationWeek Radio
Archived InformationWeek Radio
A UBM Tech Radio episode on the changing economics of Flash storage used in data tiering -- sponsored by Dell.
Live Streaming Video
Everything You've Been Told About Mobility Is Wrong
Attend this video symposium with Sean Wisdom, Global Director of Mobility Solutions, and learn about how you can harness powerful new products to mobilize your business potential.