This topic applies to .NET version only.
For this example we'll be using a hypothetical LocalizedItemList
class which binds together culture
information with a list of items.
System.Globalization.CultureInfo
is particularly interesting because it internally holds a native pointer
to a system structure which in turn cannot be cleanly stored by db4o.
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02
using System.Globalization; 03
04
namespace Db4objects.Db4odoc.Translators 05
{ 06
public class LocalizedItemList 07
{ 08
CultureInfo _culture; 09
string[] _items; 10
11
public LocalizedItemList(CultureInfo culture, string[] items) 12
{ 13
_culture = culture; 14
_items = items; 15
} 16
17
override public string ToString() 18
{ 19
return string.Join(string.Concat(_culture.TextInfo.ListSeparator, " "), _items); 20
} 21
} 22
}
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02
Imports System.Globalization 03
04
Namespace Db4objects.Db4odoc.Translators 05
''' <summary> 06
''' A CultureInfo aware list of objects. 07
''' CultureInfo objects hold a native pointer to 08
''' a system structure. 09
''' </summary> 10
Public Class LocalizedItemList 11
Private _culture As CultureInfo 12
13
Private _items As String() 14
15
Public Sub New(ByVal culture As CultureInfo, ByVal items As String()) 16
_culture = culture 17
_items = items 18
End Sub 19
20
Public Overloads Overrides Function ToString() As String 21
Return String.Join(String.Concat(_culture.TextInfo.ListSeparator, " "), _items) 22
End Function 23
24
End Class 25
End Namespace
We'll be using this code to store and retrieve and instance of this class with different configuration
settings:
01public static void TryStoreAndRetrieve(IConfiguration configuration) 02
{ 03
IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 04
try 05
{ 06
string[] champs = new string[] { "Ayrton Senna", "Nelson Piquet" }; 07
LocalizedItemList LocalizedItemList = new LocalizedItemList(CultureInfo.CreateSpecificCulture("pt-BR"), champs); 08
System.Console.WriteLine("ORIGINAL: {0}", LocalizedItemList); 09
db.Set(LocalizedItemList); 10
} 11
catch (Exception x) 12
{ 13
System.Console.WriteLine(x); 14
return; 15
} 16
finally 17
{ 18
db.Close(); 19
} 20
db = Db4oFactory.OpenFile(Db4oFileName); 21
try 22
{ 23
IObjectSet result = db.Get(typeof(LocalizedItemList)); 24
while (result.HasNext()) 25
{ 26
LocalizedItemList LocalizedItemList = (LocalizedItemList)result.Next(); 27
System.Console.WriteLine("RETRIEVED: {0}", LocalizedItemList); 28
db.Delete(LocalizedItemList); 29
} 30
} 31
finally 32
{ 33
db.Close(); 34
} 35
}
01Private Shared Sub TryStoreAndRetrieve(ByVal configuration As IConfiguration) 02
Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 03
Try 04
Dim champs As String() = New String() {"Ayrton Senna", "Nelson Piquet"} 05
Dim LocalizedItemList As LocalizedItemList = New LocalizedItemList(CultureInfo.CreateSpecificCulture("pt-BR"), champs) 06
System.Console.WriteLine("ORIGINAL: {0}", LocalizedItemList) 07
db.Set(LocalizedItemList) 08
Catch x As Exception 09
System.Console.WriteLine(x) 10
Return 11
Finally 12
db.Close() 13
End Try 14
db = Db4oFactory.OpenFile(Db4oFileName) 15
Try 16
Dim result As IObjectSet = db.Get(GetType(LocalizedItemList)) 17
While result.HasNext() 18
Dim LocalizedItemList As LocalizedItemList = DirectCast(result.Next(), LocalizedItemList) 19
System.Console.WriteLine("RETRIEVED: {0}", LocalizedItemList) 20
db.Delete(LocalizedItemList) 21
End While 22
Finally 23
db.Close() 24
End Try 25
End Sub
1public static void TryStoreWithCallConstructors() 2
{ 3
IConfiguration configuration = Db4oFactory.NewConfiguration(); 4
configuration.ExceptionsOnNotStorable(true); 5
configuration.ObjectClass(typeof(CultureInfo)) 6
.CallConstructor(true); 7
TryStoreAndRetrieve(configuration); 8
}
1Private Shared Sub TryStoreWithCallConstructors() 2
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 3
configuration.ExceptionsOnNotStorable(True) 4
configuration.ObjectClass(GetType(CultureInfo)).CallConstructor(True) 5
TryStoreAndRetrieve(configuration) 6
End Sub
At storage time, db4o tests the only available constructor with null arguments and runs into a
NullPointerException, so it refuses to accept our object.
(Note that this test only occurs when configured with exceptionsOnNotStorable - otherwise db4o will
silently fail when trying to reinstantiate the object.)
01public static void TryStoreWithoutCallConstructors() 02
{ 03
IConfiguration configuration = Db4oFactory.NewConfiguration(); 04
configuration.ObjectClass(typeof(CultureInfo)) 05
.CallConstructor(false); 06
// trying to store objects that hold onto 07
// system resources can be pretty nasty 08
// uncomment the following line to see 09
// how nasty it can be 10
//TryStoreAndRetrieve(configuration); 11
}
1Private Shared Sub TryStoreWithoutCallConstructors() 2
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 3
configuration.ObjectClass(GetType(CultureInfo)).CallConstructor(False) 4
' trying to store objects that hold onto 5
' system resources can be pretty nasty 6
' uncomment the following line to see 7
' how nasty it can be 8
'TryStoreAndRetrieve(configuration); 9
End Sub
This still does not work for our case because the native pointer will definitely be invalid. In fact this
example crashes the Common Language Runtime.
In order to solve the problem we will need to use db4o Translators.