This topic applies to .NET version only
Let's look at Transparent Persistence example. We will use SensorPanel class from Activation example, which represents a simple linked list.
First of all we must configure the database to use TP:
1private static IConfiguration ConfigureTP() 2
{ 3
IConfiguration configuration = Db4oFactory.NewConfiguration(); 4
// add TP support 5
configuration.Add(new TransparentPersistenceSupport()); 6
return configuration; 7
}
1Private Shared Function ConfigureTP() As IConfiguration 2
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration 3
' add TP support 4
configuration.Add(New TransparentPersistenceSupport) 5
Return configuration 6
End Function
Now, we should explicitly store the objects that we want to be persistent:
01private static void StoreSensorPanel() 02
{ 03
File.Delete(Db4oFileName); 04
IObjectContainer container = Database(Db4oFactory.NewConfiguration()); 05
if (container != null) 06
{ 07
try 08
{ 09
// create a linked list with length 10 10
SensorPanel list = new SensorPanel().CreateList(10); 11
container.Store(list); 12
} 13
finally 14
{ 15
CloseDatabase(); 16
} 17
} 18
}
01Private Shared Sub StoreSensorPanel() 02
File.Delete(Db4oFileName) 03
Dim container As IObjectContainer = Database(Db4oFactory.NewConfiguration) 04
If Not (container Is Nothing) Then 05
Try 06
' create a linked list with length 10 07
Dim list As SensorPanel = (New SensorPanel).CreateList(10) 08
container.Set(list) 09
Finally 10
CloseDatabase() 11
End Try 12
End If 13
End Sub
In order to test if TP actually works, we will select all the SensorPanel objects from the database, modify them and commit the transaction. If TP took place the objects will be modified in the database:
01private static void TestTransparentPersistence() 02
{ 03
StoreSensorPanel(); 04
IConfiguration configuration = ConfigureTP(); 05
06
IObjectContainer container = Database(configuration); 07
if (container != null) 08
{ 09
try 10
{ 11
IObjectSet result = container.QueryByExample(new SensorPanel(1)); 12
ListResult(result); 13
SensorPanel sensor = null; 14
if (result.Size() > 0) 15
{ 16
System.Console.WriteLine("Before modification: "); 17
sensor = (SensorPanel)result[0]; 18
// the object is a linked list, so each call to next() 19
// will need to activate a new object 20
SensorPanel next = sensor.Next; 21
while (next != null) 22
{ 23
System.Console.WriteLine(next); 24
// modify the next sensor 25
next.Sensor = (object)(10 + (int)next.Sensor); 26
next = next.Next; 27
} 28
// Explicit commit stores and commits the changes at any time 29
container.Commit(); 30
} 31
} 32
finally 33
{ 34
// If there are unsaved changes to activatable objects, they 35
// will be implicitly saved and committed when the database 36
// is closed 37
CloseDatabase(); 38
} 39
} 40
// reopen the database and check the modifications 41
container = Database(configuration); 42
if (container != null) 43
{ 44
try 45
{ 46
IObjectSet result = container.QueryByExample(new SensorPanel(1)); 47
ListResult(result); 48
SensorPanel sensor = null; 49
if (result.Size() > 0) 50
{ 51
System.Console.WriteLine("After modification: "); 52
sensor = (SensorPanel)result[0]; 53
SensorPanel next = sensor.Next; 54
while (next != null) 55
{ 56
System.Console.WriteLine(next); 57
next = next.Next; 58
} 59
} 60
} 61
finally 62
{ 63
CloseDatabase(); 64
} 65
} 66
}
01Private Shared Sub TestTransparentPersistence() 02
StoreSensorPanel() 03
Dim configuration As IConfiguration = ConfigureTP() 04
Dim container As IObjectContainer = Database(configuration) 05
If Not (container Is Nothing) Then 06
Try 07
Dim result As IObjectSet = container.QueryByExample(New SensorPanel(1)) 08
Dim sensor As SensorPanel = Nothing 09
ListResult(result) 10
If result.Size > 0 Then 11
System.Console.WriteLine("Before modification: ") 12
sensor = CType(result(0), SensorPanel) 13
' the object is a linked list, so each call to next() 14
' will need to activate a new object 15
Dim nextSensor As SensorPanel = sensor.NextSensor 16
While Not (nextSensor Is Nothing) 17
System.Console.WriteLine(nextSensor) 18
' modify the next sensor 19
nextSensor.Sensor = CType((10 + CType(nextSensor.Sensor, Int32)), Object) 20
nextSensor = nextSensor.NextSensor 21
End While 22
' Explicit commit stores and commits the changes at any time 23
container.Commit() 24
End If 25
Finally 26
' If there are unsaved changes to activatable objects, they 27
' will be implicitely saved and committed when the database 28
' is closed 29
CloseDatabase() 30
End Try 31
End If 32
' reopen the database and check the modifications 33
container = Database(configuration) 34
If Not (container Is Nothing) Then 35
Try 36
Dim result As IObjectSet = container.QueryByExample(New SensorPanel(1)) 37
Dim sensor As SensorPanel = Nothing 38
ListResult(result) 39
If result.Size > 0 Then 40
System.Console.WriteLine("After modification: ") 41
sensor = CType(result(0), SensorPanel) 42
Dim nextSensor As SensorPanel = sensor.NextSensor 43
While Not (nextSensor Is Nothing) 44
System.Console.WriteLine(nextSensor) 45
nextSensor = nextSensor.NextSensor 46
End While 47
End If 48
Finally 49
CloseDatabase() 50
End Try 51
End If 52
End Sub
The code above is ready to accommodate TP, however TP is not possible now as SensorPanel does not implement IActivatable interface. In the following chapter we will learn how to enable TP for the project classes in the build time.
The whole project code can be downloaded here: c# vb