2. First Steps

Let us get started as simple as possible. We are going to learn how to store, retrieve, update and delete instances of a single class that only contains primitive and String members. In our example this will be a Formula One (F1) pilot whose attributes are his name and the F1 points he has already gained this season.
First we create a native class such as:
namespace com.db4o.f1.chapter1
{
    public class Pilot
    {
        string _name;
        int _points;
        
        public Pilot(string name, int points)
        {
            _name = name;
            _points = points;
        }
        
        public string Name
        {
            get
            {
                return _name;
            }
        }
        
        public int Points
        {
            get
            {
                return _points;
            }
        }   
        
        public void AddPoints(int points)
        {
            _points += points;
        }    
        
        override public string ToString()
        {
            return _name + "/" + _points;
        }
    }
}

Note that this class does not contain any db4o related code.

    2.1. Storing objects

    To access a db4o database file or create a new one, call Db4o.openFile(), providing the path to your file as the parameter, to obtain an ObjectContainer instance. ObjectContainer will be your primary interface to db4o. Closing the container will release all resources associated with it.
    [accessDb4o]
    ObjectContainer db=Db4o.openFile(Util.YapFileName);
        try
        {
            // do something with db4o
        }
        finally
        {
            db.close();
        }

    For the following examples we will assume that our environment takes care of opening and closing the ObjectContainer automagically.
    To store an object, we simply call set() on our database, passing the object as a parameter.
    [storeFirstPilot]
    Pilot pilot1 = new Pilot("Michael Schumacher", 100);
        db.set(pilot1);
        Console.WriteLine("Stored " + pilot1);
    OUTPUT:
    Stored Michael Schumacher/100

We'll need a second pilot, too.
[storeSecondPilot]
Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
    db.set(pilot2);
    Console.WriteLine("Stored " + pilot2);
OUTPUT:
Stored Rubens Barrichello/99


2.2. Retrieving objects

To query the database for our pilot, we shall use Query by Example (QBE) for now. This means we will create a prototypical object for db4o to use as an example. db4o will retrieve all objects of the given type that contain the same (non-default) field values as the candidate. The result will be handed as an ObjectSet instance. We will use a convenience method 'listResult', inherited by all our main example classes, to display a result's content and reset it for further use:
public static void listResult(ObjectSet result)
    {
        Console.WriteLine(result.size());
        while (result.hasNext())
        {
            Console.WriteLine(result.next());
        }
    }

To retrieve all pilots from our database, we provide an 'empty' prototype:
[retrieveAllPilots]
Pilot proto = new Pilot(null, 0);
    ObjectSet result = db.get(proto);
    listResult(result);
OUTPUT:
2
Michael Schumacher/100
Rubens Barrichello/99

Note that our results are not constrained to have 0 points, as 0 is the default value for int fields.
To query for a pilot by name:
[retrievePilotByName]
Pilot proto = new Pilot("Michael Schumacher", 0);
    ObjectSet result = db.get(proto);
    listResult(result);
OUTPUT:
1
Michael Schumacher/100

Let's retrieve a pilot by exact points:
[retrievePilotByExactPoints]
Pilot proto = new Pilot(null, 100);
    ObjectSet result = db.get(proto);
    listResult(result);
OUTPUT:
1
Michael Schumacher/100

Of course there's much more to db4o queries. We'll come to that in a moment.

2.3. Updating objects

To update an object already stored in db4o, just call set() again after modifying it.
[updatePilot]
ObjectSet result = db.get(new Pilot("Michael Schumacher", 0));
    Pilot found = (Pilot)result.next();
    found.AddPoints(11);
    db.set(found);
    Console.WriteLine("Added 11 points for " + found);
    retrieveAllPilots(db);
OUTPUT:
Added 11 points for Michael Schumacher/111
2
Michael Schumacher/111
Rubens Barrichello/99

Note that it is necessary that db4o already 'knows' this pilot, else it will store it as a new object. 'Knowing' an object basically means having it set or retrieved during the current db4o session. We'll explain this later in more detail.
To make sure you've updated the pilot, please return to any of the retrieval examples above and run them again.

2.4. Deleting objects

Objects are removed from the database using the delete() method.
[deleteFirstPilotByName]
ObjectSet result = db.get(new Pilot("Michael Schumacher", 0));
    Pilot found = (Pilot)result.next();
    db.delete(found);
    Console.WriteLine("Deleted " + found);
    retrieveAllPilots(db);
OUTPUT:
Deleted Michael Schumacher/111
1
Rubens Barrichello/99

Let's delete the other one, too.
[deleteSecondPilotByName]
ObjectSet result = db.get(new Pilot("Rubens Barrichello", 0));
    Pilot found = (Pilot)result.next();
    db.delete(found);
    Console.WriteLine("Deleted " + found);
    retrieveAllPilots(db);
OUTPUT:
Deleted Rubens Barrichello/99
0

Please check the deletion with the retrieval examples above.
Again, the object to be deleted has to be known to db4o. It is not sufficient to provide a prototype object with the same field values.

2.5. Conclusion

That was easy, wasn't it? We have stored, retrieved, updated and deleted objects with a few lines of code. But what about complex queries? Let's have a look at the restrictions of QBE and alternative approaches in the next chapter .

2.6. Full source

namespace com.db4o.f1.chapter1
{
    using System;
    using System.IO;
    using com.db4o;
    using com.db4o.f1;
    public class FirstStepsExample : Util
    {    
        public static void Main(string[] args)
        {
            File.Delete(Util.YapFileName);
            accessDb4o();
            File.Delete(Util.YapFileName);
            ObjectContainer db = Db4o.openFile(Util.YapFileName);
            try
            {
                storeFirstPilot(db);
                storeSecondPilot(db);
                retrieveAllPilots(db);
                retrievePilotByName(db);
                retrievePilotByExactPoints(db);
                updatePilot(db);
                deleteFirstPilotByName(db);
                deleteSecondPilotByName(db);
            }
            finally
            {
                db.close();
            }
        }
        
        public static void accessDb4o()
        {
         ObjectContainer db=Db4o.openFile(Util.YapFileName);
         try
         {
             // do something with db4o
         }
         finally
         {
             db.close();
         }
     }
        
        public static void storeFirstPilot(ObjectContainer db)
        {
            Pilot pilot1 = new Pilot("Michael Schumacher", 100);
            db.set(pilot1);
            Console.WriteLine("Stored " + pilot1);
        }
    
        public static void storeSecondPilot(ObjectContainer db)
        {
            Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
            db.set(pilot2);
            Console.WriteLine("Stored " + pilot2);
        }
    
        public static void retrieveAllPilots(ObjectContainer db)
        {
            Pilot proto = new Pilot(null, 0);
            ObjectSet result = db.get(proto);
            listResult(result);
        }
    
        public static void retrievePilotByName(ObjectContainer db)
        {
            Pilot proto = new Pilot("Michael Schumacher", 0);
            ObjectSet result = db.get(proto);
            listResult(result);
        }
        
        public static void retrievePilotByExactPoints(ObjectContainer db)
        {
            Pilot proto = new Pilot(null, 100);
            ObjectSet result = db.get(proto);
            listResult(result);
        }
    
        public static void updatePilot(ObjectContainer db)
        {
            ObjectSet result = db.get(new Pilot("Michael Schumacher", 0));
            Pilot found = (Pilot)result.next();
            found.AddPoints(11);
            db.set(found);
            Console.WriteLine("Added 11 points for " + found);
            retrieveAllPilots(db);
        }
    
        public static void deleteFirstPilotByName(ObjectContainer db)
        {
            ObjectSet result = db.get(new Pilot("Michael Schumacher", 0));
            Pilot found = (Pilot)result.next();
            db.delete(found);
            Console.WriteLine("Deleted " + found);
            retrieveAllPilots(db);
        }
    
        public static void deleteSecondPilotByName(ObjectContainer db)
        {
            ObjectSet result = db.get(new Pilot("Rubens Barrichello", 0));
            Pilot found = (Pilot)result.next();
            db.delete(found);
            Console.WriteLine("Deleted " + found);
            retrieveAllPilots(db);
        }
    }
}



--
generated by
Doctor courtesy of db4objects Inc.