Now is the time to delve a bit more into the libraries that lie at the core of our factory. On top of them lie Data Rules.

First of all, note that this library owes a lot to the work of Paul Stovell, and that most of the concepts discussed here can be found in this article of his.

So Data Rules is a validation framework. The goal here is to handle invalid data in your custom types a bit like DataSet does : instead of throwing an exception every time invalid data is affected to it, it records whether the data it is containing is valid or not; and if not, why. This allows for much smoother UI integration (that is what ErrorProvider and IDataErrorInfo are all about).

This library is organized around 2 interfaces :Basics

  • IValidatable is here to help the developer define a custom type that has such a behavior. A validatable type is one for which any instance can be validated against a specified set of rules. In Data Access, our business objects are validatable : you can always know whether data is valid or not, and why. And if you try to persist an invalid business object, you will get an exception.
  • IRule is an interface implemented by a validation rule. A rule can be specific to a property of your custom type (is it null ?), or not (for rules that involve many properties for instance). The description of a rule contains a message that can be displayed to the end user (like "This field is required").

The library predefines a set of useful rules (like NotNullRule, StringLengthRule, RegexRule...). These rules can also be specified as attributes. And there is an adapter that allows to integrate the Enterprise Library Validation Application Block in Data Rules.

As previously stated, Data Rules is used by Data Access to add validation capability to our business objects. It is also used by Data Activities to enforce business rules in our data flow.

In the future, we can imagine to create our DSL to design our rules, and why not store some of them in a configuration file.

Next time, I will try to summarize the vast subject of Data Access.