Updating List Objects

As we discussed before updating list members using update depth is quite inefficient. An alternative approach can be retrieving and updating each object from the list separately:

ListOperationsExample.cs: UpdateObject
01private static void UpdateObject() 02 { 03 Stopwatch sw = new Stopwatch(); 04 05 IObjectContainer db = Db4oFactory.OpenFile(DbFile); 06 try 07 { 08 // we can set update depth to 0 09 // as we update only the current object 10 db.Ext().Configure().UpdateDepth(0); 11 IList<ListObject> result = db.Query<ListObject>(); 12 if (result.Count == 2) 13 { 14 ListObject lo1 = result[0]; 15 DataObject dataobject = lo1.Data[0]; 16 dataobject.Name = "Updated"; 17 dataobject.Data = DateTime.Now.ToString() + " ---- Updated Object "; 18 19 Console.WriteLine("Updated list {0} dataobject {1}", lo1.Name, lo1.Data[0]); 20 sw.Start(); 21 db.Set(dataobject); 22 db.Commit(); 23 sw.Stop(); 24 } 25 } 26 finally 27 { 28 db.Close(); 29 } 30 Console.WriteLine("Storing took {0}", sw.Elapsed.ToString()); 31 }
ListOperationsExample.vb: UpdateObject
01Private Shared Sub UpdateObject() 02 Dim sw As Stopwatch = New Stopwatch 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(DbFile) 04 Try 05 ' we can set update depth to 0 06 ' as we update only the current object 07 db.Ext.Configure.UpdateDepth(0) 08 Dim result As IList(Of ListObject) = db.Query(Of ListObject)() 09 If result.Count = 2 Then 10 Dim lo1 As ListObject = result(0) 11 Dim dataobject As DataObject = lo1.Data(0) 12 dataobject.Name = "Updated" 13 dataobject.Data = DateTime.Now.ToString + " ---- Updated Object " 14 Console.WriteLine("Updated list {0} dataobject {1}", lo1.Name, lo1.Data(0)) 15 sw.Start() 16 db.Set(dataobject) 17 db.Commit() 18 sw.Stop() 19 End If 20 Finally 21 db.Close() 22 End Try 23 Console.WriteLine("Storing took {0}", sw.Elapsed.ToString) 24 End Sub
In this case only the object of interest is updated, which takes much less time than updating the whole list.