Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior .Net Consultant and Architect at Sela Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft. Gil is a DZone MVB and is not an employee of DZone and has posted 98 posts at DZone. You can read more from them at their website. View Full User Profile

Self Validation in Validation Application Block

03.26.2010
Email
Views: 3237
  • submit to reddit

There are times 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?Self Validation in Validation Application Block

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.

 

References
Published at DZone with permission of Gil Fink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)