Enterprise Library 3.0 validation

I have tested the enterprise library 3.0 validation application block and i found that it is very useful in enforcing the valid data entered before any form processing. It is flexible which you can create your own custom validator and you also can reuse the out of the box validator that includes RegexValidator, RangeValidator, StringLengthValidator and etc.

I strongly suggest you to use it if you are building 3-tier enterprise application because it really save you time in form validation and the convenience that it brings.

3 things you need to do:
1-first you need to create a bussiness entity
2-add the enterprise library references
3-call the business entity from the form.




using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using Microsoft.Practices.EnterpriseLibrary;
using WindowsApplication5.CustomValidator;
namespace WindowsApplication5.BE
{
public class User
{
private const double _DefaultAmount = -1;
public User(){
}
private string _Email;
[RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate="Invalid e-mail address", Ruleset = "RuleSetA")]
public string Email
{
get { return _Email; }
set { _Email = value; }
}
private double _Amount;
[ValidatorComposition(CompositionType.And , Ruleset = "RuleSetA", MessageTemplate = "amount must be smaller then 6000, or larger t")]
[LoanValidator(_DefaultAmount,MessageTemplate="amount is larger the 6000", Ruleset = "RuleSetA")]
[LoanValidator(3000,MessageTemplate="amount is more then 3000", Ruleset = "RuleSetA")]
public double Amount
{
get { return _Amount; }
set { _Amount = value; }
}
private DateTime _Birthdate;
[RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, Ruleset="RuleSetA", MessageTemplate="Must be 18 years or older.")]
public DateTime Birthdate
{
get { return _Birthdate; }
set { _Birthdate = value; }
}
}
}

//initialize to bring user entered dato to the business entities
try{
User user = new User();
user.Birthdate = Convert.ToDateTime(txtBirthday.Text);
user.Email = txtEMail.Text;
user.Amount = Convert.ToDouble( txtLoanAmount.Text);
if (user != null)
{
Validator validator = ValidationFactory.CreateValidator("RuleSetA");
ValidationResults results = validator.Validate(user);
DisplayValidationResults(results, customerResultsTreeView);
}
}catch(Exception ex)
{
MessageBox.Show(ex.Message );
}

Download Sample

Comments

Anonymous said…
Hello !.
You re, I guess , perhaps curious to know how one can manage to receive high yields .
There is no need to invest much at first. You may start to get income with as small sum of money as 20-100 dollars.

AimTrust is what you thought of all the time
AimTrust represents an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

Its head office is in Panama with structures everywhere: In USA, Canada, Cyprus.
Do you want to become an affluent person?
That`s your chance That`s what you really need!

I`m happy and lucky, I started to take up income with the help of this company,
and I invite you to do the same. It`s all about how to choose a correct companion who uses your money in a right way - that`s the AimTrust!.
I earn US$2,000 per day, and what I started with was a funny sum of 500 bucks!
It`s easy to join , just click this link http://ulabusuhu.virtue.nu/utybev.html
and go! Let`s take our chance together to get rid of nastiness of the life

Popular posts from this blog

How to get my motherboard serial number using C#?

Giant Supermarket in Malaysia is unreliable - Prices always incorrect equals being cheated

a simple way to manipulate data using sql