Archive for March, 2008
NHibernate 2.0.0Alpha1 released
Posted by Dario in NHibernate on March 31st, 2008
Here is the link to download:
Db4o – Field Generation support
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 ?
[Download the example]
And apply this patch at the revision 10450 in order to see the changes on the code.