TNull Translator

TNull translator is used to notify db4o engine that the class data should not be stored. Db4o uses this translator internally for delegates.

Let's look at an example:

NotStorable.cs
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02namespace Db4objects.Db4odoc.BuiltInTranslators 03{ 04 public class NotStorable 05 { 06 string _name; 07 public NotStorable(string name) 08 { 09 _name = name; 10 } 11 12 public override string ToString() 13 { 14 return _name == null ? "null" : _name; 15 } 16 } 17}
BuiltInTranslatorsExample.cs: SaveNotStorable
01public static void SaveNotStorable() 02 { 03 File.Delete(Db4oFileName); 04 IConfiguration configuration = Db4oFactory.NewConfiguration(); 05 // Configure NotStorable class with TNull translator to prevent its storage 06 configuration.ObjectClass(typeof(NotStorable)).Translate(new TNull()); 07 IObjectContainer container = Database(configuration); 08 if (container != null) 09 { 10 try 11 { 12 NotStorable ns = new NotStorable("test1"); 13 container.Set(ns); 14 ns = new NotStorable("test2"); 15 container.Set(ns); 16 } 17 catch (Db4oException ex) 18 { 19 System.Console.WriteLine("Db4o Exception: " + ex.Message); 20 } 21 catch (Exception ex) 22 { 23 System.Console.WriteLine("System Exception: " + ex.Message); 24 } 25 finally 26 { 27 CloseDatabase(); 28 } 29 } 30 }
BuiltInTranslatorsExample.cs: TestTNull
01public static void TestTNull() 02 { 03 SaveNotStorable(); 04 IObjectContainer container = Database(); 05 if (container != null) 06 { 07 try 08 { 09 // Trying to retrieve 10 IList<NotStorable> result = container.Query<NotStorable>(); 11 // As the class is configured with TNull, the data should be null 12 ListResult(result); 13 } 14 catch (Db4oException ex) 15 { 16 System.Console.WriteLine("Db4o Exception: " + ex.Message); 17 } 18 catch (Exception ex) 19 { 20 System.Console.WriteLine("System Exception: " + ex.Message); 21 } 22 finally 23 { 24 CloseDatabase(); 25 } 26 } 27 }
NotStorable.vb
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02 03Namespace Db4objects.Db4odoc.BuiltInTranslators 04 Public Class NotStorable 05 Private _name As String 06 Public Sub New(ByVal name As String) 07 _name = name 08 End Sub 09 10 Public Overloads Overrides Function ToString() As String 11 Return IIf(_name Is Nothing, "null", _name) 12 End Function 13 End Class 14End Namespace

BuiltInTranslatorsExample.vb: SaveNotStorable
01Public Shared Sub SaveNotStorable() 02 File.Delete(Db4oFileName) 03 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 04 ' Configure NotStorable class with TNull translator to prevent its storage 05 configuration.ObjectClass(GetType(NotStorable)).Translate(New TNull()) 06 Dim container As IObjectContainer = Database(configuration) 07 If container IsNot Nothing Then 08 Try 09 Dim ns As New NotStorable("test1") 10 container.[Set](ns) 11 ns = New NotStorable("test2") 12 container.[Set](ns) 13 Catch ex As Db4oException 14 System.Console.WriteLine("Db4o Exception: " + ex.Message) 15 Catch ex As Exception 16 System.Console.WriteLine("System Exception: " + ex.Message) 17 Finally 18 CloseDatabase() 19 End Try 20 End If 21 End Sub
BuiltInTranslatorsExample.vb: TestTNull
01Public Shared Sub TestTNull() 02 SaveNotStorable() 03 Dim container As IObjectContainer = Database() 04 If container IsNot Nothing Then 05 Try 06 ' Trying to retrieve 07 Dim result As IList(Of NotStorable) = container.Query(Of NotStorable)() 08 ' As the class is configured with TNull, the data should be null 09 ListResult(result) 10 Catch ex As Db4oException 11 System.Console.WriteLine("Db4o Exception: " + ex.Message) 12 Catch ex As Exception 13 System.Console.WriteLine("System Exception: " + ex.Message) 14 Finally 15 CloseDatabase() 16 End Try 17 End If 18 End Sub

If you will run this example you will see that though the information about the NotStorable class was saved to the database, the retrieved value is null.