Integrating NHibernate.Validator with Db4o


For who isn’t familiar with NHibernate Validator: it’s a cool framework to validate objects anywhere at our applications. That’s it.

Now, how we can integrate this tool in order to validate the objects before save/update at Db4o? The answer is: very easy. You can download the code at the end of the post.

Remember you can configure NHibernate Validator using .Net Attributes or Xml files. In this case we will se how to do it using the first approach. The class Customer should look like this:

Now how to make the integration using a little helper class ? Like this:

As you see, with a little helper class we can configure the integration with only one line of code, very easy. In this line the method Initialize register before events on the Save/Update at the Db4o Core.

Now, let’s see the validation in action, the below example show the application trying to save an object with an invalid state:

So when the Save/Update is going to happen, first of all, NHibernate Validator check if the entity is into a valid state, if it’s not, an InvalidStateException is thrown, then Db4o wrap it with an EventException, so we have to unwrap the exception to recover the invalid values to display to the user. Would be nice that Db4o don’t wrap the exceptions, but this topic was discussed on this thread.

So, was easy to integrate NHibernate Validator with Db4o. Wasn’t it ?

Download the Example

  1. No comments yet.
(will not be published)

  1. No trackbacks yet.