You are here: Tuning > Selective Persistence > Transient Fields In .NET

Transient Fields In .NET

There are different ways to prevent fields persistence in .NET:

For example, let's create a FieldTransient attribute and mark it to prevent object persistence:

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
   {
  }
}

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

Let's use the newly-defined FieldTransient attribute and the system-provided Transient, and compare the results:

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 ;
    }
  }
}

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

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 ;
    }
  }
}

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

We will save and retrieve both Test and TestCustomized objects, having transient fields defined in different manner:

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();
      }
    }

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

You will see the identical results independently of the way the transiency is defined.