db4o makes your work with persistent objects very simple and straightforward. The only #set(object) method is used for both saving and modification of any object that exists in your model.
01public static void StorePilot() 02
{ 03
File.Delete(YapFileName); 04
IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 05
try 06
{ 07
Pilot pilot = new Pilot("Michael Schumacher", 0); 08
db.Set(pilot); 09
// change pilot and resave updated 10
pilot.AddPoints(10); 11
db.Set(pilot); 12
Console.WriteLine("Stored {0}", pilot); 13
} 14
finally 15
{ 16
db.Close(); 17
} 18
RetrieveAllPilots(); 19
}
01public static void UpdatePilotWrong() 02
{ 03
StorePilot(); 04
IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 05
try 06
{ 07
// Even completely identical Pilot object 08
// won't work for update of the saved pilot 09
Pilot pilot = new Pilot("Michael Schumacher",0); 10
pilot.AddPoints(11); 11
db.Set(pilot); 12
Console.WriteLine("Added 11 points to {0}", pilot); 13
} 14
finally 15
{ 16
db.Close(); 17
} 18
RetrieveAllPilots(); 19
}
01public static void UpdatePilot() 02
{ 03
StorePilot(); 04
IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 05
try 06
{ 07
// first retrieve the object from the database 08
IObjectSet result = db.Get(new Pilot("Michael Schumacher",10)); 09
Pilot found=(Pilot)result.Next(); 10
found.AddPoints(11); 11
db.Set(found); 12
Console.WriteLine("Added 11 points to {0}", found); 13
} 14
finally 15
{ 16
db.Close(); 17
} 18
RetrieveAllPilots(); 19
}
01Public Shared Sub StorePilot() 02
File.Delete(YapFileName) 03
Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 04
Try 05
Dim pilot As Pilot = New Pilot("Michael Schumacher", 0) 06
db.Set(pilot) 07
Console.WriteLine("Stored {0}", pilot) 08
' change pilot and resave updated 09
pilot.AddPoints(10) 10
db.Set(pilot) 11
Console.WriteLine("Stored {0}", pilot) 12
Finally 13
db.Close() 14
End Try 15
RetrieveAllPilots() 16
End Sub
01Public Shared Sub UpdatePilotWrong() 02
StorePilot() 03
Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 04
Try 05
'Even completely identical Pilot object 06
' won't work for update of the saved pilot 07
Dim pilot As Pilot = New Pilot("Michael Schumacher", 10) 08
pilot.AddPoints(10) 09
db.Set(pilot) 10
Console.WriteLine("Stored {0}", pilot) 11
Finally 12
db.Close() 13
End Try 14
RetrieveAllPilots() 15
End Sub
01Public Shared Sub UpdatePilot() 02
StorePilot() 03
Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 04
Try 05
'first retrieve the object from the database 06
Dim result As IObjectSet = db.Get(New Pilot("Michael Schumacher", 10)) 07
Dim pilot As Pilot = CType(result(0), Pilot) 08
pilot.AddPoints(10) 09
db.Set(pilot) 10
Console.WriteLine("Added 10 points to {0}", pilot) 11
Finally 12
db.Close() 13
End Try 14
RetrieveAllPilots() 15
End Sub
Deletion is just as easy:
01public static void DeletePilot() 02
{ 03
StorePilot(); 04
IObjectContainer db=Db4oFactory.OpenFile(YapFileName); 05
try 06
{ 07
// first retrieve the object from the database 08
IObjectSet result=db.Get(new Pilot("Michael Schumacher",10)); 09
Pilot found=(Pilot)result.Next(); 10
db.Delete(found); 11
System.Console.WriteLine("Deleted "+found); 12
} 13
finally 14
{ 15
db.Close(); 16
} 17
RetrieveAllPilots(); 18
}
01Public Shared Sub DeletePilot() 02
StorePilot() 03
Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 04
Try 05
'first retrieve the object from the database 06
Dim result As IObjectSet = db.Get(New Pilot("Michael Schumacher", 10)) 07
Dim pilot As Pilot = CType(result(0), Pilot) 08
db.Delete(pilot) 09
Console.WriteLine("Deleted {0}", pilot) 10
Finally 11
db.Close() 12
End Try 13
RetrieveAllPilots() 14
End Sub
The objects are identified by their references in an application cache. You do not need to implement any additional identification systems (like primary keys in RDBMS). See Identity chapter for details. The uniqueness of an object is defined only by its reference, if you will create 2 objects of the same class with exactly the same fields and save them to db4o - you will get 2 objects in your database. As you can see from the examples an object instance should be retrieved from the database before updating or deleting or you can use the newly created object if it was stored in the same session. Creating a new instance identical to the object in the database and saving it, will create a new object in the database.
Db4o does all the "dirty" work of objects transition between your classes and persistent state using Reflection . No mappings or additional coding is needed from your side. If you will need to change your application model for the next version you will also be surprised with the simplicity: all the changes are done in one place - your code, and the most common operations are done completely automatically (see Refactoring And Schema Evolution chapter for details).