You can tune up activation settings for specific classes with the following methods:
c#:configuration.ObjectClass("yourClass").MinimumActivationDepth(minimumDepth)
configuration.ObjectClass("yourClass").MaximumActivationDepth(maximumDepth)
VB:configuration.ObjectClass("yourClass").MinimumActivationDepth(minimumDepth)
configuration.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.
01private 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
IConfiguration configuration = Db4oFactory.NewConfiguration(); 07
configuration.ObjectClass(typeof(SensorPanel)).MaximumActivationDepth(2); 08
IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 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
} 29
30
}
01Private 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(Db4oFileName) 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
01private 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
IConfiguration configuration = Db4oFactory.NewConfiguration(); 08
configuration.ObjectClass(typeof(SensorPanel)).MinimumActivationDepth(1); 09
IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 10
try 11
{ 12
Console.WriteLine("Minimum activation depth = 1"); 13
IObjectSet result = db.Get(new SensorPanel(1)); 14
ListResult(result); 15
if (result.Count >0) 16
{ 17
SensorPanel sensor = (SensorPanel)result[0]; 18
SensorPanel next = sensor.Next; 19
while (next != null) 20
{ 21
Console.WriteLine(next); 22
next = next.Next; 23
} 24
} 25
} 26
finally 27
{ 28
db.Close(); 29
} 30
}
01Private 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(Db4oFileName) 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#: configuration.ObjectClass("yourClass").CascadeOnActivate (bool)
configuration.ObjectClass("yourClass").ObjectField("field").CascadeOnActivate(bool)
VB:configuration.ObjectClass("yourClass").CascadeOnActivate (bool)
configuration.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.
01private static void TestCascadeActivate() 02
{ 03
StoreSensorPanel(); 04
IConfiguration configuration = Db4oFactory.NewConfiguration(); 05
configuration.ObjectClass(typeof(SensorPanel)).CascadeOnActivate(true); 06
IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 07
try 08
{ 09
Console.WriteLine("Cascade activation"); 10
IObjectSet result = db.Get(new SensorPanel(1)); 11
ListResult(result); 12
if (result.Count >0) 13
{ 14
SensorPanel sensor = (SensorPanel)result[0]; 15
SensorPanel next = sensor.Next; 16
while (next != null) 17
{ 18
Console.WriteLine(next); 19
next = next.Next; 20
} 21
} 22
} 23
finally 24
{ 25
db.Close(); 26
} 27
}
01Private Shared Sub TestCascadeActivate() 02
StoreSensorPanel() 03
Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 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.
01private static void TestActivateDeactivate() 02
{ 03
StoreSensorPanel(); 04
IConfiguration configuration = Db4oFactory.NewConfiguration(); 05
configuration.ActivationDepth(0); 06
IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 07
try 08
{ 09
Console.WriteLine("Object container activation depth = 0" ); 10
IObjectSet result = db.Get(new SensorPanel(1)); 11
Console.WriteLine("Sensor1:"); 12
ListResult(result); 13
SensorPanel sensor1 = (SensorPanel)result[0]; 14
TestActivated(sensor1); 15
16
Console.WriteLine("Sensor1 activated:"); 17
db.Activate(sensor1,4); 18
TestActivated(sensor1); 19
20
Console.WriteLine("Sensor5 activated:"); 21
result = db.Get(new SensorPanel(5)); 22
SensorPanel sensor5 = (SensorPanel)result[0]; 23
db.Activate(sensor5,4); 24
ListResult(result); 25
TestActivated(sensor5); 26
27
Console.WriteLine("Sensor1 deactivated:"); 28
db.Deactivate(sensor1,5); 29
TestActivated(sensor1); 30
31
// DANGER !!!. 32
// If you use Deactivate with a higher value than 1 33
// make sure that you know whereto members might branch 34
// Deactivating list1 also deactivated list5 35
Console.WriteLine("Sensor 5 AFTER DEACTIVATE OF Sensor1."); 36
TestActivated(sensor5); 37
} 38
finally 39
{ 40
db.Close(); 41
} 42
}
01Private Shared Sub TestActivateDeactivate() 02
StoreSensorPanel() 03
Dim db As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 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