db4o, The Basics

With this post I start a little series about db4o, an object database. Why? Because I think object databases are a quite unknown technology. Normally when developers talk about databases they mean a relational database. Yes, relational databases are awesome and well known. But there are lots of alternatives out there! Sadly, lots of developer never even look at alternatives. For example graph-databases like Neo4J, document-database like CouchDB or a object-database like db4o.

(All posts of this series: the basics, activation, object-identity, transactions, persistent classes, single container concurrency, Queries in Java, C# 2.0, client-server concurrency, transparent persistence, adhoc query tools)

I will stick to the .NET 3.5 version of db4o. Although db4o supports different Java-, .NET and Mono-versions.

The Database-Schema


In db4o your plain old C#-classes are actually the database-schema. So for our first steps we just use two object, people and pets:

public class Pet
    public Pet(string name)
        Name = name;

    string Name { get; set; }
public class Person
    private IEnumerable<Pet> _ownsPets;

    public string FirstName { get; set; }
    public string SirName { get; set; }
    public bool IsMarried { get; set; }

    public IEnumerable<Pet> Owns
        get { return _ownsPets; }
        set { _ownsPets = value.ToArray(); }

Store Some Objects


Ok, this is a really simple task in db4o. Basically you create classes which represent your domain. Then you open a db4o-ObjectContainer and store them. Done!

var config = Db4oEmbedded.NewConfiguration();
using (var db = Db4oEmbedded.OpenFile(config, "database.db4o"))
    var myself = new Person
                         FirstName = "Roman",
                         SirName = "Stoffel",
                         Owns = new[]
                                        new Pet("Findus"),
                                        new Pet("Garfield"),

Get Objects Back


Well storing was easy. But how do I get the objects back? Well what comes to mind immediately? LINQ! So queries are easy too!

using (var db = Db4oEmbedded.OpenFile(config, "database.db4o"))
    var persons = from Person p in db
                  where p.SirName.Contains("Stoffel")
                  select p;
    foreach (var aPerson in persons)

Update Objects


Isn’t that difficult as well.

var config = Db4oEmbedded.NewConfiguration();
using (var db = Db4oEmbedded.OpenFile(config, "database.db4o"))
    var persons = from Person p in db
                  where p.SirName.Contains("Stoffel")
                  select p;
    foreach (var aPerson in persons)
        aPerson.IsMarried = true;

Deleting Objects


Ok, ok it’s getting to boring:


Next time

Well, this wasn’t difficult so far. Next time I’ll dive into a very important concept in db4o, the activation- and update-depth.

 Example-Source-Code: Person.cs Pet.cs Program.cs

Tagged on: ,

15 thoughts on “db4o, The Basics

  1. Ted Kenney

    I love the artwork… and agree that object databases don’t get enough attention.

    Another object-oriented, open source embedded DBMS you should check out is Perst, from McObject. Versions are available for .NET (including .NET Compact Framework and Silverlight), Java (including Android) and Java ME (for those BlackBerry midlets). More info on the Perst embedded database – http://www.mcobject.com/perst.

  2. giu

    nice article. pretty simple concept (altough, next time you make a comic be sure to place a sticker with the word “gore” on your strips. couldn’t have a look at your second comic due to the blood). ok, now, where did I leave off? ah yeah, I have a question regarding object databases: when do you use an object database instead of a relational database (a sample scenario)? what is the biggest disadvantage of an object database?


  3. gamlerhart Post author

    One general disadvantage is that object-databases are a niche market. Therefore no standards like JDBC exists. Each object-database brings its own API. This has even more consequence. On a relation database you can use your favorite db-tool for ad hoc queries etc. On a object-database you’re stuck with the vendor’s tool. And the tools are not nearly as good as the relation-db tool-chain.

    And of course, object-databases aren’t a silver bullet. In general object-database are powerful to store extreme complex, heavily interconnected objects. But when your data is mostly flat and you want to analyses it later a relational-database it much better.

  4. giu

    thanks for the response.

    the point about the niche market is a quite powerful one. that’s surely a showstopper for some enterprises. which object database (OD) is the most famous one for .NET? the size of the userbase of an OD is surely a criterion to have a look at too when deciding which OD you want to use.

    I’ll have a look at it in the future. altough it’s a niche market, the simplicity and powerfullness that come with it make it quite interesting.

  5. gamlerhart Post author

    I actually don’t know which object-database is mostly used for .NET projects. There are some commercial big ‘enterprise’ object-databases. For example Objectivity, Caché or Versant FastObjects.NET

    As said db4o is really intended for embedded scenarios. Well it support a client-server-mode for simple scenarios. But I cannot handle huge workloads.

  6. sathiyaseelan

    Coming from C language, I am confused even after buying DB4o Definite guide. Could you explain the idea in a complete but small project how to integrate C# class and DV4o storage and retrieval? where should I declare class? in another project and reference them in DB40 project. some time people talking about add class to this project what does that mean? reference or rewrite the code in current project?

    because I have not developed C# program where do I start the project? should calsses be one project and DB4o be another project and add them in the project solution is that the plan.
    every example I look is code fragment not complete to a to Z. The wider community will benefit and popularity of DB4o may highten if a complete guide is developed for people who come from different school. a smal but consistent project. if that is already there please direct me to that site.

  7. gamlerhart Post author

    Hi sathiyaseelan

    Well first db4o requires a good object-oriented mindset. And most tutorials assume that have your already a good understanding of object-orientation, know you language and tools. Thats why I and other tutorials only show the ‘important’ bits and leave out the rest.

    The jump from C to C# quite big. Therefore I recommend to start simple. Just create one project with all you classes.

    When you download db4o you have a quite good tutorial in the /doc directory. However it of course is there to touch lots of db4o aspects. For this tutorial all source-code is in the /src/Db4oTutorial\Db4odoc.Tutorial.Chapters\F1. You can just copy the code of a chapter into your project and it should run.

    Or you ask questions in the forum http://developer.db4o.com/forums/default.aspx

  8. kiloolik

    Hello. Can I translatate this article into russian language and publish on famous web-site habrahabr.ru ?

  9. stej

    gamlerhart, you mentioned that db4o is not intended for client-server scenarios. So what database (still embedded) would you recommend instead of that? I would try it to use for web applications (if the performance was good compared to traditional db such as sql server).

  10. gamlerhart Post author

    There are two things. The client-server-embedded-mode: You run db4o in your process and open multiple ‘clients’ on it. (For every request, session whatever) But everything run in the same process. So this mode runs quite well.

    Then there’s the client-server-mode over the network. So you have a server-process and multiple client-processes. Here db4o has quite some limitations. For example when server doesn’t has the classes some features won’t work. You can of course run the server with the same classes as your clients, but then the client-server require always the same version.

    So for an webapplication the embedded-client-server-mode of db4o is quite suitable. For ASP.NET db4o requires some rights. Anyway you can ask the question in the db4o-forums. There are people which have build web-applications with db4o. I’m using db4o for a classic client-application.

    I’ve no recommendation for alternatives right now, because I haven’t used many so far. I’ve used the traditinal mysql- or postgreSQL-db’s in webscenarious.

  11. Pingback: uberVU - social comments

  12. Pingback: db4o: Client-Server and Concurrency | Gamlor

  13. Pingback: RavenDB, the Basics | Gamlor