Chapter 6. Validation Application Block

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:

  • Validation rules are not limited to any specific layer of the application; validation logic/rules can be used to validate across different layers.
  • Rule-based validation provides flexibility to validate an object for various scenarios through a Rule Set.
  • It provides consistent and flexible validation mechanism.
  • It allows validating objects using attributes.
  • It allows validating values programmatically.
  • It allows validating objects using self-validation.
  • It allows validating objects using configuration.
  • It provides several validator types for the most common validation scenarios.
  • It provides adapters for integration with ASP.NET, Windows Forms, WPF, and WCF.

In this chapter, you will:

  • Be introduced to the Validation Application Block
  • Be introduced to concepts such as Validators, ValidatorFactory, ValidationResults, and so on
  • Learn about referencing the required and optional assemblies
  • Learn to set up the initial infrastructure configuration using the configuration editor
  • Learn to validate objects using attributes
  • Learn to validate values programmatically
  • Learn to validate using self-validation
  • Learn to validate objects using configuration
  • Learn to integrate the Validation Application Block with Windows Forms based applications
  • Learn to integrate the Validation Application Block with ASP.NET web applications
  • Learn to implement a custom Validator

Validation Application Block features

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:

  • Validating objects using attributes by decorating the properties with the required Validators
  • Validating values programmatically
  • Validating objects using self-validation by decorating the class with the HasSelfValidation attribute and providing a validation method
  • Validating objects using configuration by providing type to be validated, rule set, validation targets (Properties, Fields and/or Methods), and validation rules

Each 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.198.81