views:

104

answers:

2

I'm playing around with building a new web application using DB4O - piles of fun and some really interesting stuff learned. The one thing I'm struggling with is DB4O's current lack of support for stateless applications (i.e. web apps, mostly) and the need for automatically-generated IDs.

There are a number of creative and interesting approaches that I've been able to find that hook into DB4O's events, use GUIDs rather than numeric IDs or for whatever reason avoid using any system of ID at all.

While each approach has its merits, I'm wondering if the less-elegant approach might equally be the best fit. Consider the following pseudo-code:

If ID == 0 or null
Set ID = (typeof(myObject)).Count
myObject.Save

It seems like such a blindingly simple approach, it's usually about here that I start thinking, "I've missed something really obvious". Have I?

+1  A: 

you need to lock inserts and deletes of myObject before the count to until after the save

drscroogemcduck
Ah. That would be the obvious bit I was missing. Thanks.
Phil.Wheeler
+1  A: 

The new db4o-extras project contains an AutoIncrementID support add-in. The project was just started, and I havn't yet posted a compiled binary. But it adds support for "Identity" columns or an auto-incremented ID field/property through the use of a single attribute.

[AutoIncrement]
public property int ID {get; private set;}

[AutoIncrement]
protected int _id;
public property int ID {
  get{return _id;}
  set{this._id = value;}
}
Eric Falsken