how to incorporate the Validation Application Block into your application

06/16/2009

To prepare your application

  1. Add a reference to the Validation Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.EnterpriseLibrary.Validation.dll assembly. Select the assembly, and then click OK to add the reference.
  2. Use the same procedure to set a reference to the Enterprise Library Common assembly, named Microsoft.Practices.EnterpriseLibrary.Common.dll.
  3. Follow the same procedure to set a reference to the ObjectBuilder assembly, Microsoft.Practices.EnterpriseLibrary.ObjectBuilder 2 .dll.
  4. If you are using the ASP.NET, Windows Forms, or WCF integration assemblies, add one of the following references as appropriate.
    • Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WinForms.dll
    • Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.dll
    • Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF.dll
  5. (Optional) To use elements from the Validation Application Block without fully qualifying the type with the namespace, add the following using statements (C#) or Imports statements (Visual Basic) to the top of your source code file.

    C#

    using Microsoft.Practices.EnterpriseLibrary.Validation;
    using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

    Visual Basic

    Imports Microsoft.Practices.EnterpriseLibrary.Validation
    Imports Microsoft.Practices.EnterpriseLibrary.Validation.Validators

Note:

For Visual Basic projects, you can use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer. On the Project menu, click Properties. When the Project Designer appears, click the References tab.

Posted in: .NET Framework| Tags: Reference Block Application Validation Application Block Validation Incorporate Coding Assembly Microsoft click dll integration enterpriselibrary

Entering Configuration Information of Validation Application Block

06/16/2009

These procedures explain how to configure the Validation Application Block with the configuration tools. Properties associated with the nodes appear in the right pane of the Configuration Console or the Properties window of the Visual Studio Configuration Editor.

The Validation Application Block allows you to use attributes and code to perform many of the tasks described here. For information about how to use attributes and code, see Using the Provided Validators. For information about properties that are associated with validators such as Tag and MessageTemplate, see Understanding Common Validator Properties.

This procedure explains how to add the Validation Application Block to the configuration file. For details of the schema for the Validation Application Block configuration, see Source Schema for the Validation Application Block.

To add the Validation Application Block

  1. Open the configuration file. For more information, see Configuring Enterprise Library.
  2. Right-click Application Configuration, point to New, and then click Validation Application Block.
  3. (Optional) If you want to encrypt the configuration file, click in the ProtectionProvider field, and then select a protection provider from the drop-down list.

The next procedure shows how to define a rule set for the members of a type. Members of a type can be fields, methods, or properties. Note that it is possible for a type to have multiple rule sets associated with it. The procedure assumes that you have already added the Validation Application Block to your configuration.

To define a rule set for members of a type

  1. Right-click Validation Application Block, point to New, and click Type.
  2. In the Type Selector System.Object dialog box, expand the assembly you want to use. To filter the list, in the Filter edit box type the string to use to filter the list, for example type "string" to filter for all classes containing the word "string". If the assembly is not in the dialog box, click Load form File or Load from GAC and navigate to it.
  3. Double-click the type you want to validate.
  4. (Optional) If there is an existing rule set you want to specify as the default, click DefaultRule in the properties pane and select it from the drop-down box.
  5. To define a rule set, right-click on the type node, point to New, and then click Rule Set. Note that a rule set can contain as few as one validator.
  6. Click in the Name property field and rename the rule set as required.
  7. To select the member you want to validate, you can use either of the following methods:
    • Right-click Rule Set, point to New, and then click Field, Method, or Property. Then enter the name of the field, method, or property in the properties pane.
    • Alternatively, you can select various type members simultaneously. Right-click Rule Set, point to New, and then click Choose Members. In the Member Selector dialog box, select the Properties, Methods, and/or Fields that you want to validate, and then click OK.
  8. Right-click the field, method, or property, point to New, and then click the validator you want to apply.
  9. (Optional) Fill in the Tag, MessageTemplate, and MessageTemplateResource fields, as appropriate. If you use the MessageTemplateResource property, you must also specify the MessageTemplateResourceTypeName property. You can either enter the name of the MessageTemplateResourceTypeName type in the field or click on the ellipsis button (...) and use the Type Selector System.Object dialog box to select it.
  10. Enter values for the remaining validator properties. The properties available differ for each type of validator. For a list of properties for each type of validator, see Using the Provided Validators.
  11. Repeat steps 8 to 10 for each validator that you require for the selected member. All the validators you add for a specific member must evaluate to true for validation to succeed.
  12. Repeat steps 7 to 10 for each member of the type that you want to validate.

The next procedure shows how to apply validators at the type level. A type-level validator applies to an instance of a class as a whole rather than to members of that class. Typically, you will use a custom validator that you have created yourself, although the Not Null Validator may also be appropriate.

To apply validators at the type level

  1. Repeat steps 1 through 6 of the procedure "To define a rule set for members of a type."
  2. Right-click Self, point to New, and then click the validator you want to use. Typically, this is either Custom Validator or Not Null Validator.
  3. For each validator, fill in the Tag, MessageTemplate and MessageTemplateResource fields, as appropriate. If you use the MessageTemplateResource property you must also specify the MessageTemplateResourceTypeName property. You can either enter the MessageTemplateResourceTypeName type in the field or click on the ellipsis and use the Type Selector System.Object dialog box to select it. Many validators also have fields that are specific to them. Fill these out as well.
  4. Repeat steps 2 and 3 for each validator that you require. All the validators you add for a specific type must evaluate to true for validation to succeed.

The next procedure explains how to define an AndCompositeValidator or an OrCompositeValidator composite validator. Composite validators contain individual validators that are combined with a Boolean AND or OR operation. You can nest them to create complex logic for a member, such as (A OR (B AND C)).

To define composite validators

  1. Repeat steps 1 to 6 of the procedure "To define a rule set for members of a type."
  2. Right-click the type member that you want to validate or the Self node, and then click either AndCompositeValidator or OrCompositeValidator.
  3. Right-click AndCompositeValidator or OrCompositeValidator, and then click one of the validators that will be a part of the composite validator.
  4. For each validator, fill in the Tag, MessageTemplate and MessageTemplateResource fields, as appropriate. If you use the MessageTemplateResource property you must also specify the MessageTemplateResourceTypeName property. You can either enter the MessageTemplateResourceTypeName type in the field or click the ellipsis button (…) and use the Type Selector — System.Object dialog box to select it. Many validators also have fields that are specific to them. Fill these out as well.
  5. Repeat steps 3 and 4 for each validator that you require to create the logic for the current AndCompositeValidator or OrCompositeValidator.
  6. Repeat steps 2 to 5 for each AndCompositeValidator or OrCompositeValidator you require.
Posted in: .NET Framework| Tags: Configuration Validation Application Block Validation

Introduction to the Validation Application Block

06/16/2009

This topic includes a series of brief sections that provide information to help you decide whether the Validation Application Block is suitable for your requirements. This topic includes the following sections:

  • Common Scenarios
  • Example Application Code
  • Highlights of the Validation Application Block
  • Determining When to Use the Validation Application Block
  • Alternatives to Using the Validation Application Block

In addition to this introductory material, the documentation contains the following topics:

  • Developing Applications Using the Validation Application Block. This topic explains how to include the Validation Application Block in your applications and how to configure it. It also contains more detailed information, such as how to create custom message templates and information on how validation works with inheritance.
  • Key Scenarios. This topic shows different ways to use the Validation Application Block in your own applications.
  • Design of the Validation Application Block. This topic includes a class diagram of the Validation Application Block.
  • Extending and Modifying the Validation Application Block. This topic explains how to extend the application block by adding custom validators and attributes. It also contains advice about how to modify the source code.
  • Deployment and Operations. This topic explains how to deploy and update the application block assemblies. It also explains the application block's instrumentation.
  • Validation QuickStarts. This topic explains how to install and configure the two QuickStart applications and contains a series of walkthroughs that demonstrate how to incorporate common validation operations into an application.

For details of the system requirements for the Validation Application Block, see System Requirements. For details of the dependencies for the Validation Application Block, see Application Block Dependencies.

Common Scenarios

Validation has many applications. For example, you can use it to prevent the injection of malicious data by checking to see if a string is too long or if it contains illegal characters. You can also use validation to enforce business rules and to provide responses to user input. It is often important to validate data several times within the same application. For example, you may need to validate data at the UI layer to give immediate feedback when a user enters an invalid data value, and again at the service interface layer for security.

The Validation Application Block is designed to address the most common tasks developers face when they must validate input either from a user or another system. These tasks are arranged according to scenarios. Each scenario gives an example of a typical way to use the Validation Application Block and shows the code that accomplishes the task.

The scenarios are the following:

  • Using Attributes to Define Validation Rule Sets
  • Using Self Validation
  • Validating Objects
  • Integrating with ASP.NET, Windows Forms, and WCF; this scenario includes information about integrating the Validation Application Block into the following types of applications:
    • ASP.NET
    • Windows Forms
    • WCF

Highlights of the Validation Application Block

The Validation Application Block has the following benefits:

  • It helps maintain consistent validation practices.
  • It includes validators for validating most standard .NET data types.
  • It allows you to create validation rules with configuration, attributes, and code.
  • It allows you to associate multiple rule sets with the same class and with members of that class.
  • It allows you to apply one or more rule sets when you validate an object.
  • It can be integrated with ASP.NET, Windows Forms, and Windows Communications Foundation (WCF).

Determining When to Use the Validation Application Block

The Validation Application Block allows you to encapsulate validation best practices into easily maintainable code that you can reuse. Encapsulation also allows you to separate the application code from the validation logic. In some situations, you may be able to update the validation logic without redeploying the application. Another common situation where the application block works well is when your validation code must work across multiple layers of the application's architecture.

In very simple cases, when you only need to validate a few objects, you may not want to incur the overhead of adding the application block.

Alternatives to Using the Validation Application Block

Alternatives to using the Validation Application Block may include using the validation capabilities that are a part of ASP.NET and Windows Forms. Another alternative for WCF and other applications that use XML data is to use the XML Schema Definition tool (XSD), which allows you to validate messages at the XML level. If your validation logic only needs to be applied within these technologies you may not need to use the application block. However, if the validation logic needs to be reused, the application block is a better choice.

Posted in: .NET Framework| Tags: WCF Introduction Validation Application Block Validation

The Validation Application Block in Enterprise Library 4.1

06/16/2009

The Enterprise Library Validation Application Block provides useful features that allow developers to implement structured and easy-to-maintain validation scenarios in their applications. Any application that accepts input either from users or from other systems must ensure that the information is valid in terms of some set of rules that you specify. For example, when processing an order, you may need to check that a customer's phone number has the correct number of digits or that a date falls within a particular range. In addition, if the validation fails, you may need to send an error message that explains what is wrong.

The Enterprise Library Validation Application Block provides a library of classes named validators, which implement functionality for validating .NET Framework data types. For example, one validator checks for null strings and another validator checks that a number falls within a specified range.

There are also special validators named AndCompositeValidator and OrCompositeValidator. If you create an AndCompositeValidator, which aggregates other validators, all validators in the composite validator must return T rue for successful validation. If you create an OrCompositeValidator, at least one of the validators in the composite validator must return T rue for successful validation.

You can also group validators together in a rule set. A rule set allows you to validate a complex object or graph by composing different validators of different types and applying them to elements in the object graph. Examples of these elements include fields, properties, and nested objects.

By using the Validation Application Block, you can perform validation and create rule sets in the following three ways:

  • Using configuration
  • Using attributes
  • Using code

In addition, the Validation Application Block includes adapters that allow you to use the application block with the following technologies:

  • ASP.NET
  • Windows Forms
  • Windows Communications Framework (WCF)
Posted in: .NET Framework| Tags: Enterprise Library Block Application Validation Application Block Validation number example enterprise rule validator library range

Hot Posts

Latest posts

Tags

Others

Sponsors