TSerializable translator allows persistence of classes that do not have a constructor acceptable for db4o (For more information see Translators). Under the hood this translator converts an object to a memory stream on store and restores it upon instantiation. The limitations of this translator:
TSerializable translator should be used only with classes implementing java.io.Serializable interface (Java) or using [Serializable] attribute (.NET).
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02
using System; 03
04
namespace Db4objects.Db4odoc.BuiltInTranslators 05
{ 06
[Serializable] 07
class Pilot 08
{ 09
public string _name; 10
public int _points; 11
12
public Pilot() 13
{ 14
} 15
16
public Pilot(string name, int points) 17
{ 18
_name = name; 19
_points = points; 20
} 21
22
public string Name 23
{ 24
get 25
{ 26
return _name; 27
} 28
set 29
{ 30
_name = value; 31
} 32
} 33
34
public int Points 35
{ 36
get 37
{ 38
return _points; 39
} 40
} 41
42
public override string ToString() 43
{ 44
return string.Format("{0}/{1}", _name, _points); 45
} 46
} 47
}
01public static void SaveSerializable() 02
{ 03
File.Delete(Db4oFileName); 04
IConfiguration configuration = Db4oFactory.NewConfiguration(); 05
// configure class as serializable 06
configuration.ObjectClass(typeof(Pilot)).Translate(new TSerializable()); 07
IObjectContainer container = Database(configuration); 08
if (container != null) 09
{ 10
try 11
{ 12
Pilot pilot = new Pilot("Test Pilot 1", 99); 13
container.Set(pilot); 14
pilot = new Pilot("Test Pilot 2", 100); 15
container.Set(pilot); 16
} 17
catch (Db4oException ex) 18
{ 19
System.Console.WriteLine("Db4o Exception: " + ex.Message); 20
} 21
catch (Exception ex) 22
{ 23
System.Console.WriteLine("System Exception: " + ex.Message); 24
} 25
finally 26
{ 27
CloseDatabase(); 28
} 29
} 30
31
}
01public static void TestSerializable() 02
{ 03
SaveSerializable(); 04
IConfiguration configuration = Db4oFactory.NewConfiguration(); 05
// configure class as serializable to retrieve correctly 06
configuration.ObjectClass(typeof(Pilot)).Translate(new TSerializable()); 07
IObjectContainer container = Database(configuration); 08
if (container != null) 09
{ 10
try 11
{ 12
System.Console.WriteLine("Retrieving pilots by name:"); 13
IQuery query = container.Query(); 14
query.Constrain(typeof(Pilot)); 15
query.Descend("_name").Constrain("Test Pilot 1"); 16
IObjectSet resultByName = query.Execute(); 17
ListResult(resultByName); 18
19
System.Console.WriteLine("Retrieve all pilot objects:"); 20
IList<Pilot> result = container.Query<Pilot>(); 21
ListResult(result); 22
} 23
catch (Db4oException ex) 24
{ 25
System.Console.WriteLine("Db4o Exception: " + ex.Message); 26
} 27
catch (Exception ex) 28
{ 29
System.Console.WriteLine("System Exception: " + ex.Message); 30
} 31
finally 32
{ 33
CloseDatabase(); 34
} 35
} 36
}
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02
03
Imports System 04
05
Namespace Db4objects.Db4odoc.BuiltInTranslators 06
<Serializable()> _ 07
Class Pilot 08
Public _name As String 09
Public _points As Integer 10
11
Public Sub New() 12
End Sub 13
14
Public Sub New(ByVal name As String, ByVal points As Integer) 15
_name = name 16
_points = points 17
End Sub 18
19
Public Property Name() As String 20
Get 21
Return _name 22
End Get 23
Set(ByVal value As String) 24
_name = value 25
End Set 26
End Property 27
28
Public ReadOnly Property Points() As Integer 29
Get 30
Return _points 31
End Get 32
End Property 33
34
Public Overloads Overrides Function ToString() As String 35
Return String.Format("{0}/{1}", _name, _points) 36
End Function 37
End Class 38
End Namespace
01Public Shared Sub SaveSerializable() 02
File.Delete(Db4oFileName) 03
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 04
' configure class as serializable 05
configuration.ObjectClass(GetType(Pilot)).Translate(New TSerializable()) 06
Dim container As IObjectContainer = Database(configuration) 07
If container IsNot Nothing Then 08
Try 09
Dim pilot As New Pilot("Test Pilot 1", 99) 10
container.[Set](pilot) 11
pilot = New Pilot("Test Pilot 2", 100) 12
container.[Set](pilot) 13
Catch ex As Db4oException 14
System.Console.WriteLine("Db4o Exception: " + ex.Message) 15
Catch ex As Exception 16
System.Console.WriteLine("System Exception: " + ex.Message) 17
Finally 18
CloseDatabase() 19
End Try 20
End If 21
22
End Sub
01Public Shared Sub TestSerializable() 02
SaveSerializable() 03
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 04
' configure class as serializable to retrieve correctly 05
configuration.ObjectClass(GetType(Pilot)).Translate(New TSerializable()) 06
Dim container As IObjectContainer = Database(configuration) 07
If container IsNot Nothing Then 08
Try 09
System.Console.WriteLine("Retrieving pilots by name:") 10
Dim query As IQuery = container.Query() 11
query.Constrain(GetType(Pilot)) 12
query.Descend("_name").Constrain("Test Pilot 1") 13
Dim resultByName As IObjectSet = query.Execute() 14
ListResult(resultByName) 15
16
System.Console.WriteLine("Retrieve all pilot objects:") 17
Dim result As IList(Of Pilot) = container.Query(Of Pilot)() 18
ListResult(result) 19
Catch ex As Db4oException 20
System.Console.WriteLine("Db4o Exception: " + ex.Message) 21
Catch ex As Exception 22
System.Console.WriteLine("System Exception: " + ex.Message) 23
Finally 24
CloseDatabase() 25
End Try 26
End If 27
End Sub