Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Build Data-Driven Client Validation
Create data-driven validation rules for distributed apps, so you can distribute the rules to clients without modifying client-side application code.
by Tony Surma and Bill Wagner

Posted April 9, 2004

Technology Toolbox: C#, XML

Data can come from diverse sources—many .NET applications involve smart clients, Web services, and a database engine for storage. You also might use a DataSet to transfer collections of data from the Web service to the client application, and a DataGrid to display these elements to users. Despite this complexity, or perhaps because of it, you need to provide users with validating feedback when they edit collections of data.

The validations can be as simple as requiring a field to be present, or as complex as a calculation involving multiple columns and tables. You can enforce simple validation rules easily by adding constraints to the DataSet. However, you must write code to implement complicated business rules, and you'll have to duplicate this code on both the server and client sides of your application. Worse yet, these business rules change as new practices are put into place.

ADVERTISEMENT

We'll show you a design that enables you to create data-driven validation rules for your distributed applications. You can distribute the data driving the business rules from server to client without modifying the application's code. As business rules change or get extended, you need to modify only the data stored on the server. None of the clients need any code modifications.

A variety of methods are available for validating user input. We created a set of classes that integrate with the DataSet class and set error information on the row or column involved whenever users enter invalid information. We wanted the validation class to be completely data-driven, with the specific rules encoded in data members that you can serialize from the server to client, or even store persistently. The specific rule loads the data at run time and attaches event handlers that respond to data-change events in the DataSet. The validator runs its rules and sets or clears the proper error information when users change particular data elements.

Of course, one class can't do all that work; too many different kinds of validation rules are possible. So we wrote a hierarchy of validators that perform different types of validation. The base class attaches event handlers and stores the table and column names along with custom error messages. Each derived class is distinguished by the code used to perform the validation. For example, one class models the WebForms' RangeValidator control, and another uses the DataTable's Compute function to execute a formula on a set of rows and a given column. Both classes use logic you can serialize and modify at run time.

We used Microsoft's sample Pubs database to create a sample application that demonstrates this functionality (see Figure 1). The sample loads a list of titles, a list of authors for each title, and the royalty schedule for the titles. You can edit any of the fields, though we didn't add code that would enable the sample app to save changes back to the database.




Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home