Write the validation block - Validation block tutorial 3/4
Here i show you another example to use the validation application block
I’ll explain a little bit detail bellow the example
-
using Common;
-
using Microsoft.Practices.EnterpriseLibrary.Validation;
-
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Text.RegularExpressions;
-
-
namespace net.de.common.security.access.model
-
{
-
///
-
/// A property class that represent the User entity
-
///
-
public class User
-
{
-
private long userId;
-
private string username;
-
private string password;
-
private string retypePassword;
-
private string passwordHint;
-
private string firstName;
-
private string lastName;
-
private DateTime birthday;
-
private string email;
-
private string phoneNumber;
-
private string mobilePhoneNumber;
-
private Address address;
-
private string userPic;
-
private bool enabled;
-
private bool accountExpired;
-
private bool accountLocked;
-
private bool credentialExpired;
-
-
public long UserId {
-
get { return userId; }
-
set { this.userId = value; }
-
}
-
-
[ValidatorComposition(CompositionType.And, MessageTemplateResourceName = "MsgEmptyUsername", MessageTemplateResourceType = typeof(CommonResources))]
-
[NotNullValidator]
-
[StringLengthValidator(6, 20)]
-
public string Username {
-
get { return username; }
-
set { this.username = value; }
-
}
-
-
[ValidatorComposition(CompositionType.And, MessageTemplateResourceName = "MessageEmptyPassword", MessageTemplateResourceType = typeof(CommonResources))]
-
[NotNullValidator]
-
[StringLengthValidator(6, 20)]
-
public string Password {
-
get { return password; }
-
set { this.password = value; }
-
}
-
-
[PropertyComparisonValidator("Password", ComparisonOperator.Equal, MessageTemplateResourceName = "MsgInvalidPassword", MessageTemplateResourceType = typeof(CommonResources))]
-
public string RetypePassword {
-
get { return retypePassword; }
-
set { this.retypePassword = value; }
-
}
-
-
public string PasswordHint {
-
get { return passwordHint; }
-
set { this.passwordHint = value; }
-
}
-
-
[NotNullValidator(MessageTemplateResourceName = "MsgEmptyFirstName", MessageTemplateResourceType = typeof(CommonResources))]
-
public string FirstName {
-
get { return firstName; }
-
set { this.firstName = value; }
-
}
-
-
public string LastName {
-
get { return lastName; }
-
set { this.lastName = value; }
-
}
-
-
[RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, MessageTemplateResourceName = "MsgInvalidAge", MessageTemplateResourceType = typeof(CommonResources))]
-
public DateTime Birthday {
-
get { return birthday; }
-
set { this.birthday = value; }
-
}
-
-
[RegexValidator(@”\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplateResourceName = "MsgInvalidEmail", MessageTemplateResourceType = typeof(CommonResources))]
-
public string Email {
-
get { return email; }
-
set { this.email = value; }
-
}
-
-
[ValidatorComposition(CompositionType.Or, MessageTemplateResourceName = "MsgInvalidPhoneNumber", MessageTemplateResourceType = typeof(CommonResources))]
-
[StringLengthValidator(0)]
-
[RegexValidator(@”(\+)?\b\d+\b")]
-
public string PhoneNumber {
-
get { return phoneNumber; }
-
set { this.phoneNumber = value; }
-
}
-
-
[ValidatorComposition(CompositionType.Or, MessageTemplateResourceName = "MsgInvalidMobilePhoneNumber", MessageTemplateResourceType = typeof(CommonResources))]
-
[StringLengthValidator(0)]
-
[RegexValidator(@”(\+)?\b\d{7,15}\b")]
-
public string MobilePhoneNumber {
-
get { return mobilePhoneNumber; }
-
set { this.mobilePhoneNumber = value; }
-
}
-
-
[NotNullValidator]
-
[ObjectValidator]
-
public Address Address {
-
get { return address; }
-
set { this.address = value; }
-
}
-
-
public string UserPic {
-
get { return userPic; }
-
set { this.userPic = value; }
-
}
-
-
public bool Enabled {
-
get { return enabled; }
-
set { this.enabled = value; }
-
}
-
-
public bool AccountExpired {
-
get { return accountExpired; }
-
set { this.accountExpired = value; }
-
}
-
-
public bool AccountLocked {
-
get { return accountLocked; }
-
set { this.accountLocked = value; }
-
}
-
-
public bool CredentialExpired {
-
get { return credentialExpired; }
-
set { this.credentialExpired = value; }
-
}
-
-
}
-
}
And here is the Address.cs class
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using Microsoft.Practices.EnterpriseLibrary.Validation;
-
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
-
using Common;
-
-
namespace net.de.common.security.access.model
-
{
-
///
-
/// A property class that represent the common address entity
-
///
-
public class Address
-
{
-
private string line1;
-
private string line2;
-
private string city;
-
private string state;
-
private string postCode;
-
private string country;
-
-
[StringLengthValidator(5, 50, MessageTemplateResourceName = "MsgInvalidLine1", MessageTemplateResourceType = typeof(CommonResources))]
-
public string Line1 {
-
get { return line1; }
-
set { line1 = value; }
-
}
-
-
[IgnoreNulls]
-
[ValidatorComposition(CompositionType.Or, MessageTemplateResourceName = "MsgInvalidLine2", MessageTemplateResourceType = typeof(CommonResources))]
-
[StringLengthValidator(0)]
-
[StringLengthValidator(5, 50)]
-
public string Line2 {
-
get { return line2; }
-
set { line2 = value; }
-
}
-
-
[StringLengthValidator(1, 50, MessageTemplateResourceName = "MsgInvalidCity", MessageTemplateResourceType = typeof(CommonResources))]
-
public string City {
-
get { return city; }
-
set { city = value; }
-
}
-
-
[StringLengthValidator(1, 50, MessageTemplateResourceName = "MsgInvalidState", MessageTemplateResourceType = typeof(CommonResources))]
-
public string State {
-
get { return state; }
-
set { state = value; }
-
}
-
-
[RegexValidator(@”\b\d+\b", MessageTemplateResourceName = "MsgInvalidPostCode", MessageTemplateResourceType = typeof(CommonResources))]
-
public string PostCode {
-
get { return postCode; }
-
set { postCode = value; }
-
}
-
-
public string Country {
-
get { return country; }
-
set { country = value; }
-
}
-
-
}
-
}
you can see from the above example there is a few validators used to validate the User.cs and Address.cs classes.
for the Username and Password property i use the Not null validator, String length validator wrapped by And composite validator.
The String length validator check the range between 6-20 character length, And Composite Validator use the message from the project resource. So it must specified the MessageTemplateResourceName as the name of the message in the resource and also specify the MessageTemplateResourceType as the type of the resource name.
-
[ValidatorComposition(CompositionType.And, MessageTemplateResourceName = "MsgEmptyUsername", MessageTemplateResourceType = typeof(CommonResources))]
-
[NotNullValidator]
-
[StringLengthValidator(6, 20)]
-
public string Username {
-
get { return username; }
-
set { this.username = value; }
-
}
The RetypePassword property use the PropertyComparisonValidator to check the value that it is equal with the Password property. The PropertyComparisonValidator take 4 parameters in the example above. 1st is the property name to be compared, 2nd the method used to compare. 3rd is the MessageTemplateResourceName and the last parameter is MessageTemplateResourceType
-
[PropertyComparisonValidator("Password", ComparisonOperator.Equal, MessageTemplateResourceName = "MsgInvalidPassword", MessageTemplateResourceType = typeof(CommonResources))]
-
public string RetypePassword {
-
get { return retypePassword; }
-
set { this.retypePassword = value; }
-
}
The following code snippet attaches the RelativeDateTimeValidator attribute to the Birthday property and checks to see if the user is 18 years or older.
-
[RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, MessageTemplateResourceName = "MsgInvalidAge", MessageTemplateResourceType = typeof(CommonResources))]
-
public DateTime Birthday {
-
get { return birthday; }
-
set { this.birthday = value; }
-
}
The following code example checks that the e-mail address is formed according to the pattern given by the regular expression.
-
[RegexValidator(@”\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplateResourceName = "MsgInvalidEmail", MessageTemplateResourceType = typeof(CommonResources))]
-
public string Email {
-
get { return email; }
-
set { this.email = value; }
-
}
The following code example use the Or composite validator, String length validator and also Regular Expression Validator to checks that the given phoneNumber is formed according to the pattern given by the regular expression.
-
[ValidatorComposition(CompositionType.Or, MessageTemplateResourceName = "MsgInvalidPhoneNumber", MessageTemplateResourceType = typeof(CommonResources))]
-
[StringLengthValidator(0)]
-
[RegexValidator(@”(\+)?\b\d+\b")]
-
public string PhoneNumber {
-
get { return phoneNumber; }
-
set { this.phoneNumber = value; }
-
}
To perform the validation on the User object, you have to create the Validator object then call the validate method on the user object.
here is the example :
-
Validator validator = ValidationFactory.CreateValidator();
-
ValidationResults results = validator.Validate(user);
You can download the complete example of Enterprise Library Validation application block here.
ValidationExample.rar
Leave a Reply