Object-Specific Activation

You can tune up activation settings for specific classes with the following methods:

c#:
Db4oFactory.Configure().ObjectClass("yourClass").MinimumActivationDepth(minimumDepth)
Db4oFactory.Configure().ObjectClass("yourClass").MaximumActivationDepth(maximumDepth)

VB:
Db4oFactory.Configure().ObjectClass("yourClass").MinimumActivationDepth(minimumDepth)
Db4oFactory.Configure().ObjectClass("yourClass").MaximumActivationDepth(maximumDepth)

Cascading the activation depth to member fields, the depth value is reduced by one for the field. If the depth exceeds the maximumDepth specified for the class of the object, it is reduced to the maximumDepth. If the depth value is lower than the minimumDepth it is raised to the minimumDepth.

ActivationExample.cs: TestMaxActivate
01public static void TestMaxActivate() 02 { 03 StoreSensorPanel(); 04 // note that the maximum is applied to the retrieved root object and limits activation 05 // further down the hierarchy 06 Db4oFactory.Configure().ObjectClass(typeof(SensorPanel)).MaximumActivationDepth(2); 07 08 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 09 try 10 { 11 Console.WriteLine("Maximum activation depth = 2 (default = 5)"); 12 IObjectSet result = db.Get(new SensorPanel(1)); 13 ListResult(result); 14 if (result.Count > 0) 15 { 16 SensorPanel sensor = (SensorPanel) result[0]; 17 SensorPanel next = sensor.Next; 18 while (next != null) 19 { 20 Console.WriteLine(next); 21 next = next.Next; 22 } 23 } 24 } 25 finally 26 { 27 db.Close(); 28 Db4oFactory.Configure().ObjectClass(typeof(SensorPanel)).MaximumActivationDepth(Int32.MaxValue); 29 } 30 31 }

ActivationExample.vb: TestMaxActivate
01Public Shared Sub TestMaxActivate() 02 StoreSensorPanel() 03 ' note that the maximum is applied to the retrieved root object and limits activation 04 ' further down the hierarchy 05 Db4oFactory.Configure().ObjectClass(GetType(SensorPanel)).MaximumActivationDepth(2) 06 07 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 08 Try 09 Console.WriteLine("Maximum activation depth = 2 (default = 5)") 10 Dim result As IObjectSet = db.Get(New SensorPanel(1)) 11 ListResult(result) 12 If result.Count > 0 Then 13 Dim sensor As SensorPanel = CType(result(0), SensorPanel) 14 Dim nextSensor As SensorPanel = sensor.NextSensor 15 While Not nextSensor Is Nothing 16 Console.WriteLine(nextSensor) 17 nextSensor = nextSensor.NextSensor 18 End While 19 End If 20 Finally 21 db.Close() 22 Db4oFactory.Configure().ObjectClass(GetType(SensorPanel)).MaximumActivationDepth(Int32.MaxValue) 23 End Try 24 End Sub

ActivationExample.cs: TestMinActivate
01public static void TestMinActivate() 02 { 03 StoreSensorPanel(); 04 // note that the minimum applies for *all* instances in the hierarchy 05 // the system ensures that every instantiated List object will have it's 06 // members set to a depth of 1 07 Db4oFactory.Configure().ObjectClass(typeof(SensorPanel)).MinimumActivationDepth(1); 08 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 09 try 10 { 11 Console.WriteLine("Minimum activation depth = 1"); 12 IObjectSet result = db.Get(new SensorPanel(1)); 13 ListResult(result); 14 if (result.Count >0) 15 { 16 SensorPanel sensor = (SensorPanel)result[0]; 17 SensorPanel next = sensor.Next; 18 while (next != null) 19 { 20 Console.WriteLine(next); 21 next = next.Next; 22 } 23 } 24 } 25 finally 26 { 27 db.Close(); 28 Db4oFactory.Configure().ObjectClass(typeof(SensorPanel)).MinimumActivationDepth(0); 29 } 30 }

ActivationExample.vb: TestMinActivate
01Public Shared Sub TestMinActivate() 02 StoreSensorPanel() 03 ' note that the minimum applies for *all* instances in the hierarchy 04 ' the system ensures that every instantiated List object will have it's 05 ' members set to a depth of 1 06 Db4oFactory.Configure().ObjectClass(GetType(SensorPanel)).MinimumActivationDepth(1) 07 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 08 Try 09 Console.WriteLine("Minimum activation depth = 1") 10 Dim result As IObjectSet = db.Get(New SensorPanel(1)) 11 ListResult(result) 12 If result.Count > 0 Then 13 Dim sensor As SensorPanel = CType(result(0), SensorPanel) 14 Dim nextSensor As SensorPanel = sensor.NextSensor 15 While Not nextSensor Is Nothing 16 Console.WriteLine(nextSensor) 17 nextSensor = nextSensor.NextSensor 18 End While 19 End If 20 Finally 21 db.Close() 22 Db4oFactory.Configure().ObjectClass(GetType(SensorPanel)).MinimumActivationDepth(0) 23 End Try 24 End Sub

You can set up automatic activation for specific objects or fields:

c#:
Db4oFactory.Configure().ObjectClass("yourClass").CascadeOnActivate (bool)
Db4oFactory.Configure().ObjectClass("yourClass").ObjectField("field").CascadeOnActivate(bool)

VB:
Db4oFactory.Configure().ObjectClass("yourClass").CascadeOnActivate (bool)
Db4oFactory.Configure().ObjectClass("yourClass").ObjectField("field").CascadeOnActivate(bool)
>

Cascade activation will retrieve the whole object graph, starting from the specified object(field). This setting can lead to increased memory consumption.

ActivationExample.cs: TestCascadeActivate
01public static void TestCascadeActivate() 02 { 03 StoreSensorPanel(); 04 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 05 db.Ext().Configure().ObjectClass(typeof(SensorPanel)).CascadeOnActivate(true); 06 try 07 { 08 Console.WriteLine("Cascade activation"); 09 IObjectSet result = db.Get(new SensorPanel(1)); 10 ListResult(result); 11 if (result.Count >0) 12 { 13 SensorPanel sensor = (SensorPanel)result[0]; 14 SensorPanel next = sensor.Next; 15 while (next != null) 16 { 17 Console.WriteLine(next); 18 next = next.Next; 19 } 20 } 21 } 22 finally 23 { 24 db.Close(); 25 } 26 }

ActivationExample.vb: TestCascadeActivate
01Public Shared Sub TestCascadeActivate() 02 StoreSensorPanel() 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 04 db.Ext().Configure().ObjectClass(GetType(SensorPanel)).CascadeOnActivate(True) 05 Try 06 Console.WriteLine("Cascade activation") 07 Dim result As IObjectSet = db.Get(New SensorPanel(1)) 08 ListResult(result) 09 If result.Count > 0 Then 10 Dim sensor As SensorPanel = CType(result(0), SensorPanel) 11 Dim nextSensor As SensorPanel = sensor.NextSensor 12 While Not nextSensor Is Nothing 13 Console.WriteLine(nextSensor) 14 nextSensor = nextSensor.NextSensor 15 End While 16 End If 17 Finally 18 db.Close() 19 End Try 20 End Sub

An alternative to cascade activation can be manual activation of objects:

c#: IObjectContainer#Activate(object, activationDepth);

VB: IObjectContainer#Activate(object, activationDepth);

Manual deactivation may be used to save memory:

c#:IObjectContainer#Deactivate(object, activationDepth);

VB:IObjectContainer#Deactivate(object, activationDepth);

These 2 methods give you an excellent control over object activation, but they obviously need more attention from the application side.

ActivationExample.cs: TestActivateDeactivate
01public static void TestActivateDeactivate() 02 { 03 StoreSensorPanel(); 04 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 05 db.Ext().Configure().ActivationDepth(0); 06 try 07 { 08 Console.WriteLine("Object container activation depth = 0" ); 09 IObjectSet result = db.Get(new SensorPanel(1)); 10 Console.WriteLine("Sensor1:"); 11 ListResult(result); 12 SensorPanel sensor1 = (SensorPanel)result[0]; 13 TestActivated(sensor1); 14 15 Console.WriteLine("Sensor1 activated:"); 16 db.Activate(sensor1,4); 17 TestActivated(sensor1); 18 19 Console.WriteLine("Sensor5 activated:"); 20 result = db.Get(new SensorPanel(5)); 21 SensorPanel sensor5 = (SensorPanel)result[0]; 22 db.Activate(sensor5,4); 23 ListResult(result); 24 TestActivated(sensor5); 25 26 Console.WriteLine("Sensor1 deactivated:"); 27 db.Deactivate(sensor1,5); 28 TestActivated(sensor1); 29 30 // DANGER !!!. 31 // If you use Deactivate with a higher value than 1 32 // make sure that you know whereto members might branch 33 // Deactivating list1 also deactivated list5 34 Console.WriteLine("Sensor 5 AFTER DEACTIVATE OF Sensor1."); 35 TestActivated(sensor5); 36 } 37 finally 38 { 39 db.Close(); 40 } 41 }

ActivationExample.vb: TestActivateDeactivate
01Public Shared Sub TestActivateDeactivate() 02 StoreSensorPanel() 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 04 db.Ext().Configure().ActivationDepth(0) 05 Try 06 Console.WriteLine("Object container activation depth = 0") 07 Dim result As IObjectSet = db.Get(New SensorPanel(1)) 08 Console.WriteLine("Sensor1:") 09 ListResult(result) 10 Dim sensor1 As SensorPanel = CType(result(0), SensorPanel) 11 TestActivated(sensor1) 12 13 Console.WriteLine("Sensor1 activated:") 14 db.Activate(sensor1, 4) 15 TestActivated(sensor1) 16 17 Console.WriteLine("Sensor5 activated:") 18 result = db.Get(New SensorPanel(5)) 19 Dim sensor5 As SensorPanel = CType(result(0), SensorPanel) 20 db.Activate(sensor5, 4) 21 ListResult(result) 22 TestActivated(sensor5) 23 24 Console.WriteLine("Sensor1 deactivated:") 25 db.Deactivate(sensor1, 5) 26 TestActivated(sensor1) 27 28 ' DANGER !!!. 29 ' If you use Deactivate with a higher value than 1 30 ' make sure that you know whereto members might branch 31 ' Deactivating list1 also deactivated list5 32 Console.WriteLine("Sensor 5 AFTER DEACTIVATE OF Sensor1.") 33 TestActivated(sensor5) 34 Finally 35 db.Close() 36 End Try 37 End Sub