Build enterprise application with WPF, WCF, Entity Framework and Prism. Tutorial 03. | Raffaeu’s blog

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.

What’s the difference? The first, the value validation should validate an entity against it’s value content. For example, the entity order should have a validation of type NotNull inside it’s Id field, or a LengthValidation inside it’s 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 don’t really like it. What’s a business validation? Let’s 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”.

Finally, my friend Simone Chiaretta, suggested me this open source framework: Fluent Validation, that has a nice procedural implementation:

Leave a comment

Your email address will not be published. Required fields are marked *