ValidationFx
Support This Project polski English
Using ValidationFx is trivially simple. Read the following text and learn how to easly and quickly use ValidationFx in your application.

1. Necessary files and installation

Of course, you must download the lastest version of ValidationFx. You can download by going to the download page. You need PostSharp 1.5 compiler also. You can download it from www.postsharp.org.
Copy ValidationFx.Weaver.dll and ValidationFx.Weaver.psplugin files plugins directory in PostSharp installation directory.

2. Project and references

Create a new project in your favourite development enviroment that using MSBuild building system like Visual Studio or SharpDevelop. Add a reference to ValidationFx.dll. Also add a referece to PostSharp.Public w that shoud be registred in Global Assembly Cache.

3. Test class

Create in your project the Test class. Create a standard property with proud name SomeNumber.
public class Test
{
	public int SomeNumber {get; set;}
}

4. First validation

Add necessary imports.
using ValidationFx;
using ValidationFx.Validators;
Mark SomeNumber property with LessThan attribute with parameter 10. The whole class mark with WeaveValidation attribute.
[WeaveValidation]
public class Test
{
	[LessThan(10)]
	public int SomeNumber {get; set;}
}
And that is it. You can test it now.

5. Sample test code

Test instance = new Test();
			
try
{
	instance.SomeNumber = 53;	
}
catch(Exception e)
{
	Console.WriteLine("Exception of type: {0}", e.GetType());
	Console.WriteLine("Exception message: {0}", e.Message);
	
	if(e.InnerException != null)
	{
		Console.WriteLine("Inner exception of type: {0}", 
                           e.InnerException.GetType());
		Console.WriteLine("Inner exception message: {0}", 
                           e.InnerException.Message);
	}
}
When you lauch the application, you can message like this:
Exception of type: System.ArgumentOutOfRangeException
Exception message: (Int32) SomeNumber: Passed value '53' must be less than '10'.

Inner exception of type: ValidationFx.Exceptions.NotLessThanException
Inner exception message: (Int32) SomeNumber: Passed value '53' must be less than '10'.

Validators can be combined

For example, if you want property to accept only numbers in range (0; 10):
[GreaterThan(0)]
[LessThan(10)]
public int SomeNumber {get; set;}
or
[Between(0, 10)]
public int SomeNumber {get; set;}

Strings are also easy to validate

This is e-mail address validation:
[NotNullOrEmpty]
[RegexValidator("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", RegexOptions.IgnoreCase)]
public string Email {get; set;}

Parameters validation

Example: param1 in range <0; 10> but not equal to 5; param2 is date not before 2009-10-01. You can code it in this way, because validators parameters are automatically converter to validating value type [if it possible].
public void SomeMethod([InRange(0, 10)] [NotEqual(5)] int param1, 
                       [GreaterOrEqual("2009-10-01 00:00")] DateTime param2)
{
	//some code
}

Lambda validators

[WeaveValidation]
public class Test
{
	static Test()
	{
		ValidationManager.AddValidator("Custom_SomeNumber_Validation_ID",
		                               x => (int)x.Value >= 1 
		                                    && (int)x.Value < 9
		                                    && (int)x.Value % 2 != 1);
	}
	
	[CustomValidation("Custom_SomeNumber_Validation_ID")]
	public int SomeNumber {get; set;}
	
...

Without using PostSharp you can also do it...

...but you need to prepend setter code with ValidationFx call.
private int _someNumber;
			
[Between(0, 10)]
[NotEqual(5)]
public int SomeNumber
{
	get 
	{
		return _someNumber;
	}
	
	set 
	{
		Validator.ValidateProperty(GetType(), "SomeNumber", this, value);
		_someNumber = value;
	}
}
public void SomeMethod([InRange(0, 10)] [NotEqual(5)] int param1, 
                       [GreaterOrEqual("2009-10-01 00:00")] DateTime param2)
{
	Validator.ValidateParameters(MethodInfo.GetCurrentMethod(), 
	                             this, new object[] {param1, param2});
	
	//some code
}
Now you know enought to using ValidationFx. You can directly go to the download section or request a support.
Get ValidationFx at SourceForge.net. Fast, secure and Free Open Source software downloads