April 2007 Entries
The Rules Engine That Saved the Day (But not the Application…)

The Rules Engine That Saved the Day (But not the Application…) -

I think at some level we all can relate to Rick Wagner's Business Rules Engine/Workflow story.

posted @ Friday, April 20, 2007 10:22 AM | Feedback (26)
Outlook Trivia - Out of Office

I came across this post this morning...

You've heard the term "Out of Office" or OOF for short.  Why isn't it OOO for Out Of Office?  Read the blog post for the full story....

"OOF was a command used in the days of Microsoft’s Xenix mail system, which set a user as ‘Out of Facility’ - ie Out of the Office."


posted @ Friday, April 6, 2007 11:49 AM | Feedback (20)
Validation Application Block - Custom Validation

Microsoft is on the verge of releasing version 3.0 of the Enterprise Library.  Among the many new features included in version 3.0 is a block called the Validation Application Block (VAB).  The VAB is one of my favorite blocks because of its simplicity, extensibility and how relative it is to application development.

"The Enterprise Library Validation Application Block provides a library of classes, called validators, that supplies the code 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 three ways to use the VAB.  You can place attributes above your methods, use a configuration file to define rule sets or code the validation logic.

Below is an example from the documentation demonstrating the simplicity of the VAB.  The MyExample class thows an exception if the length of the CustomerName value is greater than 20 characters using the StringLengthValidator attribute.  The use of attributes is straightforward, intuitive and simple. 

   1: public class Customer
   2: {
   3:     [StringLengthValidator(0, 20)]
   4:     public string CustomerName;
   6:     public Customer(string customerName)
   7:     {
   8:         this.CustomerName = customerName;
   9:     }
  10: }
  12: public class MyExample
  13: {
  14:     public static void Run() 
  15:     {
  16:         Customer myCustomer = new Customer("A name that is too long");
  17:         ValidationResults r = Validation.Validate<Customer>(myCustomer);
  18:         if (!r.IsValid)
  19:         {
  20:             throw new InvalidOperationException("Validation error found.");
  21:         }
  22:     }
  23: }

There are a number of out of box Validators you can use that cover most scenerios.  Here is a list of Validators from the Februrary CTP release:

And Composite Validator
Contains Characters Validator
Date Time Range Validator
Domain Validator<T>
Enum Conversion Validator
Not Null Validator
Object Collection Validator
Object Validator
Or Composite Validator
Property Comparison Validator
Range Validator<T>
Regular Expression Validator
Relative Date Time Validator
String Length Validator
Type Conversion Validator

The VAB documentation includes sample code and quickstarts to get you started.

Custom Validation Example
The VAB is designed as is most of the Enterprise Library on the Open-Closed Principle.  In other words, the VAB is easy to extend yet doesn't require modifying the VAB code.

In the code below, I created a custom validator called NotEmptyCollectionValidator, which validates that an object collection is not empty.  See the code below.

   1: [NotEmptyCollectionValidator]
   2: public ObligorCollection ObligorCollection
   3: {
   4:     get { return _obligorCollection; }
   5: }

Here is the NotEmptyCollectionValidator class and NotEmptyCollectionValidatorAttribute class:

   1: using System;
   2: using System.Collections;
   3: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
   4: using Microsoft.Practices.EnterpriseLibrary.Validation;
   5: using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
   6: using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
   8: namespace Halos.DocumentManagement.VirtualVault.BusinessLibrary
   9: {
  10:     [ConfigurationElementType(typeof (CustomValidatorData))]
  11:     public class NotEmptyCollectionValidator : Validator
  12:     {
  13:         public NotEmptyCollectionValidator() : base(null, null) { }
  15:         protected override string DefaultMessageTemplate
  16:         {
  17:             get { return "The collection {0} does not have any items"; }
  18:         }
  20:         protected override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
  21:         {
  22:             ICollection obCollection = (ObligorCollection) objectToValidate;
  23:             if (obCollection.Count == 0)
  24:             {
  25:                 string message = string.Format(MessageTemplate, objectToValidate);
  26:                 LogValidationResult(validationResults, message, currentTarget, key);
  27:             }
  28:         }
  29:     }
  31:     public class NotEmptyCollectionValidatorAttribute : ValidatorAttribute
  32:     {
  33:         protected override Validator DoCreateValidator(Type targetType)
  34:         {
  35:             return new NotEmptyCollectionValidator();
  36:         }
  37:     }
  38: }

As you can see extending the VAB to add custom validators is quite easy. 

posted @ Sunday, April 1, 2007 4:31 AM | Feedback (82)