Equality Comparison

Db4o uses reference cache for quick access to persistent objects. Each persistent object is guaranteed to have only one instance in the object reference cache independently of whether it was saved or retrieved. You can retrieve the same object several times with different querying methods, but you will still get references to the same object, so that ref(1) == ref(2) == ... == ref(n).

In the same time it means that 2 objects, for example, one created in the runtime and another retrieved from the database, with the same data (field values) won't be equal for db4o.

There are 2 ways to compare db4o objects by data:

Let's save an object to the database and try the above mentioned methods in practice.

EqualityExample.cs: StorePilot
01private static void StorePilot() 02 { 03 IObjectContainer container = Database(); 04 if (container != null) { 05 try { 06 Pilot pilot = new Pilot("Kimi Raikkonnen", 100); 07 container.Set(pilot); 08 } catch (Exception ex) { 09 System.Console.WriteLine("System Exception: " + ex.Message); 10 } finally { 11 CloseDatabase(); 12 } 13 } 14 }
EqualityExample.vb: StorePilot
01Private Shared Sub StorePilot() 02 Dim container As IObjectContainer = Database() 03 If container IsNot Nothing Then 04 Try 05 Dim pilot As New Pilot("Kimi Raikkonnen", 100) 06 container.[Set](pilot) 07 Catch ex As Exception 08 System.Console.WriteLine("System Exception: " + ex.Message) 09 Finally 10 CloseDatabase() 11 End Try 12 End If 13 End Sub
More Reading: