Static fields API

By default db4o does not persist static fields. Normally this is not necessary as static values are set for a class, not for an object. However you can set up db4o to store static fields if you need to implement constant or enumeration:

c#: Db4oFactory.Configure().ObjectClass(typeof(Foo)).PersistStaticFieldValues()

VB: Db4oFactory.Configure().ObjectClass(GetType(Foo)).PersistStaticFieldValues()

Do not use this option unnecessarily, as it will slow down the process of opening database files and the stored objects will occupy space in the database file.

This option does not have any eaffect on primitive types (int, boolean, etc). Use their object alternatives instead (Integer, Boolean, etc).

When this setting is on for a specific class, all non-primitive-typed static field values of this class are stored the first time an object of the class is stored, and restored, every time a database file is opened afterwards, after class meta information is loaded for this class (when the class objects are retrieved with a query, for example).

A good example of non-primitive constant type is type-safe enumeration implementation:

PilotCategories.cs
01namespace Db4objects.Db4odoc.StaticFields 02{ 03 public class PilotCategories 04 { 05 private string _qualification = null; 06 public static PilotCategories Winner = new PilotCategories("WINNER"); 07 public static PilotCategories Talented = new PilotCategories("TALENTED"); 08 public static PilotCategories Average = new PilotCategories("AVERAGE"); 09 public static PilotCategories Disqualified = new PilotCategories("DISQUALIFIED"); 10 11 private PilotCategories(string qualification) 12 { 13 this._qualification = qualification; 14 } 15 16 public PilotCategories() 17 { 18 19 } 20 21 public void TestChange(string qualification) 22 { 23 this._qualification = qualification; 24 } 25 26 override public string ToString() 27 { 28 return _qualification; 29 } 30 } 31}

PilotCategories.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02 03Namespace Db4objects.Db4odoc.StaticFields 04 Public Class PilotCategories 05 Private _qualification As String = Nothing 06 Public Shared WINNER As PilotCategories = New PilotCategories("WINNER") 07 Public Shared TALENTED As PilotCategories = New PilotCategories("TALENTED") 08 Public Shared AVERAGE As PilotCategories = New PilotCategories("AVERAGE") 09 Public Shared DISQUALIFIED As PilotCategories = New PilotCategories("DISQUALIFIED") 10 11 Private Sub New(ByVal qualification As String) 12 Me._qualification = qualification 13 End Sub 14 15 Public Sub New() 16 17 End Sub 18 19 Public Sub TestChange(ByVal qualification As String) 20 Me._qualification = qualification 21 End Sub 22 23 Public Overrides Function ToString() As String 24 Return _qualification 25 End Function 26 End Class 27End Namespace

Let's use it with

Pilot.cs
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02namespace Db4objects.Db4odoc.StaticFields 03{ 04 public class Pilot 05 { 06 private string _name; 07 private PilotCategories _category; 08 09 public Pilot(string name,PilotCategories category) 10 { 11 _name=name; 12 _category=category; 13 } 14 15 public PilotCategories Category 16 { 17 get 18 { 19 return _category; 20 } 21 } 22 23 public string Name 24 { 25 get 26 { 27 return _name; 28 } 29 } 30 31 override public string ToString() 32 { 33 return string.Format("{0}/{1}", _name, _category); 34 } 35 } 36}

Pilot.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02 03Namespace Db4objects.Db4odoc.StaticFields 04 Public Class Pilot 05 Private _name As String 06 Private _category As PilotCategories 07 08 Public Sub New(ByVal name As String, ByVal Category As PilotCategories) 09 Me._name = name 10 Me._category = Category 11 End Sub 12 13 Public ReadOnly Property Category() As PilotCategories 14 Get 15 Return _category 16 End Get 17 End Property 18 19 Public ReadOnly Property Name() As String 20 Get 21 Return _name 22 End Get 23 End Property 24 25 Public Overrides Function ToString() As String 26 Return String.Format("{0}/{1}", _name, _category) 27 End Function 28 End Class 29End Namespace

