<?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 Fields In .NET </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_fields_in_.net.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 Fields In .NET</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="kanchor119"></a>Transient Fields In .NET</h1>There are different ways to prevent fields persistence in .NET: <ul><li value="1">You can use the [com.db4o.Transient] or [NonSerialized] attribute to indicate that a field is not part of the persistent state of an object.</li><li value="2">You can use any built-in .NET attribute or define your own attribute class and mark it transient for db4o.</li></ul><p>For example, let's create a FieldTransient attribute and mark it to prevent object persistence:</p><p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><pre class="prettyprint" xml:space="preserve">FieldTransient.cs /**//* Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com */ using System; namespace Db4objects.Db4odoc.SelectivePersistence { [AttributeUsage(AttributeTargets.Field)] public class FieldTransient: Attribute { } }</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">FieldTransient.vb ' Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com Namespace Db4objects.Db4odoc.SelectivePersistence <AttributeUsage(AttributeTargets.Field)> _ Public Class FieldTransient Inherits Attribute End Class End Namespace</pre></p><p MadCap:conditions="Global.Primary:vb" />Let's use the newly-defined FieldTransient attribute and the system-provided Transient, and compare the results: <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><pre class="prettyprint" xml:space="preserve">Test.cs /**//* Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com */ using System; namespace Db4objects.Db4odoc.SelectivePersistence { public class Test { [Db4objects.Db4o.Transient] // you can also use [NonSerializedAttribute] string _transientField; string _persistentField; public Test(string transientField, string persistentField) { _transientField = transientField; _persistentField = persistentField; } public override string ToString() { return "Test: persistent: " + _persistentField + ", transient: " + _transientField ; } } }</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">Test.vb ' Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com Imports System Imports Db4objects.Db4o Namespace Db4objects.Db4odoc.SelectivePersistence Public Class Test <Transient()> Dim _transientField As String ' You can also use <NonSerialized()> Dim _transientField As String ' and mark the class <Serializable()> Dim _persistentField As String Public Sub New(ByVal transientField As String, _ ByVal persistentField As String) _transientField = transientField _persistentField = persistentField End Sub Public Overrides Function ToString() As String Return "Test: persistent: " + _persistentField + _ ", transient: " + _transientField End Function End Class End Namespace</pre></p><p MadCap:conditions="Global.Primary:vb" /><p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><pre class="prettyprint" xml:space="preserve">TestCustomized.cs /**//* Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com */ namespace Db4objects.Db4odoc.SelectivePersistence { public class TestCustomized { [Db4objects.Db4odoc.SelectivePersistence.FieldTransient] string _transientField; string _persistentField; public TestCustomized(string transientField, string persistentField) { _transientField = transientField; _persistentField = persistentField; } public override string ToString() { return "Customized test: persistent: " + _persistentField + ", transient: " + _transientField ; } } }</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">TestCustomized.vb ' Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com Namespace Db4objects.Db4odoc.SelectivePersistence Public Class TestCusomized <Db4objects.Db4odoc.SelectivePersistence.FieldTransient()> _ Dim _transientField As String Dim _persistentField As String Public Sub New(ByVal transientField As String, _ ByVal persistentField As String) _transientField = transientField _persistentField = persistentField End Sub Public Overrides Function ToString() As String Return "Customized test: persistent: " + _persistentField + ", _ transient: " + _transientField End Function End Class End Namespace</pre></p><p MadCap:conditions="Global.Primary:vb" /><p>We will save and retrieve both Test and TestCustomized objects, having transient fields defined in different manner: </p><p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><pre class="prettyprint" xml:space="preserve">MarkTransientExample.cs: SaveObjects private static void SaveObjects(IConfiguration configuration) { File.Delete(Db4oFileName); IObjectContainer container = Db4oFactory.OpenFile(configuration, Db4oFileName); try { Test test = new Test("Transient string","Persistent string"); container.Store(test); TestCustomized testc = new TestCustomized("Transient string","Persistent string"); container.Store(testc); } 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">MarkTransientExample.vb: SaveObjects Public Shared Sub SaveObjects(ByVal configuration As IConfiguration) File.Delete(Db4oFileName) Dim container As IObjectContainer = _ Db4oFactory.OpenFile(configuration, Db4oFileName) Try Dim test As Test = New Test("Transient string", _ "Persistent string") container.Store(test) Dim testc As TestCusomized = New _ TestCusomized("Transient string", "Persistent string") container.Store(testc) Finally container.Close() End Try End Sub</pre></p><p MadCap:conditions="Global.Primary:vb" />You will see the identical results independently of the way the transiency is defined. <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js"></script></body> </html>