Simple Example

Let's look how to enable Transparent Activation in practice. We will take the example class from the Activation chapter and modify it to enable TA:


SensorPanelTA.cs
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02using Db4objects.Db4o; 03using Db4objects.Db4o.Activation; 04using Db4objects.Db4o.TA; 05 06namespace Db4ojects.Db4odoc.TAExamples 07{ 08 public class SensorPanelTA /*must implement Activatable for TA*/: IActivatable 09 { 10 private object _sensor; 11 12 private SensorPanelTA _next; 13 14 /*activator registered for this class*/ 15 [System.NonSerialized] 16 IActivator _activator; 17 18 public SensorPanelTA() 19 { 20 // default constructor for instantiation 21 } 22 23 public SensorPanelTA(int value) 24 { 25 _sensor = value; 26 } 27 28 /*Bind the class to the specified object container, create the activator*/ 29 public void Bind(IActivator activator) 30 { 31 if (null != _activator) 32 { 33 throw new System.InvalidOperationException(); 34 } 35 _activator = activator; 36 } 37 38 /*Call the registered activator to activate the next level, 39 * the activator remembers the objects that were already 40 * activated and won't activate them twice. 41 */ 42 protected void Activate() 43 { 44 if (_activator == null) 45 return; 46 _activator.Activate(); 47 } 48 49 public SensorPanelTA Next 50 { 51 get 52 { 53 /*activate direct members*/ 54 Activate(); 55 return _next; 56 } 57 } 58 59 public object Sensor 60 { 61 get 62 { 63 /*activate direct members*/ 64 Activate(); 65 return _sensor; 66 } 67 } 68 69 public SensorPanelTA CreateList(int length) 70 { 71 return CreateList(length, 1); 72 } 73 74 public SensorPanelTA CreateList(int length, int first) 75 { 76 int val = first; 77 SensorPanelTA root = NewElement(first); 78 SensorPanelTA list = root; 79 while (--length > 0) 80 { 81 list._next = NewElement(++val); 82 list = list.Next; 83 } 84 return root; 85 } 86 87 protected SensorPanelTA NewElement(int value) 88 { 89 return new SensorPanelTA(value); 90 } 91 92 public override string ToString() 93 { 94 return "Sensor #" + Sensor; 95 } 96 } 97 98}


SensorPanelTA.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02Imports Db4objects.Db4o 03Imports Db4objects.Db4o.Activation 04Imports Db4objects.Db4o.TA 05 06Namespace 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 Not Nothing Is _activator Then 28 Throw New System.InvalidOperationException() 29 End If 30 _activator = activator 31 End Sub 32 33 'Call the registered activator to activate the next level, 34 ' the activator remembers the objects that were already 35 ' activated and won't activate them twice. 36 Protected Sub Activate() 37 If _activator Is Nothing Then 38 Return 39 End If 40 _activator.Activate() 41 End Sub 42 43 Public ReadOnly Property NextSensor() As SensorPanelTA 44 Get 45 ' activate direct members 46 Activate() 47 Return _next 48 End Get 49 End Property 50 51 Public ReadOnly Property Sensor() As Object 52 Get 53 ' activate direct members 54 Activate() 55 Return _sensor 56 End Get 57 End Property 58 59 Public Function CreateList(ByVal length As Integer) As SensorPanelTA 60 Return CreateList(length, 1) 61 End Function 62 63 Public Function CreateList(ByVal length As Integer, ByVal first As Integer) As SensorPanelTA 64 Dim val As Integer = first 65 Dim root As SensorPanelTA = NewElement(first) 66 Dim list As SensorPanelTA = root 67 While System.Threading.Interlocked.Decrement(length) > 0 68 list._next = NewElement(System.Threading.Interlocked.Increment(val)) 69 list = list.NextSensor 70 End While 71 Return root 72 End Function 73 74 Protected Function NewElement(ByVal value As Integer) As SensorPanelTA 75 Return New SensorPanelTA(value) 76 End Function 77 78 Public Overloads Overrides Function ToString() As String 79 Return "Sensor #" + Sensor.ToString() 80 End Function 81 End Class 82End 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:

TAExample.cs: ConfigureTA
01private static IConfiguration ConfigureTA() 02 { 03 IConfiguration configuration = Db4oFactory.NewConfiguration(); 04 // set normal activation to 0 05 configuration.ActivationDepth(0); 06 // add TA support 07 configuration.Add(new TransparentActivationSupport()); 08 // activate TA diagnostics to reveal the classes that are not TA-enabled. 09 // ActivateDiagnostics(configuration); 10 return configuration; 11 }

TAExample.vb: ConfigureTA
01Private Shared Function ConfigureTA() As IConfiguration 02 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration 03 ' set normal activation to 0 04 configuration.ActivationDepth(0) 05 ' add TA support 06 configuration.Add(New TransparentActivationSupport) 07 ' activate TA diagnostics to reveal the classes that are not TA-enabled. 08 ' ActivateDiagnostics(configuration) 09 Return configuration 10 End Function

We can test TA using the configuration above:

 

TAExample.cs: StoreSensorPanel
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 }
TAExample.cs: TestActivation
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 }

 

TAExample.vb: StoreSensorPanel
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
TAExample.vb: TestActivation
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