By default db4o does not persist static fields. This is not necessary because static values are set for a class, not for an object. However you can set up db4o to store static fields if you want to implement constants or enumeration: See "Persist Static Fields"
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration(); configuration.Common.ObjectClass(typeof (Person)).PersistStaticFieldValues();
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() configuration.Common.ObjectClass(GetType(Person)).PersistStaticFieldValues()
When this setting is enabled, all non-primitive-typed static fields are stored the first time an instance of the class is stored. The values are restored every time a database file is opened afterwards, after the class meta information is loaded for this class (when the class objects are retrieved with a query, for example).
Use this option with caution. This option means that static fields are stored in the database. When you change the value of this field, you need to store it explicitly again. Furthermore, db4o will replace the static value at runtime, which can lead to very subtle bugs in your application.
This option does not have any effect on primitive types like ints, longs, floats etc.
One of the use-cases is when you have an enumeration-class which you want to store. For example we have a color-class, which also has some static colors.
public sealed class Color { public static readonly Color Black = new Color(0, 0, 0); public static readonly Color White = new Color(255, 255, 255); public static readonly Color Red = new Color(255, 0, 0); public static readonly Color Green = new Color(0, 255, 0); public static readonly Color Blue = new Color(0, 0, 255); private readonly int red; private readonly int green; private readonly int blue; private Color(int red, int green, int blue) { this.red = red; this.green = green; this.blue = blue; } public int RedValue { get { return red; } } public int GreenValue { get { return green; } } public int BlueValue { get { return blue; } } public bool Equals(Color other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return other.red == red && other.green == green && other.blue == blue; } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != typeof (Color)) return false; return Equals((Color) obj); } public override int GetHashCode() { unchecked { int result = red; result = (result*397) ^ green; result = (result*397) ^ blue; return result; } } public override string ToString() { return string.Format("Red: {0}, Green: {1}, Blue: {2}", red, green, blue); } }
Public NotInheritable Class Color Public Shared ReadOnly Black As New Color(0, 0, 0) Public Shared ReadOnly White As New Color(255, 255, 255) Public Shared ReadOnly Red As New Color(255, 0, 0) Public Shared ReadOnly Green As New Color(0, 255, 0) Public Shared ReadOnly Blue As New Color(0, 0, 255) Private ReadOnly m_red As Integer Private ReadOnly m_green As Integer Private ReadOnly m_blue As Integer Private Sub New(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer) Me.m_red = red Me.m_green = green Me.m_blue = blue End Sub Public ReadOnly Property RedValue() As Integer Get Return m_red End Get End Property Public ReadOnly Property GreenValue() As Integer Get Return m_green End Get End Property Public ReadOnly Property BlueValue() As Integer Get Return m_blue End Get End Property Public Overloads Function Equals(ByVal other As Color) As Boolean If ReferenceEquals(Nothing, other) Then Return False If ReferenceEquals(Me, other) Then Return True Return other.m_red = m_red AndAlso other.m_green = m_green AndAlso other.m_blue = m_blue End Function Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean If ReferenceEquals(Nothing, obj) Then Return False If ReferenceEquals(Me, obj) Then Return True If Not Equals(obj.GetType(), GetType(Color)) Then Return False Return Equals(DirectCast(obj, Color)) End Function Public Overrides Function GetHashCode() As Integer Dim hashCode As Integer = m_red hashCode = (hashCode * 397) Xor m_green hashCode = (hashCode * 397) Xor m_blue Return hashCode End Function Public Overrides Function ToString() As String Return String.Format("Red: {0}, Green: {1}, Blue: {2}", m_red, m_green, m_blue) End Function End Class
We want to ensure reference equality on colors so that you easily can check for a certain color. But when we load the colors from the database you get new instances and not the same instance as in the static field. This means that comparing the references will fail.
// When you enable persist static field values, you can compare by reference // because db4o stores the static field if (car.Color == Color.Black) { Console.WriteLine("Black cars are boring"); } else if (car.Color == Color.Red) { Console.WriteLine("Fire engine?"); }
' When you enable persist static field values, you can compare by reference ' because db4o stores the static field If car.Color Is Color.Black Then Console.WriteLine("Black cars are boring") ElseIf car.Color Is Color.Red Then Console.WriteLine("Fire engine?")
When you enable the persist static fields option, the static fields are stored. This means that the object referenced in the static fields are loaded from the database and therefore the same instance. And the comparing the references works again.
IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration(); configuration.Common.ObjectClass(typeof (Color)).PersistStaticFieldValues();
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() configuration.Common.ObjectClass(GetType(Color)).PersistStaticFieldValues()