StaticFieldExample.cs: SetPilotsSimple
01public static void SetPilotsSimple() 02 { 03 Console.WriteLine("In the default setting, static constants are not continously stored and updated."); 04 File.Delete(Db4oFileName); 05 IObjectContainer db=Db4oFactory.OpenFile(Db4oFileName); 06 try 07 { 08 db.Set(new Pilot("Michael Schumacher",PilotCategories.Winner)); 09 db.Set(new Pilot("Rubens Barrichello",PilotCategories.Talented)); 10 } 11 finally 12 { 13 db.Close(); 14 } 15 }

StaticFieldExample.vb: SetPilotsSimple
01Private Shared Sub SetPilotsSimple() 02 Console.WriteLine("In the default setting, static constants are not continously stored and updated.") 03 File.Delete(Db4oFileName) 04 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 05 Try 06 db.Set(New Pilot("Michael Schumacher", PilotCategories.WINNER)) 07 db.Set(New Pilot("Rubens Barrichello", PilotCategories.TALENTED)) 08 Finally 09 db.Close() 10 End Try 11 End Sub

We can try to save pilots with the default db4o settings:

StaticFieldExample.cs: CheckPilots
01public static void CheckPilots() 02 { 03 IObjectContainer db=Db4oFactory.OpenFile(Db4oFileName); 04 try 05 { 06 IObjectSet result = db.Get(typeof(Pilot)); 07 for(int x = 0; x < result.Count; x++) 08 { 09 Pilot pilot = (Pilot )result[x]; 10 if (pilot.Category == PilotCategories.Winner) 11 { 12 Console.WriteLine("Winner pilot: " + pilot); 13 } 14 else if (pilot.Category == PilotCategories.Talented) 15 { 16 Console.WriteLine("Talented pilot: " + pilot); 17 } 18 else 19 { 20 Console.WriteLine("Uncategorized pilot: " + pilot); 21 } 22 } 23 } 24 finally 25 { 26 db.Close(); 27 } 28 }

StaticFieldExample.vb: CheckPilots
01Private Shared Sub CheckPilots() 02 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 03 Try 04 Dim result As IObjectSet = db.Get(GetType(Pilot)) 05 Dim x As Integer 06 For x = 0 To result.Count - 1 Step x + 1 07 Dim pilot As Pilot = CType(result(x), Pilot) 08 If pilot.Category Is PilotCategories.WINNER Then 09 Console.WriteLine("Winner pilot: " + pilot.ToString()) 10 ElseIf pilot.Category Is PilotCategories.TALENTED Then 11 Console.WriteLine("Talented pilot: " + pilot.ToString()) 12 Else 13 Console.WriteLine("Uncategorized pilot: " + pilot.ToString()) 14 End If 15 Next 16 Finally 17 db.Close() 18 End Try 19 End Sub

That does not work however. We will have to explicitly point out, which class's static fields we want to save:

StaticFieldExample.cs: SetPilotsStatic
01public static void SetPilotsStatic() 02 { 03 Console.WriteLine("The feature can be turned on for individual classes."); 04 Db4oFactory.Configure().ObjectClass(typeof(PilotCategories)).PersistStaticFieldValues(); 05 File.Delete(Db4oFileName); 06 IObjectContainer db=Db4oFactory.OpenFile(Db4oFileName); 07 try 08 { 09 db.Set(new Pilot("Michael Schumacher",PilotCategories.Winner)); 10 db.Set(new Pilot("Rubens Barrichello",PilotCategories.Talented)); 11 } 12 finally 13 { 14 db.Close(); 15 } 16 }

StaticFieldExample.vb: SetPilotsStatic
01Private Shared Sub SetPilotsStatic() 02 Console.WriteLine("The feature can be turned on for individual classes.") 03 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 04 configuration.ObjectClass(GetType(PilotCategories)).PersistStaticFieldValues() 05 File.Delete(Db4oFileName) 06 Dim db As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 07 Try 08 db.Set(New Pilot("Michael Schumacher", PilotCategories.WINNER)) 09 db.Set(New Pilot("Rubens Barrichello", PilotCategories.TALENTED)) 10 Finally 11 db.Close() 12 End Try 13 End Sub

