<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Tuning|Selective Persistence" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Transient Classes </title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../../Resources/Code/prettify.js"> </script> <script src="../../Resources/Code/lang-vb.js"> </script> <script src="../../Resources/Code/jquery.min.js"> </script> <script src="../../Resources/Code/ZeroClipboard.js"> </script> <script src="../../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../../index_CSH.html#tuning/selective_persistence/transient_classes.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../tuning.htm">Tuning</a><span class="MCBreadcrumbsDivider"> > </span><a class="MCBreadcrumbsLink" href="../selective_persistence.htm">Selective Persistence</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Transient Classes </span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1><a name="kanchor120"></a>Transient Classes</h1> <p>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/<a href="transient_fields_in_.net.htm">.NET</a>). But that needs some attention and additional coding. You can achieve the same result in an easier way using TransientClass interface:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <p>c#:</p> <p><code>Db4objects.Db4o.Types. ITransientClass</code> </p> </p> <p MadCap:conditions="Global.Primary:cs" /> <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"> <p>VB:</p> <p><code>Db4objects.Db4o.Types. ITransientClass</code> </p> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>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.</p> <p>An example of the TransientClass implementation is db4o object container (we do not need to save a database into itself).</p> <p>Let's look how it works on an example. We will create a simplest class implementing TransientClass interface:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">NotStorable.cs using Db4objects.Db4o.Types; namespace Db4objects.Db4odoc.SelectivePersistence { class NotStorable: ITransientClass { public override string ToString() { return "NotStorable class"; } } }</pre> </p> <p MadCap:conditions="Global.Primary:cs" /> <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"> <pre class="prettyprint lang-vb" xml:space="preserve">NotStorable.vb ' Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com Imports Db4objects.Db4o.Types Namespace Db4objects.Db4odoc.SelectivePersistence Class NotStorable Implements ITransientClass Public Overloads Overrides Function ToString() As String Return "NotStorable class" End Function End Class End Namespace</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>NotStorable class will be used as a field in two test objects: <a href="transient_classes/test1.htm">Test1</a> and <a href="transient_classes/test2.htm">Test2</a>.</p> <p>In our example we will use the default configuration and save Test1 and Test2 objects just as usual:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">TransientClassExample.cs: SaveObjects private static void SaveObjects() { File.Delete(Db4oFileName); IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); try { // Save Test1 object with a NotStorable class field Test1 test1 = new Test1("Test1", new NotStorable()); container.Store(test1); // Save Test2 object with a NotStorable class field Test2 test2 = new Test2("Test2", new NotStorable(), test1); container.Store(test2); } finally { container.Close(); } }</pre> </p> <p MadCap:conditions="Global.Primary:cs" /> <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"> <pre class="prettyprint lang-vb" xml:space="preserve">TransientClassExample.vb: SaveObjects Public Shared Sub SaveObjects() File.Delete(Db4oFileName) Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) Try ' Save Test1 object with a NotStorable class field Dim test1 As Test1 = New Test1("Test1", New NotStorable) container.Store(test1) ' Save Test2 object with a NotStorable class field Dim test2 As Test2 = New Test2("Test2", New NotStorable, test1) container.Store(test2) Finally container.Close() End Try End Sub</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>Now let's try to retrieve the saved objects:</p> <p MadCap:conditions="Global.Primary:java" /> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">TransientClassExample.cs: RetrieveObjects private static void RetrieveObjects() { IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); try { // retrieve the results and check if the NotStorable instances were saved IList result = container.QueryByExample(null); ListResult(result); } finally { container.Close(); } }</pre> </p> <p MadCap:conditions="Global.Primary:cs" /> <p MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"> <pre class="prettyprint lang-vb" xml:space="preserve">TransientClassExample.vb: RetrieveObjects Public Shared Sub RetrieveObjects() Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) Try ' retrieve the results and check if the ' NotStorable instances were saved Dim result As IList = container.QueryByExample(Nothing) ListResult(result) Finally container.Close() End Try End Sub</pre> </p> <p MadCap:conditions="Global.Primary:vb" />If you will run the example code you will see that all the instances of NotStorable class are set to null. <p MadCap:conditions="Primary.Online">Download example code:</p><p MadCap:conditions="Primary.Online"><MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.VB.NET,Primary.All languages"><a href="selectivepersistencevb.zip">VB.NET </a></MadCap:conditionalText><MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><a href="selectivepersistencecs.zip">c# </a></MadCap:conditionalText></p><script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"></script></body> </html>