While developing applications we always have to be distrustful of any input; be it from users or from other systems, it is very important to validate the input. Developers often spend the bulk of their development effort on validating input, yet we find ourselves struggling to manage the validation logic, which spreads like a plague into every nook and corner of the application code. To make our life more challenging we have to deal with several different validation mechanisms that are available for different types of applications (ASP.NET, Windows Forms, WCF, WPF); also within the same application we may have to validate input at multiple places across different layers or trust boundaries.
Started to feel dizzy? Let us do something about it.
The Validation Application Block is a structured, consistent, easy-to-maintain, flexible, and reusable component to perform validations. It provides commonly required Validators that can be leveraged to validate input. It can be used to prevent invalid input; also business rules validation can be implemented. As the approach is not focused towards any specific layer (for example the UI) the same validation rules can be used to validate the input at different layers in the application. The Validation Application Block includes adapters for technologies such as ASP.NET, Windows Forms, Windows Communication Foundation (WCF), and Windows Presentation Foundation (WPF).
The following are the key features of the Validation block:
In this chapter, you will:
Validator
Several Validation Application Block elements work together to fulfill the validation requirements. To start with, we have to decide on the validation method. The application block provides validation methods such as the following:
HasSelfValidation
attribute and providing a validation methodEach validation method is useful in its own way; we may opt for the validation method based on our needs. The Validation block provides several Validators (.NET classes), which can be grouped using Rule Sets, so while validating objects we may provide a Rule Set and all the validation rules are processed. These Rule Sets are mapped against properties, fields, and/or methods of the types to be validated. Now using the appropriate ValidatorFactory
we may initiate the validation process by providing the object to be validated and the rule set to be used for validation.
3.141.192.120