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 ?
I think the field generation in Db4o could be a cool feature. When I talk about field generation is a kind of interception before save/update on an object letting to a generator set a value on a specified field.
The field generation is not a primary key generation! Maybe, this feature may able make more easier the implementation of an Auto-Increment field generation for example.
Like an exercise, and to learn about the inside-of-Db4o I’ve implemented this feature in an a local working copy. Let see some code and how to use it.
First, to be able to create field generators you have to implement your own generator via the interface IFieldGenerator. And this is a simple example of an generator: a Guid generator.
Now, lets see the objects where the generation should occur. We have an hierarchy of Animal <- Dog.
Now we want to inject a value into the field id before an object-Insert. For make this happen we must to configured Db4o with a code like this:
And that’s all. Before of an Insert, the object will be injected with a new Guid value. Lets see the code in action in a test method:
An interesting point here is the polimorphistic behavior: we have configured the Animal class, and later the field is injected in a instance of a Dog class.
You can configure this feature using attributes/annotations. In order to do that you on have to denote the class with the attribute GeneratorAttribute. And this attribute receive the parameter of the generator.
GeneratorAttribute make the magic because it just inherit from IDb4oAttribute interface, and later via the ConfigurationIntrospector we configure Db4o as usual. Cool isn’t ?
And apply this patch at the revision 10450 in order to see the changes on the code.