Transient Classes

Some of the classes are not supposed to be persistent. Of course you can avoid saving their instances in your code and mark all their occurrences in another classes as transient (Java/.NET). But that needs some attention and additional coding. You can achieve the same result in an easier way using TransientClass interface:

c#:

Db4objects.Db4o.Types. ITransientClass

VB:

Db4objects.Db4o.Types. ITransientClass

TransientClass is a marker interface, which guarantees that the classes implementing it will never be added to the class metadata. In fact they are just skipped silently by db4o persistence mechanism.

An example of the TransientClass implementation is db4o object container (we do not need to save a database into itself).

Let's look how it works on an example. We will create a simplest class implementing TransientClass interface:

NotStorable.cs
01using Db4objects.Db4o.Types; 02 03namespace Db4objects.Db4odoc.selectivepersistence 04{ 05 class NotStorable: ITransientClass 06 { 07 public override string ToString() 08 { 09 return "NotStorable class"; 10 } 11 } 12}

NotStorable.vb
01Imports Db4objects.Db4o.Types 02Namespace Db4objects.Db4odoc.SelectivePersistence 03 04 Class NotStorable 05 Implements ITransientClass 06 07 Public Overloads Overrides Function ToString() As String 08 Return "NotStorable class" 09 End Function 10 End Class 11End Namespace

NotStorable class will be used as a field in two test objects: Test1 and Test2.

In our example we will use the default configuration and save Test1 and Test2 objects just as usual:

TransientClassExample.cs: SaveObjects
01public static void SaveObjects() 02 { 03 File.Delete(YapFileName); 04 IObjectContainer oc = Db4oFactory.OpenFile(YapFileName); 05 try 06 { 07 // Save Test1 object with a NotStorable class field 08 Test1 test1 = new Test1("Test1", new NotStorable()); 09 oc.Set(test1); 10 // Save Test2 object with a NotStorable class field 11 Test2 test2 = new Test2("Test2", new NotStorable(), test1); 12 oc.Set(test2); 13 } 14 finally 15 { 16 oc.Close(); 17 } 18 }

TransientClassExample.vb: SaveObjects
01Public Shared Sub SaveObjects() 02 File.Delete(YapFileName) 03 Dim oc As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 04 Try 05 ' Save Test1 object with a NotStorable class field 06 Dim test1 As Test1 = New Test1("Test1", New NotStorable) 07 oc.Set(test1) 08 ' Save Test2 object with a NotStorable class field 09 Dim test2 As Test2 = New Test2("Test2", New NotStorable, test1) 10 oc.Set(test2) 11 Finally 12 oc.Close() 13 End Try 14 End Sub

Now let's try to retrieve the saved objects:

TransientClassExample.cs: RetrieveObjects
01public static void RetrieveObjects() 02 { 03 IObjectContainer oc = Db4oFactory.OpenFile(YapFileName); 04 try 05 { 06 // retrieve the results and check if the NotStorable instances were saved 07 IList result = oc.Get(null); 08 ListResult(result); 09 } 10 finally 11 { 12 oc.Close(); 13 } 14 }

TransientClassExample.vb: RetrieveObjects
01Public Shared Sub RetrieveObjects() 02 Dim oc As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 03 Try 04 ' retrieve the results and check if the NotStorable instances were saved 05 Dim result As IList = oc.Get(Nothing) 06 ListResult(result) 07 Finally 08 oc.Close() 09 End Try 10 End Sub

If you will run the example code you will see that all the instances of NotStorable class are set to null.