Logo

NHibernate

The object-relational mapper for .NET

NHibernate Validator

Validation is one of those things that goes hand in hand with data access. I guess it is not much of surprise that one of the contrib projects for NHibernate is extensive validation support.

True, there are about as many validation frameworks as there are ToDo applications, but NHibernate Validator bring something special to the table, it brings tight integration with NHibernate itself and:

“…multi-layered data validation, where constraints are expressed in a single place and checked in various different layers of the application.”

I am sorry, I just love this quote. :-)

Anyway, let me jump right it and show you what I mean by that.

We can initialize the validation framework using several ways, but probably the easier would be:

var configuration = new Configuration()
	.Configure("hibernate.cfg.xml");

var engine = new ValidatorEngine();
engine.Configure(new NHVConfigurationBase());

ValidatorInitializer.Initialize(configuration, engine);

And now, all we need to do is set the validation attributes on our the entities, and we are done:

[NotNullNotEmpty]
[Length(25)]
public virtual string Title

At this point, several very interesting things are going to happen. First, if we ask NHibernate to generate the schema for us we are going to get the following:

Before using NHV After using NHV
create table Blogs (
  Id INT IDENTITY NOT NULL,
   Title NVARCHAR(255) null,
   Subtitle NVARCHAR(255) null,
   AllowsComments BIT null,
   CreatedAt DATETIME null,
   primary key (Id)
)
create table Blogs (
   Id INT IDENTITY NOT NULL,
   Title NVARCHAR(25) not null,
   Subtitle NVARCHAR(255) null,
   AllowsComments BIT null,
   CreatedAt DATETIME null,
   primary key (Id)
)

Note the title column, where before we used the default values (null and 255) we are now using the values defined in the validation scheme. That is what we mean when we say that we can get pretty multi layered data validation.

That is not the end of it, however, NHibernate Validator is hooking up into the NHibernate engine, and if we tried to save the following, we will get a validation exception:

s.Save(new Blog
{
	Title = new string('*',255),
});

And, obviously, we support a way to extract all the validation errors from the entity:

var invalidValues = engine.Validate(blog);
foreach (var invalidValue in invalidValues)
{
	Console.WriteLine(
        "{0}: {1}",
		invalidValue.PropertyName, 
		invalidValue.Message);
}

NHibernate Validator also support all the other things that you would expect from validation frameworks, the ability to create your own constraints (including the ability to embed them in the database schema!), i18n, XML only configuration, if you want to keep your entities clear of attributes, etc.

This has been truly just a tidbit, to whet your appetite.

You can learn more about NH Validator here.


Posted Thu, 30 April 2009 11:28:00 PM by Ayende
Filed under: validation Validator

comments powered by Disqus
© NHibernate Community 2016