Sunday, April 08, 2007

Enterprise Library 3.0

Enterprise Libary 3.0

Last week Enterprise Library 3.0 was released.

Enterprise library is a package of eight application blocks. The blocks include Caching, Cryptography, Data Access, Exception Handling, Logging, Security, Validation, and Policy Injection. These blocks are small frameworks that implement enterprise patterns that help make developing enterprise applications easier. The Enterprise Library also includes a set of core functions, including configuration, instrumentation, and object builder services. These functions are used by all other application blocks.

Of all the new blocks the two that jump out at me are Validation, and Policy Injection. They are new to version 3.0.


Validation Block
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 allow you to easily validate objects. In many situations, you can validate an object with a single line of code. The following example shows how to associate a validator with an object and how to validate that object.
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public class Customer
{
[StringLengthValidator(0, 20)]
public string CustomerName;

public Customer(string customerName)
{
this.CustomerName = customerName;
}
}

public class MyExample
{
public static void Main()
{
Customer myCustomer = new Customer("A name that is too long");
ValidationResults r = Validation.Validate(myCustomer);
if (!r.IsValid)
{
throw new InvalidOperationException("Validation error found.");
}
}
}

Because the StringLengthValidator attribute is applied, the Validation Block checks that the length of the customer name is between 0 and 20 characters. The application creates a new Customer object and validates it using the Validation Block fa├žade. In this case, the customer name is illegal because it is too long. The application throws an exception that notifies you of the error.

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, with attributes, and with code.
  • It allows you to associate multiple rule sets with the same class and with members of that class.
  • It can be integrated with ASP.NET, Windows Forms, and Windows Communications Foundation (WCF).
I will talk about the policy injection block in another post. I have not used it yet and really don't have much knowledge of it.

Post a Comment