VAB Contributions

This section presents the contributions for the Validation Application Block.

ArgumentValidation
ArgumentValidationException
CompositeRulesetValidator
DefaultValidators
ExternallyConfigurableObjectValidator
CollectionCountValidator
TypeValidator
ObjectValidator
EnumDefinedValidator

ArgumentValidation

The ArgumentValidation class can be used to validate pre/post conditions of method calls. It throws a new ArgumentValidationException if the validation fails.

ArgumentValidation is meant to be used as an alternative to argument validation attributes: often, it is not possible, or just too much of an overhead, to use the Policy Injection Application Block to enable validation of method arguments.

public void MyMethod(string myParam)
{
  ArgumentValidation.Validate("myParam", myParam, DefaultValidators.NotNullValidator);
  // …
}

The method takes a variable number of arguments, so you can also automatically combine validators in a single call.

public void MyMethod(string myParam)
{
  ArgumentValidation.Validate("myParam", myParam, DefaultValidators.NotNullValidator, new StringLengthValidator(10));
  // …
}

ArgumentValidationException

The ArgumentValidationException class is similar to Enterprise Library Policy Injection Application Block’s ArgumentValidationException, but provides safer behaviors by making it optional for the exception to propagate the tested value.

CompositeRulesetValidator

Allows for composition of multiple rulesets

DefaultValidators

The DefaultValidators class provides pre-allocated validators. Use them instead of instantiating new ones every time you need common validators.

ExternallyConfigurableObjectValidator

The ExternallyConfigurableObjectValidator is an ObjectValidator that allows to use configuration from a configuration file other than web.config or app.config.

CollectionCountValidator

The CollectionCountValidator class works like the StringLengthValidator, but for collections. It can be used to validate the number of elements in a collection.

// Given that "myCollection" is an ICollection, validate that it contains between 1 and 10 items.
CollectionCountValidator validator = new CollectionCountValidator(1, 10);
ValidationResults results = Validator.Validate(myCollection);

TypeValidator<T>

The TypeValidator<T> generic is used to validate that a variable is of a given type.

// Given that "myVariable" is an object reference, validate that we can dispose it.
TypeValidator<IDisposable> validator = new TypeValidator<IDisposable>();
ValidationResults results = validator.Validate(myVariable);
if (results.IsValid)
	((IDisposable)myVariable).Dispose();

ObjectValidator<T>

Same as Enterprise Library’s ObjectValidator, but as a generic.

EnumDefinedValidator

The EnumDefinedValidator returns whether the value of an enum corresponds to a value in its definition. For example, consider the following enum definition:
public enum Bar
{
    A = 0,
    B = 1
}

When validating an instance of Bar with the EnumDefinedValidator, validation will succeed if the value is 0 or 1, and will fail if it contains any other value.

Last edited Dec 22, 2009 at 8:48 PM by ewdev, version 2

Comments

No comments yet.