Translator API provides a special way of storing and retrieving objects. In fact the actual class is not stored in the database. Instead the information from that class is stored in a primitive object (object array) and the class is recreated during instantiation or activation.
Let's look how queries handle translated classes. Diagnostics system will help us to see, what is going on.
In our example class Car is configured to be saved and retrieved with CarTranslator class. CarTranslator saves only car model information appending it with the production date.
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02
03
using Db4objects.Db4o; 04
using Db4objects.Db4o.Config; 05
06
namespace Db4objects.Db4odoc.Diagnostics 07
{ 08
09
public class CarTranslator: IObjectConstructor 10
{ 11
public object OnStore(IObjectContainer container, object applicationObject) 12
{ 13
Car car =(Car)applicationObject; 14
15
string fullModel; 16
if (HasYear(car.Model)) 17
{ 18
fullModel = car.Model; 19
} 20
else 21
{ 22
fullModel = car.Model + GetYear(car.Model); 23
} 24
return fullModel; 25
} 26
27
private string GetYear(string carModel) 28
{ 29
if (carModel.Equals("BMW")) 30
{ 31
return " 2002"; 32
} 33
else 34
{ 35
return " 1999"; 36
} 37
} 38
39
private bool HasYear(string carModel) 40
{ 41
return false; 42
} 43
44
public object OnInstantiate(IObjectContainer container, object storedObject) 45
{ 46
string model=(string)storedObject; 47
return new Car(model); 48
} 49
50
public void OnActivate(IObjectContainer container, 51
object applicationObject, object storedObject) 52
{ 53
} 54
55
IObjectTranslator Members 64
} 65
}
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02
03
Imports Db4objects.Db4o 04
Imports Db4objects.Db4o.Config 05
06
Namespace Db4objects.Db4odoc.Diagnostics 07
08
Public Class CarTranslator 09
Implements IObjectConstructor 10
11
Public Function OnStore(ByVal container As IObjectContainer, ByVal applicationObject As Object) As Object Implements IObjectConstructor.OnStore 12
Dim car As Car = CType(applicationObject, Car) 13
14
Dim fullModel As String 15
If HasYear(car.Model) Then 16
fullModel = car.Model 17
Else 18
fullModel = car.Model + GetYear(car.Model) 19
End If 20
Return fullModel 21
22
End Function 23
24
25
Private Function GetYear(ByVal carModel As String) As String 26
If carModel.Equals("BMW") Then 27
Return " 2002" 28
Else 29
Return " 1999" 30
End If 31
End Function 32
33
Private Function HasYear(ByVal carModel As String) As Boolean 34
Return False 35
End Function 36
37
Public Function OnInstantiate(ByVal container As IObjectContainer, ByVal storedObject As Object) As Object Implements IObjectConstructor.OnInstantiate 38
Dim model As String = DirectCast(storedObject, String) 39
Return New Car(model) 40
End Function 41
42
Public Sub OnActivate(ByVal container As IObjectContainer, ByVal applicationObject As Object, ByVal storedObject As Object) Implements IObjectConstructor.OnActivate 43
End Sub 44
45
Public Function StoredClass() As System.Type Implements IObjectConstructor.StoredClass 46
Return GetType(String) 47
End Function 48
49
End Class 50
End Namespace
[/filter]
Let's clean our database and store 2 cars:
We can check the contents of our database with the following method:
We will use simple evaluation to check our cars:
In both cases we the results are correct. Native Query optimization cannot be used with the translated classes, because the actual values of the translated fields are only known after instantiation and activation. That also means that translated classes can have a considerable impact on database performance and should be used with care.