Click here to Skip to main content
Click here to Skip to main content

A Nice Way to Collect All Input Errors in a Submit and Feedback to the User at a Time

By , 2 Mar 2010
 

Introduction

ValidationScope is used to collect all input errors in a context although validation code exists in independent APIs. We don't need to return error messages and throw exceptions explicitly. It like transaction context is managed by TransactionScope and we don't need to transmit transaction instances. It's thread safe.

ValidationScope implements the interface IDisposable that we can initialize a scope by using block. And in a block, you can add errors into the scope that the using block disposal throws an exception with all added errors if there is one. And ValidationScope supports nest. A validation scope and its nested scopes share the same validation context so that it is possible to write independent APIs with great coordination on validation.

Background

ValidationScope is included in CommonLibrary of RapidWebDev. RapidWebDev is an infrastructure that helps engineers to develop enterprise software solutions in Microsoft .NET easily and productively. It consists of an extendable and maintainable web system architecture with a suite of generic business model, APIs and services as fundamental functionalities needed in development for almost all business solutions. So when engineers develop solutions in RapidWebDev, they can have a lot of reusable and ready things, then they can focus more on business logics implementation. In practice, we can save more than 50% time on developing a high quality and performance business solution than traditional ASP.NET development.

Source Code

The source code of the implementation, sample and test cases can be found in RapidWebDev source package which you can download from the official website.

Using the Code

  • Project: RapidWebDev.Common
  • Namespace: RapidWebDev.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 are 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.

A simple sample using ValidationScope is 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!");
}

A nested validation scope sample is as follows. 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 the 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 in nested or ancestor scope if there are errors logged.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Eunge
President TCWH
China China
Member
I've worked as a software architect and developer based on Microsoft .NET framework and web technology since 2002, having rich experience on SaaS, multiple-tier web system and website development. I've devoted into open source project development since 2003, and have created 3 main projects including DataQuicker (ORM), ExcelQuicker (Excel Report Generator), and RapidWebDev (Enterprise-level CMS)
 
I worked in BenQ (8 mo), Bleum (4 mo), Newegg (1 mo), Microsoft (3 yr) and Autodesk (5 yr) before 2012. Now I own a startup company in Shanghai China to deliver the exceptional results to global internet users by leveraging the power of Internet and the local excellence.
SearchBestHosting.com and LinkedHosts.com are two main sites owned by me, designed to help people find the best web hosting deal at the reasonable price for their web presence, based on our real editorial experience and real customer voice. So far we have reviewed over 100 web hosts based on our true experience and the real customer reviews. If you're looking for a budget and reliable web host, you shall check out SearchBestHosting.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 2 Mar 2010
Article Copyright 2010 by Eunge
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid