Search code examples
linquuiddb4o

db4o, Linq, and UUID's


Apparently the db4o website was recently redone, and now old urls are giving 404 errors. Everytime I think I've found the answer, I get a 404 error.

I have a simple db4o database I've setup to store people.

public class Person
{
     public string Firstname { get; set;}
     public string Lastname {get;set;}
}

I've been able to run Linq queries on the database, and everything works wonderfully. I'm programming in a web environment, so I will need someone to identify and fetch unique objects from the database, so I simply configured the database to use UUID's. Now the problem has become, how can I get to the UUID information from the objects I get from the Linq query?

For example, let's say I get all the people stored in the database, and I need to generate the unique URL's for each person. I will use the UUID to do so. So I'll run this:

var people = (from Person p in db select p).ToList();

And then I'll iterate through the list

foreach( Person person in people)
{
  DoSomething(person);
}

The best solution would be making the UUID a property on the Person class, so that I could just:

var uuid = person.UUID;

I don't see a mechanism for doing that however. Am I missing something?


Solution

  • I've answered this already here but together with other question. Therefore I answer it again: In db4o you have normally no id. db4o uses the object-identity to distinguish the object apart. So the same object in memory is going to be the same object for the database.

    As long a you don't serialize object this works fine, you don't need any ID. However as soon as objects are serialized / disconnected this doesn't work anymore (Typical for web-apps).

    I think this three options are possible:

    • Use the db4o internal id. You can get this id with IObjectContainer.Ext().GetID(object). And of course you can get a object by id: IObjectContainer.Ext().GetByID(id). However this id isn't forever. Defragmenting the database changes this id.
    • Using db4o's UUIDs. But db4o UUIDs are quite large
    • Creating ids by yourself. For example using a GUID. Or using a clever id-generator. Remember to index such a id-field when you use it in queries.

    Of course you can create a base-class which provides the id-property with any of the implementations above.