Common Library - Validation Scope

ValidationScope is used to collect all errors of input and coordinate validation logic across independent APIs. It's thread safe. It implements the interface IDisposable that you can initialize a scope by using block. And in a block, you can add errors into a validation context if there are. The block disposal throws an exception with all errors by checking validation context. A validation scope and its nested scopes share the same validation context so that you're possible to write independent APIs with great coordination on validation.

Project: BaoJianSoft.Common
Namespace: BaoJianSoft.Common.Validation

There are four constructors for ValidationScope,
  1. ValidationScope()
  2. ValidationScope(Type thrownExceptionType)
  3. ValidationScope(bool forceToThrowExceptionIfHasErrorsWhenDisposing)
  4. ValidationScope(bool forceToThrowExceptionIfHasErrorsWhenDisposing, Type thrownExceptionType)

Parameters:
  • thrownExceptionType is to specify type of exceptions which is thrown in disposal if there are errors logged. By default, BaoJianSoft.Common.Validation.ValidationException is thrown.
  • forceToThrowExceptionIfHasErrorsWhenDisposing is True to indicate that the disposal for nested validation scope throws an exception directly if there are errors. The ancestor validation scope catches and integrates all errors from its nested scope with it equals to False.

As a simple sample using ValidationScope as below. An InvalidOperationException instance with 2 error messages is thrown in disposal of this using block. If you don't specify typeof(InvalidOperationException) explicitly, a ValidationException instance is thrown instead.
using (ValidationScope scope = new ValidationScope(typeof(InvalidOperationException)))
{
    if (true)
        scope.Error("Name cannot be empty!");
    if (true)
        scope.Error("Password cannot be empty!");
}

As a nested validation scope sample as following. You can imagine that the NestedMethod1 and NestedMethod2 are from independent APIs. The ancestor scope disposal throws a validation exception with all 5 error messages in this case. But if you construct the first nested validation scope using 3rd/4th constructor with forceToThrowExceptionIfHasErrorsWhenDisposing to True, the thrown exception only contains top 3 error messages.
public void NestedValidationScopeTest()
{
    using (ValidationScope scope = new ValidationScope())
    {
        scope.Error("Error 1");

        NestedMethod1();
        NestedMethod2();
    }
}

private void NestedMethod1()
{
    using (ValidationScope scope = new ValidationScope())
    {
        scope.Error("Error 2");
        scope.Error("Error 3");
    }
}

private void NestedMethod2()
{
    using (ValidationScope scope = new ValidationScope())
    {
        scope.Error("Error 4");
        scope.Error("Error 5");
    }
}

You can also call method Throw() of validation scope which throws an exception for current validation scope immediately whatever in nested or ancestor scope if there are errors logged.

Last edited Jan 13, 2010 at 2:24 PM by eungeliu, version 1

Comments

No comments yet.