You are here: Basics Operations & Concepts > Activation > Activation In Action

Activation In Action

Let's see db4o's activation in action. To see activation you need a deep object-graph. To keep this example simple we create a person-class with a mother-field. This allows us to simply create a very deep object graph.

First the Person class:

internal class Person
{
    private Person mother;
    private string name;

    public Person(string name)
    {
        this.name = name;
    }

    public Person(Person mother, string name)
    {
        this.mother = mother;
        this.name = name;
    }

    public Person Mother
    {
        get { return mother; }
    }

    public string Name
    {
        get { return name; }
    }
}
Person.cs: Person with a reference to the mother
Friend Class Person
    Private m_mother As Person
    Private m_name As String

    Public Sub New(ByVal name As String)
        m_mother = m_mother
        Me.m_name = name
    End Sub

    Public Sub New(ByVal mother As Person, ByVal name As String)
        Me.m_mother = mother
        Me.m_name = name
    End Sub

    Public ReadOnly Property Mother() As Person
        Get
            Return m_mother
        End Get
    End Property

    Public ReadOnly Property Name() As String
        Get
            Return m_name
        End Get
    End Property
End Class
Person.vb: Person with a reference to the mother

After that we store a deep hierarchy of persons. Let's say we store a hierarchy of seven people. Then we query for it and traverse this object graph. When we hit the sixth person, that object won't be activated, because it's outside the activation depth. That object will have all fields set to null.

Person jodie = QueryForJodie(container);

Person julia = jodie.Mother.Mother.Mother.Mother.Mother;

// This will print null
// Because julia is not activated
// and therefore all fields are not set
Console.WriteLine(julia.Name);
// This will throw a NullPointerException.
// Because julia is not activated
// and therefore all fields are not set
string joannaName = julia.Mother.Name;
ActivationDepthPitfall.cs: Run into not activated objects
Dim jodie As Person = QueryForJodie(container)

Dim julia As Person = jodie.Mother.Mother.Mother.Mother.Mother

' This will print null
' Because julia is not activated
' and therefore all fields are not set
Console.WriteLine(julia.Name)
' This will throw a NullPointerException.
' Because julia is not activated
' and therefore all fields are not set
Dim joannaName As String = julia.Mother.Name
ActivationDepthPitfall.vb: Run into not activated objects

Use Explicit Activation

When we traverse deep object graphs, we know that we might run into not activated objects. Therefore you can activate objects explicitly.

Person julia = jodie.Mother.Mother.Mother.Mother.Mother;
container.Activate(julia,5);

Console.WriteLine(julia.Name);
string joannaName = julia.Mother.Name;
Console.WriteLine(joannaName);
ActivationDepthPitfall.cs: Fix with explicit activation
Dim julia As Person = jodie.Mother.Mother.Mother.Mother.Mother
container.Activate(julia, 5)

Console.WriteLine(julia.Name)
Dim joannaName As String = julia.Mother.Name
ActivationDepthPitfall.vb: Fix with explicit activation

Configure Activation

You can configure db4o to increase the activation depth. You can increase it globally or for certain classes. Or you can cascade activate certain objects.

However remember that activation is there to improve the performance and save memory. If you set the activation depth to high it will hurt the performance.

Transparent Activation

If you have a very complex model or don't want to deal with all the activation hassle then transparent activation is the best option. Transparent activation will manage the activation for you. See "Transparent Activation"