IActivatable Interface

Summary

The IActivatable interface must be implemented by classes in order to support transparent activation/persistence

The IActivatable interface may be added to persistent classes by hand or by using the db4o instrumentation (Db4oTools). For further information instrumentation see the chapter "Enhancement" in the db4o reference documentation.
graph BT Type["IActivatable"] class Type type-node Implementing0["ActivatableDictionary<TKey, TValue>"]-.->Type click Implementing0 "/db4o-gpl-doc/output/api/Db4objects.Db4o.Collections/ActivatableDictionary_2" Implementing1["ArrayList4<E>"]-.->Type click Implementing1 "/db4o-gpl-doc/output/api/Db4objects.Db4o.Collections/ArrayList4_1" Implementing2["ArrayDictionary4<K, V>"]-.->Type click Implementing2 "/db4o-gpl-doc/output/api/Db4objects.Db4o.Collections/ArrayDictionary4_2" Implementing3["ActivatableList<T>"]-.->Type click Implementing3 "/db4o-gpl-doc/output/api/Db4objects.Db4o.Collections/ActivatableList_1" Implementing4["IActivatableCollection<T>"]-.->Type click Implementing4 "/db4o-gpl-doc/output/api/Db4objects.Db4o.Collections/IActivatableCollection_1" Implementing5["ActivatableBase"]-.->Type click Implementing5 "/db4o-gpl-doc/output/api/Db4objects.Db4o.Internal.Activation/ActivatableBase"

Syntax

public interface IActivatable

Remarks

The IActivatable interface must be implemented by classes in order to support transparent activation/persistence

The IActivatable interface may be added to persistent classes by hand or by using the db4o instrumentation (Db4oTools). For further information instrumentation see the chapter "Enhancement" in the db4o reference documentation.

The basic idea for Transparent Activation is:
Objects have an activation depth of 0, so they are not activated at all. Whenever a method is called on such an object, the first thing to do before actually executing the method body is to activate the object to populating its direct members.

To illustrate this approach, we will use the following simple class.


    public class Item
    {
       private Item next;
    
       public Item(Item next)
       {
          this.next = next;
       }
    
       public Item Next
       {
          get { return next; }
          set { next = value; }
       }
    
       // ...
    }
    }
The basic sequence of actions to get the above scheme to work is the following:

Whenever an object is instantiated from db4o, the database registers an activator for this object. To enable this, the object has to implement the Activatable interface and provide the according bind(Activator) method. The default implementation of the bind method will simply store the given activator reference for later use.

public class Item : IActivatable
{
   private Item next;
   [NonSerialized] private IActivator activator;

   public void Bind(IActivator activator)
   {
      if (this.activator == activator)
      {
         return;
      }
      if (activator != null && null != this.activator)
      {
         throw new InvalidOperationException("Object can only be bound to one activator");
      }
      this.activator = activator;
   }

   public void Activate(ActivationPurpose activationPurpose)
   {
      if (null != activator)
      {
         activator.Activate(activationPurpose);
      }
   }

   // ...
}
The first action in every method body of an activatable object should be a call to the corresponding activator's Activate() method with the given purpose. If the method is reading the purpose should be Db4objects.Db4o.Activation.ActivationPurpose.Read, for writing Db4objects.Db4o.Activation.ActivationPurpose.Write.

public class Item : IActivatable
    {
       private Item next;
       [NonSerialized] private IActivator activator;
    
       public void Bind(IActivator activator)
       {
          if (this.activator == activator)
          {
             return;
          }
          if (activator != null && null != this.activator)
          {
             throw new InvalidOperationException("Object can only be bound to one activator");
          }
          this.activator = activator;
       }
    
       public void Activate(ActivationPurpose activationPurpose)
       {
          if (null != activator)
          {
             activator.Activate(activationPurpose);
          }
       }
    
       public Item Next
       {
          get
          {
             Activate(ActivationPurpose.Read);
             return next;
          }
          set
          {
             Activate(ActivationPurpose.Write);
             next = value;
          }
       }
    
       // ...
    }
    
You always need to call Activate() before any data access in the object. Otherwise transparent activation / persistence will not work. Since this process is error prone we recommend to use the enhancer tools shipped with db4o. The Activate() method will check whether the object is already activated. If this is not the case, it will request the container to activate the object to level 1 and set the activated flag accordingly.

To instruct db4o to actually use these hooks (i.e. to register the database when instantiating an object), Db4objects.Db4o.TA.TransparentActivationSupport or Db4objects.Db4o.TA.TransparentPersistenceSupport has to be registered with the db4o configuration.

ICommonConfiguration config = ... // your configuration
config.Add(new TransparentActivationSupport());

Methods

Name Value Summary
Activate(ActivationPurpose) void
Should be called by every reading field access of an object.
Bind(IActivator) void
Called by db4o after the object instantiation.