StaticFieldExample.cs: CheckPilots
01public static void CheckPilots() 02 { 03 IObjectContainer db=Db4oFactory.OpenFile(Db4oFileName); 04 try 05 { 06 IObjectSet result = db.Get(typeof(Pilot)); 07 for(int x = 0; x < result.Count; x++) 08 { 09 Pilot pilot = (Pilot )result[x]; 10 if (pilot.Category == PilotCategories.Winner) 11 { 12 Console.WriteLine("Winner pilot: " + pilot); 13 } 14 else if (pilot.Category == PilotCategories.Talented) 15 { 16 Console.WriteLine("Talented pilot: " + pilot); 17 } 18 else 19 { 20 Console.WriteLine("Uncategorized pilot: " + pilot); 21 } 22 } 23 } 24 finally 25 { 26 db.Close(); 27 } 28 }

StaticFieldExample.vb: CheckPilots
01Private Shared Sub CheckPilots() 02 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 03 Try 04 Dim result As IObjectSet = db.Get(GetType(Pilot)) 05 Dim x As Integer 06 For x = 0 To result.Count - 1 Step x + 1 07 Dim pilot As Pilot = CType(result(x), Pilot) 08 If pilot.Category Is PilotCategories.WINNER Then 09 Console.WriteLine("Winner pilot: " + pilot.ToString()) 10 ElseIf pilot.Category Is PilotCategories.TALENTED Then 11 Console.WriteLine("Talented pilot: " + pilot.ToString()) 12 Else 13 Console.WriteLine("Uncategorized pilot: " + pilot.ToString()) 14 End If 15 Next 16 Finally 17 db.Close() 18 End Try 19 End Sub

As it was mentioned before, it is important to keep static values in one place and do not allow different objects to modify them. If we try to change static value from the referencing object:

StaticFieldExample.cs: UpdatePilots
01public static void UpdatePilots() 02 { 03 Console.WriteLine("Updating PilotCategory in pilot reference:"); 04 IObjectContainer db=Db4oFactory.OpenFile(Db4oFileName); 05 try 06 { 07 IObjectSet result = db.Get(typeof(Pilot)); 08 for(int x = 0; x < result.Count; x++) 09 { 10 Pilot pilot = (Pilot )result[x]; 11 if (pilot.Category == PilotCategories.Winner) 12 { 13 Console.WriteLine("Winner pilot: " + pilot); 14 PilotCategories pc = pilot.Category; 15 pc.TestChange("WINNER2006"); 16 db.Set(pilot); 17 } 18 } 19 PrintCategories(db); 20 } 21 finally 22 { 23 db.Close(); 24 } 25 }

StaticFieldExample.vb: UpdatePilots
01Private Shared Sub UpdatePilots() 02 Console.WriteLine("Updating PilotCategory in pilot reference:") 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 04 Try 05 Dim result As IObjectSet = db.Get(GetType(Pilot)) 06 Dim x As Integer 07 For x = 0 To result.Count - 1 Step x + 1 08 Dim pilot As Pilot = CType(result(x), Pilot) 09 If pilot.Category Is PilotCategories.WINNER Then 10 Console.WriteLine("Winner pilot: " + pilot.ToString()) 11 Dim pc As PilotCategories = pilot.Category 12 pc.TestChange("WINNER2006") 13 db.Set(pilot) 14 End If 15 Next 16 PrintCategories(db) 17 Finally 18 db.Close() 19 End Try 20 End Sub

the value just does not change.

In order to update static field we will have to do that explicitly:

StaticFieldExample.cs: UpdatePilotCategories
01public static void UpdatePilotCategories() 02 { 03 Console.WriteLine("Updating PilotCategories explicitly:"); 04 IObjectContainer db=Db4oFactory.OpenFile(Db4oFileName); 05 try 06 { 07 IObjectSet result = db.Get(typeof(PilotCategories)); 08 for(int x = 0; x < result.Count; x++) 09 { 10 PilotCategories pc = (PilotCategories)result[x]; 11 if (pc == PilotCategories.Winner) 12 { 13 pc.TestChange("WINNER2006"); 14 db.Set(pc); 15 } 16 } 17 PrintCategories(db); 18 } 19 finally 20 { 21 db.Close(); 22 } 23 }

