Almost every respectable application is build within in layered architecture. The most common layers are data access layer (DAL), business logic layer (BLL) and user interface layer (UI). When using the ASP.NET technology to create a user interface, the most common scenario to validate data is to drag some validator controls on the web page (f.e. RequiredFieldValidator) and bind them to the controls containing data that should be validated. But nowadays, when we speak about Service Oriented Architectures, your data could be exposed to other applications, which has different validation rules. So in order to protect your database from malicious input, it could be necessary to validate your business entities at business logic level before the data will be persisted to your data source.
I was looking for a solid base for my entity validation, without having to do a lot of coding. So recently I downloaded Microsoft Enterprise Library 3.0, which contains an entity validation block. To be able to run the code in this article, you need to download and install enterprise library 3.0. Also take a look at the prerequisites of the installation. In this example, I would like to show you how to create a base implementation of a validatable entity which can be reused through your domain logic.
First I created an interface IValidatableObject
public interface IValidatableObject
{
bool IsValid { get; }
}
The interface is the contract for every entity that should be validated. To create a generic base implementation, I created a new class which inherits from the interface:
First you have to make sure that you have a reference to the validation namespace:
using Microsoft.Practices.EnterpriseLibrary.Validation;
public class BusinessEntityBase<TEntity> : IValidatableObject
{
private ValidationResults _validationResults;
public BusinessEntityBase()
{
_validationResults = new ValidationResults();
}
public bool IsValid
{
get
{
//Create a validator using the validator factory
Validator validator = ValidationFactory.CreateValidator<TEntity>();
//Validate the entity based on the validation rules and put the result
//in the ValidationResults field
validator.Validate(this, _validationResults);
return _validationResults.IsValid;
}
}
public ValidationResults ValidationResults
{
get{return _validationResults;}
}
}
The next step is the implementation of your business entity itself. So every business entity in your domain logic that must be validatable, should inherit from base class
BusinessEntityBase. To add validation to the business entity, you have to decorate the properties with validator attributes. There are other ways to specify validation
rules (like an XML-configuration), but for now we are going to use attributes:
public class MyEntity : BusinessEntityBase<MyEntity>
{
private string _property1;
public MyEntity(){}
[StringLengthValidator(5, 10, MessageTemplate="The content of Property1 is not valid")]
public string Property1
{
get { return _property1; }
set { _property1 = value; }
}
}
Now we only have to do a simple test to see if the implementation really works. We expect that Property1 contains at least 5 characters and maximum 10 characters. The first assertion expects that the entity is not valid because it contains only 4 characters. The second assertion expects that the entity now is valid because contains 5 characters.
MyEntity target = new MyEntity();
target.Property1 = "1234";
Assert.IsFalse(target.IsValid);
target.Property1 = "12345";
Assert.IsTrue(target.IsValid);
In the future I will spend more time writing articles about entity validation. Enterprise Library 3.0 has built-in functionality to directly use entity validation in web forms and windows forms, on which I'll write some more articles about in the future..
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment