2. First StepsLet's get started as simple as possible. We are going to demonstrate 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 class to hold our data. It looks like this:
Notice that this class does not contain any db4o-related code. 2.1. Opening the databaseTo access a db4o database file or create a new one, call DB4OEMBEDDED.OPENFILE() and provide Db4oEmbedded.newConfiguration() as a configuration template and the path to your database file as the second parameter, to obtain an IObjectContainer instance. IObjectContainer represents "The Database", and will be your primary interface to db4o. Closing the IObjectContainer with the #Close() method will close the database file and release all resources associated with it.
Db4oFileName is just a string value representing any filename. If the file with this name already exists, it will be opened as db4o database, otherwise a new db4o database will be created. For the following examples we will assume that our environment takes care of opening and closing the IObjectContainer automagically, and stores the reference in a variable named 'db'. 2.2. Storing objectsTo store an object, we simply call #Store() on our database, passing any object as a parameter.
We'll need a second pilot, too.
2.3. Retrieving objectsThe easiest way to see the content of our database is to use Object Manager Enterprise, which will be introduced in the next chapter . For now let's continue with the API overview and learn how to build db4o queries. db4o supplies several different querying systems, Query by Example (QBE), LINQ, Native Queries (NQ) and the SODA Query API (SODA). In this first example we will introduce QBE. Once you are familiar with storing objects, we encourage you to use LINQ . When using Query-By-Example, you create a prototypical object for db4o to use as an example of what you wish to retrieve. db4o will retrieve all objects of the given type that contain the same (non-default) field values as the example. The results will be returned as an IObjectSet instance. We will use a convenience method #ListResult() to display the contents of our result IObjectSet :
To retrieve all pilots from our database, we provide an 'empty' prototype:
Note that we specify 0 points, but our results were not constrained to only those Pilots with 0 points; 0 is the default value for int fields. db4o also supplies a shortcut to retrieve all instances of a class:
For .NET there also is a generics shortcut, using the query method:
To query for a pilot by name:
And to query for Pilots with a specific number of points:
Of course there's much more to db4o queries. They will be covered in more depth in later chapters. 2.4. Updating objectsUpdating objects is just as easy as storing them. In fact, you use the same #Store() method to update your objects: just call #Store() again after modifying any object.
Notice that we query for the object first. This is an importaint point. When you call #Store() to modify a stored object, if the object is not 'known' (having been previously stored or retrieved during the current session), db4o will insert a new object. db4o does this because it does not automatically match up objects to be stored, with objects previously stored. It assumes you are inserting a second object which happens to have the same field values. To make sure you've updated the pilot, please return to any of the retrieval examples above and run them again. 2.5. Deleting objectsObjects are removed from the database using the #Delete() method.
Let's delete the other one, too.
Please check the deletion with the retrieval examples above. As with updating objects, 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.6. ConclusionThat was easy, wasn't it? We have stored, retrieved, updated and deleted objects with a few lines of code. Now you are probably interested to see how the database looks like. Let's have a look using db4o graphical tool - Object Manager in the next chapter . 2.7. Full source
|