Did you know about db4o – a brilliant object oriented database
January 27, 2010

One of my habits is making utilities app with C#.Net. I made a lot of small apps and a lot of them will require some sort of local database. Normally I’m just using XML serialization. It’s a quick solution, easy to implement and you could read the XML file and change it with just the notepad. However, I’m planning on a middle-size project lately. It does not require a complex structure but the amount of data is kind of big so XML is not really suitable anymore. So I’m on the search for a easy to use local database. It’s when I found “db4objects”.
It’s an object oriented database. It’s so simple, so easy to learn, well, you nearly don’t need to remember anything new. And guess what, it’s free and open source as well Db4o is free if your application is under GPL.
However I didn’t know about this database until now, so I think a lot of developers wouldn’t know as well. So here is just a bit taste of db4o. You could download my demo and play around with it. You don’t nead to set up anything, only need to add reference to 2 dlls file from db4objects.
db4o Demonstration (373)You could just head to its website to download the thing back and play with it. Their document are quite easy to follow as well.
What is db4o
Here is a quick defination from their website
db4o is the open source object-oriented database that enables Java and .NET developers to store and retrieve any application object with only one line of code, eliminating the need to predefine or maintain a separate, rigid data model.
Take a look at it in action
So I prepared this structure for this post. it contains a simple structure which includes a few different problems type: Inherition, Array, List, Guid object…
All the db4o database is controlled through an IObjectContainer object. So we declare a IObjectContainer variable and use it through our application.
IObjectContainer db;
First thing first, open the db. If the file isn’t existing, it creates a new one.
db = Db4oFactory.OpenFile("dat.db4o");
Now let’s work with it. It may be a bit strange if you haven’t work with OO DB before. But say we create a new product.
Product newProd = new Product(Guid.NewGuid(), "Test Product " + DateTime.Now.ToString("hhmmss"), 100);
newProd.Images = new string[]{"Image 1", "Image 2", "Image 3"};
Store data
We will store this object information to our database file. Just a simple command.
db.Store(newProd); db.Commit();
That’s it. The whole object’s in your database file now. When you get it out, you will get the whole object, not just the information inside it. You don’t need to get the information out and put it in the object as you do with the SQL.
Ok, let’s get all the Product objects out.
IListallProducts = db.Query (delegate(Product iteProduct) { return true; });
Retreive data
Now allProducts store the list of all the product object in the database.
I’ll print it out a text box.
StringBuilder result = new StringBuilder();
result.AppendLine("> Products List");
foreach (Product prod in allProducts)
{
result.AppendLine(prod.ToString());
foreach (string imgStr in prod.Images)
{
result.Append(" - ").Append(imgStr);
}
result.AppendLine();
}
result.AppendLine();
txtResult.Text = result.ToString() + txtResult.Text;
Store data with many different layers
Start with something more complex. We create a Category class with a list of product objects.
Category newCat = new Category(Guid.NewGuid(), "Test Cat " + DateTime.Now.ToString("hhmmss"));
newCat.Products.Add(new Product(Guid.NewGuid(), "Test prod for Cat " + DateTime.Now.ToString("hhmmss") + newCat.Id.ToString(), 100));
newCat.Products.Add(new Product(Guid.NewGuid(), "Test prod for Cat " + DateTime.Now.ToString("hhmmss") + newCat.Id.ToString(), 100));
db.Store(newCat);
Then we do the same to show the list of all category objects in the database with the products inside it.
Now if you show the list of products. You will see our new products in the category are also there as well.
Do the same with Store object so we have 3 three layers of data
Store newStore = new Store(Guid.NewGuid(), "Test Store " + DateTime.Now.ToString("hhmmss"));
newStore.Categories.Add(new Category(Guid.NewGuid(), "Test cat for Store " + DateTime.Now.ToString("hhmmss") + newStore.Id.ToString()));
newStore.Categories[0].Products.Add(new Product(Guid.NewGuid(), "Test prod for Cat " + DateTime.Now.ToString("hhmmss"), 100));
db.Store(newStore);
And it’s return this in the result
> Stores List 45024d - Test Store 015153 +1830d6 - Test cat for Store 01515345024d2f-5caa-42d1-bd79-e1c5d59bdcf9 - 33a7d0 - Test prod for Cat 015153 - 100
So it’s still fine.
Query
Now instead of get the whole list, let’s just get the result base on some rule. This codes bellow will get all the categories which have more than 1 product.
IListallCats = db.Query (delegate(Category iteCategory) { return iteCategory.Products.Count > 1; });
Want more condition, this time we get a list of product with more than 1 product and contain the word ‘Test’ in the name. A piece of cake.
IListallCats = db.Query (delegate(Category iteCategory) { bool condResult; condResult = (iteCategory.Name.Contains("Test") && (iteCategory.Products.Count > 1)); return condResult; });
Conclusion
Ok, it’s generally how the db4o work. I’m not gonna write more about this ‘cos db4o website has complete tutorial to working on matter. It’s so clear that’s I don’t think I could do better than them. Also, the system is so simple to use, not much I could wrote about anyways. Here is just quick article to let you know how this thing works. How brilliant is it. Head to their website site for more information.
Update
As Joan-Carles Vilaseca query about what will happen when you add new class member. It’s actually quite simple. After you add 1 member to the class, all the old objects in the database will have that new member and the value is the default value of that new member. Say if you add a new int member call “newInt” to our product class. All the product objects in the database will have that variable with the value 0.
An extra information about the matter. If you change the class hierarchy, say make the Base class inherit from a NewBase. db4o will return an error ‘cos it not support this change.
However, say if you remove the inheritance of Product class from Base class, it will work just fine.

Usually object oriented database does not manage class versioning, so what happens if you add new class members?
Hi, the problem you bring up is quite interesting and I think many people think about it as well, so I updated my blog to answer your question.
Hi, This is not free (except for GPL projects). It’s very expensive ($1000+) for commercial projects and on top of that they charge a run time fee for every deployment. It’s completely priced out of the market for a small commercial applications selling for under $100.
You say “And guess what, it’s free and open source as well”
Well, it’s not really free is it? Unless your product is GPL, and that’s a major gap, this product is hardly free
You’re actually right. I haven’t really notice it ‘cos most of my applications is under GPL. $1000+ is for db4o Developer Network (dDN) but you will need to contact them to get the price for Runtime License. I will update my post so it’s not misleading.
Sorry for the confusion.
I suggest to try siaqodb (http://siaqodb.com). Has LINQ default and only query engine, fast, simple, runtime royalty free, cheap per developer licenses
As another alternative try Eloquera Database ( http://www.eloquera.com ).
Eloquera is a native .NET object database.
Eloquera originally designed and developed for use in the Web environment and it’s designed as native .NET application in C#.
- Save the data with a single line of code
- 1TB+ max database size
- SQL and LINQ queries
- Simultaneous user access
- Security settings
- Has genuine C/S architecture, has desktop mode available
- Supports in-memory database for the fast data processing
and many more
And it is completely FREE for commercial use.
Trackbacks