Showing posts with label Patterns and Practices. Show all posts
Showing posts with label Patterns and Practices. Show all posts
Monday, May 21, 2007
WCSF 1.1 release
WCSF is about to be released. The P&P team will start testing next week, so WCSF 1.1 is coming soon. Also interesting is the fact that WCSF 1.1 will be compatible with Enterprise Library 3.1. Things to look forward to...
Wednesday, May 16, 2007
WCSF and Enterprise Library 3.0
The current release of WCSF does not support Enterprise Library 3.0, but it is planned in the next upcoming release. For those of you who can't wait for the next release, there is workaround to compile WCSF with EntLib 3.0. Here you can read an excellent article about it.
Wednesday, May 9, 2007
Creating a simple generic base implementation of a validatable business entity using the Validation Application Block
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..
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..
Monday, April 23, 2007
WCSF Tip: Changing Module Assembly Names
There's something with the latest release of WCSF you definitely have to know when you try to change the assembly name of a module. This is something I came across too, and I also found out how to solve it. I was ready to blog about it, but someone else was ahead of me.... click here to go to the article.
Tuesday, April 17, 2007
WCSF Workshop
On March 12 & 13 the Patterns & Practices Team has organised a workshop about WCSF. They now have published their presentations on the internet and are free to download. Click here to go to the download page.
Subscribe to:
Posts (Atom)