StaticFieldExample.vb: UpdatePilotCategories
01Private Shared Sub UpdatePilotCategories() 02 Console.WriteLine("Updating PilotCategories explicitly:") 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 04 Try 05 Dim result As IObjectSet = db.Get(GetType(PilotCategories)) 06 Dim x As Integer 07 For x = 0 To result.Count - 1 Step x + 1 08 Dim pc As PilotCategories = CType(result(x), PilotCategories) 09 If pc Is PilotCategories.WINNER Then 10 pc.TestChange("WINNER2006") 11 db.Set(pc) 12 End If 13 Next 14 PrintCategories(db) 15 Finally 16 db.Close() 17 End Try 18 End Sub

StaticFieldExample.cs: CheckPilots
01public static void CheckPilots() 02 { 03 IObjectContainer db=Db4oFactory.OpenFile(Db4oFileName); 04 try 05 { 06 IObjectSet result = db.Get(typeof(Pilot)); 07 for(int x = 0; x < result.Count; x++) 08 { 09 Pilot pilot = (Pilot )result[x]; 10 if (pilot.Category == PilotCategories.Winner) 11 { 12 Console.WriteLine("Winner pilot: " + pilot); 13 } 14 else if (pilot.Category == PilotCategories.Talented) 15 { 16 Console.WriteLine("Talented pilot: " + pilot); 17 } 18 else 19 { 20 Console.WriteLine("Uncategorized pilot: " + pilot); 21 } 22 } 23 } 24 finally 25 { 26 db.Close(); 27 } 28 }

StaticFieldExample.vb: CheckPilots
01Private Shared Sub CheckPilots() 02 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 03 Try 04 Dim result As IObjectSet = db.Get(GetType(Pilot)) 05 Dim x As Integer 06 For x = 0 To result.Count - 1 Step x + 1 07 Dim pilot As Pilot = CType(result(x), Pilot) 08 If pilot.Category Is PilotCategories.WINNER Then 09 Console.WriteLine("Winner pilot: " + pilot.ToString()) 10 ElseIf pilot.Category Is PilotCategories.TALENTED Then 11 Console.WriteLine("Talented pilot: " + pilot.ToString()) 12 Else 13 Console.WriteLine("Uncategorized pilot: " + pilot.ToString()) 14 End If 15 Next 16 Finally 17 db.Close() 18 End Try 19 End Sub

We can see that the reference has changed correctly.

What about deletion? Nothing is new here. We can delete static fields from the referenced object, or directly from the database:

StaticFieldExample.cs: DeleteTest
01public static void DeleteTest() 02 { 03 IObjectContainer db=Db4oFactory.OpenFile(Db4oFileName); 04 db.Ext().Configure().ObjectClass(typeof(Pilot)).CascadeOnDelete(true); 05 try 06 { 07 Console.WriteLine("Deleting Pilots :"); 08 IObjectSet result = db.Get(typeof(Pilot)); 09 for(int x = 0; x < result.Count; x++) 10 { 11 Pilot pilot = (Pilot )result[x]; 12 db.Delete(pilot); 13 } 14 PrintCategories(db); 15 Console.WriteLine("Deleting PilotCategories :"); 16 result = db.Get(typeof(PilotCategories)); 17 for(int x = 0; x < result.Count; x++) 18 { 19 db.Delete(result[x]); 20 } 21 PrintCategories(db); 22 } 23 finally 24 { 25 db.Close(); 26 } 27 }

StaticFieldExample.vb: DeleteTest
01Private Shared Sub DeleteTest() 02 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 03 configuration.ObjectClass(GetType(Pilot)).CascadeOnDelete(True) 04 Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 05 Try 06 Console.WriteLine("Deleting Pilots :") 07 Dim result As IObjectSet = db.Get(GetType(Pilot)) 08 Dim x As Integer 09 For x = 0 To result.Count - 1 Step x + 1 10 Dim pilot As Pilot = CType(result(x), Pilot) 11 db.Delete(pilot) 12 Next 13 PrintCategories(db) 14 Console.WriteLine("Deleting PilotCategories :") 15 result = db.Get(GetType(PilotCategories)) 16 For x = 0 To result.Count - 1 Step x + 1 17 db.Delete(result(x)) 18 Next 19 PrintCategories(db) 20 Finally 21 db.Close() 22 End Try 23 End Sub