The object container is the door to the database access. It's the starting point for all database operations.
The object container is the interface for accessing the database. To open the database you pass the file-name to the object container factory. Normally you should open an object container when the application starts and close it when it is shuts down.
using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o")) { // use the object container }
' use the object container Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o") End Using
Storing a object with db4o is extremely easy. Open the object container and pass your object to the store method and db4o will do the rest. There's no mapping required. db4o will read the class meta data, the read the object values with reflection and store the data.
using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o")) { Pilot pilot = new Pilot("Joe"); container.Store(pilot); }
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o") Dim pilot As New Pilot("Joe") container.Store(pilot) End Using
Querying for objects is also easy. There are different query interfaces available with different benefits. See "Querying"
The most natural query method is using LINQ.
using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o")) { var pilots = from Pilot p in container where p.Name == "Joe" select p; foreach (var pilot in pilots) { Console.Out.WriteLine(pilot.Name); } }
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o") Dim pilots = From p As Pilot In container Where p.Name = "Joe" For Each pilot As Pilot In pilots Console.Out.WriteLine(pilot.Name) Next End Using
Updating objects is also easy. First you query for the object which you want to update. Then you change the object and store it again in the database.
using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o")) { var pilot = (from Pilot p in container where p.Name == "Joe" select p).First(); pilot.Name = "New Name"; // update the pilot container.Store(pilot); }
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o") Dim pilot = (From p As Pilot In container Where p.Name = "Joe").First() pilot.Name = "New Name" ' update the pilot container.Store(pilot) End Using
Use the delete-operation to delete objects.
using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o")) { var pilot = (from Pilot p in container where p.Name == "Joe" select p).First(); container.Delete(pilot); }
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o") Dim pilot = (From p As Pilot In container Where p.Name = "Joe").First() container.Delete(pilot) End Using