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
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02Imports Db4objects.Db4o.Types 03 04Namespace Db4objects.Db4odoc.SelectivePersistence 05 06 Class NotStorable 07 Implements ITransientClass 08 09 Public Overloads Overrides Function ToString() As String 10 Return "NotStorable class" 11 End Function 12 End Class 13End 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
01private static void SaveObjects() 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); 05 try 06 { 07 // Save Test1 object with a NotStorable class field 08 Test1 test1 = new Test1("Test1", new NotStorable()); 09 container.Set(test1); 10 // Save Test2 object with a NotStorable class field 11 Test2 test2 = new Test2("Test2", new NotStorable(), test1); 12 container.Set(test2); 13 } 14 finally 15 { 16 container.Close(); 17 } 18 }

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

Now let's try to retrieve the saved objects:

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

TransientClassExample.vb: RetrieveObjects
01Public Shared Sub RetrieveObjects() 02 Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 03 Try 04 ' retrieve the results and check if the NotStorable instances were saved 05 Dim result As IList = container.Get(Nothing) 06 ListResult(result) 07 Finally 08 container.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.