Let's look at the manual Transparent Activation implementation. This example will help you to understand how TA is implemented under the hood.
We will take the example class from the Activation chapter and modify it to enable TA:
Activatable
interface (bind
method)_activator
variable to keep the current activator;activate()
method;activate()
method each time field objects are required.001/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 002
using Db4objects.Db4o; 003
using Db4objects.Db4o.Activation; 004
using Db4objects.Db4o.TA; 005
006
namespace Db4ojects.Db4odoc.TAExamples 007
{ 008
public class SensorPanelTA /*must implement Activatable for TA*/: IActivatable 009
{ 010
private object _sensor; 011
012
private SensorPanelTA _next; 013
014
/*activator registered for this class*/ 015
[System.NonSerialized] 016
IActivator _activator; 017
018
public SensorPanelTA() 019
{ 020
// default constructor for instantiation 021
} 022
023
public SensorPanelTA(int value) 024
{ 025
_sensor = value; 026
} 027
028
/*Bind the class to the specified object container, create the activator*/ 029
public void Bind(IActivator activator) 030
{ 031
if (_activator == activator) 032
{ 033
return; 034
} 035
if (activator != null && null != _activator) 036
{ 037
throw new System.InvalidOperationException(); 038
} 039
_activator = activator; 040
} 041
042
/*Call the registered activator to activate the next level, 043
* the activator remembers the objects that were already 044
* activated and won't activate them twice. 045
*/ 046
public void Activate(ActivationPurpose purpose) 047
{ 048
if (_activator == null) 049
return; 050
_activator.Activate(purpose); 051
} 052
053
public SensorPanelTA Next 054
{ 055
get 056
{ 057
/*activate direct members*/ 058
Activate(ActivationPurpose.Read); 059
return _next; 060
} 061
} 062
063
public object Sensor 064
{ 065
get 066
{ 067
/*activate direct members*/ 068
Activate(ActivationPurpose.Read); 069
return _sensor; 070
} 071
} 072
073
public SensorPanelTA CreateList(int length) 074
{ 075
return CreateList(length, 1); 076
} 077
078
public SensorPanelTA CreateList(int length, int first) 079
{ 080
int val = first; 081
SensorPanelTA root = NewElement(first); 082
SensorPanelTA list = root; 083
while (--length > 0) 084
{ 085
list._next = NewElement(++val); 086
list = list.Next; 087
} 088
return root; 089
} 090
091
protected SensorPanelTA NewElement(int value) 092
{ 093
return new SensorPanelTA(value); 094
} 095
096
public override string ToString() 097
{ 098
return "Sensor #" + Sensor; 099
} 100
} 101
102
}
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02
Imports Db4objects.Db4o 03
Imports Db4objects.Db4o.Activation 04
Imports Db4objects.Db4o.TA 05
06
Namespace Db4ojects.Db4odoc.TAExamples 07
08
Public Class SensorPanelTA ' must implement Activatable for TA 09
Implements IActivatable 10
11
Private _sensor As Object 12
Private _next As SensorPanelTA 13
14
' activator registered for this class 15
<Transient()> _ 16
Private _activator As IActivator 17
18
Public Sub New() 19
End Sub 20
21
Public Sub New(ByVal value As Integer) 22
_sensor = value 23
End Sub 24
25
' Bind the class to the specified object container, create the activator 26
Public Sub Bind(ByVal activator As IActivator) Implements IActivatable.Bind 27
If _activator Is activator Then 28
Return 29
End If 30
If Not (activator Is Nothing Or _activator Is Nothing) Then 31
Throw New System.InvalidOperationException() 32
End If 33
_activator = activator 34
End Sub 35
36
'Call the registered activator to activate the next level, 37
' the activator remembers the objects that were already 38
' activated and won't activate them twice. 39
Public Sub Activate(ByVal purpose As ActivationPurpose) Implements IActivatable.Activate 40
If _activator Is Nothing Then 41
Return 42
End If 43
_activator.Activate(purpose) 44
End Sub 45
46
Public ReadOnly Property NextSensor() As SensorPanelTA 47
Get 48
' activate direct members 49
Activate(ActivationPurpose.Read) 50
Return _next 51
End Get 52
End Property 53
54
Public ReadOnly Property Sensor() As Object 55
Get 56
' activate direct members 57
Activate(ActivationPurpose.Read) 58
Return _sensor 59
End Get 60
End Property 61
62
Public Function CreateList(ByVal length As Integer) As SensorPanelTA 63
Return CreateList(length, 1) 64
End Function 65
66
Public Function CreateList(ByVal length As Integer, ByVal first As Integer) As SensorPanelTA 67
Dim val As Integer = first 68
Dim root As SensorPanelTA = NewElement(first) 69
Dim list As SensorPanelTA = root 70
While System.Threading.Interlocked.Decrement(length) > 0 71
list._next = NewElement(System.Threading.Interlocked.Increment(val)) 72
list = list.NextSensor 73
End While 74
Return root 75
End Function 76
77
Protected Function NewElement(ByVal value As Integer) As SensorPanelTA 78
Return New SensorPanelTA(value) 79
End Function 80
81
Public Overloads Overrides Function ToString() As String 82
Return "Sensor #" + Sensor.ToString() 83
End Function 84
End Class 85
End Namespace
As you can see from the example class we can call activate()
to activate the field objects. However, transparent activation is currently not available directly on field variables, you will have to wrap them into methods.
Let's create a configuration for transparent activation:
1private static IConfiguration ConfigureTA() 2
{ 3
IConfiguration configuration = Db4oFactory.NewConfiguration(); 4
// add TA support 5
configuration.Add(new TransparentActivationSupport()); 6
// activate TA diagnostics to reveal the classes that are not TA-enabled. 7
// ActivateDiagnostics(configuration); 8
return configuration; 9
}
1Private Shared Function ConfigureTA() As IConfiguration 2
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration 3
' add TA support 4
configuration.Add(New TransparentActivationSupport) 5
' activate TA diagnostics to reveal the classes that are not TA-enabled. 6
' ActivateDiagnostics(configuration) 7
Return configuration 8
End Function
We can test TA using the configuration above:
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
SensorPanelTA list = new SensorPanelTA().CreateList(10); 11
container.Set(list); 12
} 13
finally 14
{ 15
CloseDatabase(); 16
} 17
} 18
}
01private static void TestActivation() 02
{ 03
StoreSensorPanel(); 04
IConfiguration configuration = ConfigureTA(); 05
06
IObjectContainer container = Database(configuration); 07
if (container != null) 08
{ 09
try 10
{ 11
System.Console.WriteLine("Zero activation depth"); 12
IObjectSet result = container.Get(new SensorPanelTA(1)); 13
ListResult(result); 14
if (result.Size() > 0) 15
{ 16
SensorPanelTA sensor = (SensorPanelTA)result[0]; 17
// the object is a linked list, so each call to next() 18
// will need to activate a new object 19
SensorPanelTA next = sensor.Next; 20
while (next != null) 21
{ 22
System.Console.WriteLine(next); 23
next = next.Next; 24
} 25
} 26
} 27
finally 28
{ 29
CloseDatabase(); 30
} 31
} 32
}
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 SensorPanelTA = (New SensorPanelTA).CreateList(10) 08
container.Set(list) 09
Finally 10
CloseDatabase() 11
End Try 12
End If 13
End Sub
01Private Shared Sub TestActivation() 02
StoreSensorPanel() 03
Dim configuration As IConfiguration = ConfigureTA() 04
Dim container As IObjectContainer = Database(configuration) 05
If Not (container Is Nothing) Then 06
Try 07
System.Console.WriteLine("Zero activation depth") 08
Dim result As IObjectSet = container.Get(New SensorPanelTA(1)) 09
ListResult(result) 10
If result.Size > 0 Then 11
Dim sensor As SensorPanelTA = CType(result(0), SensorPanelTA) 12
' the object is a linked list, so each call to next() 13
' will need to activate a new object 14
Dim nextSensor As SensorPanelTA = sensor.NextSensor 15
While Not (nextSensor Is Nothing) 16
System.Console.WriteLine(nextSensor) 17
nextSensor = nextSensor.NextSensor 18
End While 19
End If 20
Finally 21
CloseDatabase() 22
End Try 23
End If 24
End Sub