DCSIMG
Self Validation in Validation Application Block - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Self Validation in Validation Application Block

Self Validation in Validation Application Block

There are timesSelf Validation in Validation Application Block
when you don’t
find a solution for
a validation you
need inside
Validation Application
Block
validators.
When this is happening Self Validation
can help you to keep using VAB and gaining
all its abilities. This post will explain how to use
Self Validation in VAB.

What is Self Validation?

Self validation gives you the ability to implement validation logic
inside the class you want to validate. When you have a
complicated validation or your validation logic can’t be performed 
using the VAB validators you can apply this method.

How to use Self Validation?

The first thing to do when you want to indicate that a class has
self validation is to decorate the class with a HasSelfValidation
attribute. Then you decorate every self validation method with
the SelfValidation attribute. The method signature need to be
void and take only one instance of ValidationResults as parameter.
This collection will be updated inside the method if the validation
failed.
For example the following class uses self validation:

using System;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary.Validation;
 
namespace Search   
{   
    [HasSelfValidation]
    public class Search
    {
        #region Properties
 
        public int Sum { get; set; }
 
        #endregion
 
        #region Methods
        
        [SelfValidation]
        public void CheckSum(ValidationResults results)
        {
            if (Sum < 0)
            {
                results.AddResult(new ValidationResult("Must have positive sum",  
                    this, "Positive", null, null));
            }
        }
 
        #endregion
    }
}

As you can see this is only an example and I could have used the
regular VAB validators to achieve the same impact.

The SelfValidation attribute can also be used within a specific
rule set by providing to it the rule set name as parameter.
When we want to use the self validation logic we use it as we
used all the other validators in VAB.
For example:

var search = new Search();
var validator = ValidationFactory.CreateValidator<Search>();
var results = validator.Validate(search);
 

Summary

I use a lot the the Validation Application Block as a validation
framework. When I get stuck with complicated validations or
the supplied validators can’t help me then I use the self
validation
feature. It is very simple and easy to apply.
In the post I showed how to use it in code. I hope you will
find it helpful.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# March 25, 2010 10:56 AM

Twitter Trackbacks for Self Validation in Validation Application Block - Gil Fink on .Net [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Self Validation in Validation Application Block - Gil Fink on .Net         [microsoft.co.il]        on Topsy.com

# March 27, 2010 11:35 PM