Value and business validation with Enterprise Library 4.1 and Entity Framework.
In this article I will show you how to validate a domain using the enterprise library 4.1 over the Entity Framework.
First of all I want to talk about the validation and the thousands of implementations you can find over the web. In my opinion there are 2 different types of validation. The value validation and the business validation.
Whats the difference? The first, the value validation should validate an entity against its value content. For example, the entity order should have a validation of type NotNull inside its Id field, or a LengthValidation inside its order number, in order to reflect the corresponding field in the database table. In this way we will execute a value validation in our domain model, before sending the data to the Data Access Layer.
The second validation is the business validation and it can be accomplished with a lot of different ways. The most common is the hard coded way that personally, I dont really like it. Whats a business validation? Lets say we have an order entity and we have a rule that says if the total order is grater than 1,000 $ apply 10% of discount. This is a business rule that force our entity to change its value after some business considerations.
Available validation framework for NET.
I use for my value validation the Enterprise Library Validation block and I have found it really useful. The only problem is that this framework forces you to use the decorator pattern, so we hard code our entity with the value validation rules. This approach is fine, but only if related to the values. You can also include the value validation in a separate XML file, and this is the solution we will use later.
Another interesting framework is the Validation Framework project on Codeplex, that similar to the EL, uses decorator and generics to validate our entities.
Very powerful but with an high learning curve is the powerful SPRING.NET framework, that inside its application blocks has a space also for the validation. What I really like about SPRING.NET is the fact that you will not hard code anything because the entire validation is in a separate XML file. But remember that the learning curve is pretty complex because SPRING.NET is a full IoC tool, so everything is under the concept of be Injected.