Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 456

db4o-doc-8.0-1.fc15.i686.rpm

<?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="Advanced Features|Type Handling|Static Fields And Enums" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Storing Static Fields</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#advanced_topics/type_handling/static_fields_and_enums/static_fields_api.htm" style="">Open topic with navigation</a>
        </p>
        <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../../../advanced_topics.htm">Advanced Features</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../../type_handling.htm">Type Handling</a><span class="MCBreadcrumbsDivider"> &gt; </span><a class="MCBreadcrumbsLink" href="../static_fields_and_enums.htm">Static Fields And Enums</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Storing Static Fields</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>Storing Static Fields</h1>
        <p>By default db4o does not persist static fields. This is not necessary because static values are set for a class, not for an object. However you can set up db4o to store static fields if you want to implement constants or enumeration: <a href="../../../configuration/objectclass/persist_static_fields.htm" target="" title="" alt="" class="MCXref" xrefformat="See &quot;{paratext}&quot;">See "Persist Static Fields"</a></p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.ObjectClass(typeof (Person)).PersistStaticFieldValues();</pre>
            <div class="codesnippet-meta">ObjectConfigurationExamples.cs: Persist also the static fields
			<div class="codedownload"><a href="../../../CodeExamples/configuration/objectconfig/Example-configuration-objectconfig-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.Common.ObjectClass(GetType(Person)).PersistStaticFieldValues()</pre>
            <div class="codesnippet-meta">ObjectConfigurationExamples.vb: Persist also the static fields
			<div class="codedownload"><a href="../../../CodeExamples/configuration/objectconfig/Example-configuration-objectconfig-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p>When this setting is enabled, all non-primitive-typed static fields are stored the first time an instance of the class is stored. The values are restored every time a database file is opened afterwards, after the class meta information is loaded for this class (when the class objects are retrieved with a query, for example).</p>
        <p>Use this option with caution. This option means that static fields are stored in the database. When you change the value of this field, you need to store it explicitly again. Furthermore, db4o will replace the static value at runtime, which can lead to very subtle bugs in your application.</p>
        <p>This option does not have any effect on primitive types like ints, longs, floats etc.</p>
        <h2>Enum Class Use case</h2>
        <p style="font-weight: normal;">One of the use-cases is when you have an enumeration-class which you want to store.
		 For example we have a color-class, which also has some static colors.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">public sealed class Color
{
    public static readonly Color Black = new Color(0, 0, 0);
    public static readonly Color White = new Color(255, 255, 255);
    public static readonly Color Red = new Color(255, 0, 0);
    public static readonly Color Green = new Color(0, 255, 0);
    public static readonly Color Blue = new Color(0, 0, 255);

    private readonly int red;
    private readonly int green;
    private readonly int blue;

    private Color(int red, int green, int blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }

    public int RedValue
    {
        get { return red; }
    }

    public int GreenValue
    {
        get { return green; }
    }

    public int BlueValue
    {
        get { return blue; }
    }

    public bool Equals(Color other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return other.red == red &amp;&amp; other.green == green &amp;&amp; other.blue == blue;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof (Color)) return false;
        return Equals((Color) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = red;
            result = (result*397) ^ green;
            result = (result*397) ^ blue;
            return result;
        }
    }

    public override string ToString()
    {
        return string.Format("Red: {0}, Green: {1}, Blue: {2}", red, green, blue);
    }
}</pre>
            <div class="codesnippet-meta">Color.cs: Class as enumeration
			<div class="codedownload"><a href="../../../CodeExamples/strategies/storingstatic/Example-strategies-storingstatic-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Public NotInheritable Class Color
    Public Shared ReadOnly Black As New Color(0, 0, 0)
    Public Shared ReadOnly White As New Color(255, 255, 255)
    Public Shared ReadOnly Red As New Color(255, 0, 0)
    Public Shared ReadOnly Green As New Color(0, 255, 0)
    Public Shared ReadOnly Blue As New Color(0, 0, 255)

    Private ReadOnly m_red As Integer
    Private ReadOnly m_green As Integer
    Private ReadOnly m_blue As Integer

    Private Sub New(ByVal red As Integer, ByVal green As Integer, ByVal blue As Integer)
        Me.m_red = red
        Me.m_green = green
        Me.m_blue = blue
    End Sub

    Public ReadOnly Property RedValue() As Integer
        Get
            Return m_red
        End Get
    End Property

    Public ReadOnly Property GreenValue() As Integer
        Get
            Return m_green
        End Get
    End Property

    Public ReadOnly Property BlueValue() As Integer
        Get
            Return m_blue
        End Get
    End Property

    Public Overloads Function Equals(ByVal other As Color) As Boolean
        If ReferenceEquals(Nothing, other) Then Return False
        If ReferenceEquals(Me, other) Then Return True
        Return other.m_red = m_red AndAlso other.m_green = m_green AndAlso other.m_blue = m_blue

    End Function

    Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
        If ReferenceEquals(Nothing, obj) Then Return False
        If ReferenceEquals(Me, obj) Then Return True
        If Not Equals(obj.GetType(), GetType(Color)) Then Return False
        Return Equals(DirectCast(obj, Color))

    End Function

    Public Overrides Function GetHashCode() As Integer
        Dim hashCode As Integer = m_red
        hashCode = (hashCode * 397) Xor m_green
        hashCode = (hashCode * 397) Xor m_blue
        Return hashCode
    End Function

    Public Overrides Function ToString() As String
        Return String.Format("Red: {0}, Green: {1}, Blue: {2}", m_red, m_green, m_blue)
    End Function
End Class</pre>
            <div class="codesnippet-meta">Color.vb: Class as enumeration
			<div class="codedownload"><a href="../../../CodeExamples/strategies/storingstatic/Example-strategies-storingstatic-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p style="font-weight: normal;">We want to ensure reference equality on colors so that you easily can check for a certain color. But when we load the colors from the database you get new instances and not the same instance as in the static field. This means that comparing the references will fail.   <![CDATA[ ]]></p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">// When you enable persist static field values, you can compare by reference
// because db4o stores the static field
if (car.Color == Color.Black)
{
    Console.WriteLine("Black cars are boring");
}
else if (car.Color == Color.Red)
{
    Console.WriteLine("Fire engine?");
}</pre>
            <div class="codesnippet-meta">StoringStaticFields.cs: Compare by reference
			<div class="codedownload"><a href="../../../CodeExamples/strategies/storingstatic/Example-strategies-storingstatic-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">' When you enable persist static field values, you can compare by reference
' because db4o stores the static field
If car.Color Is Color.Black Then
    Console.WriteLine("Black cars are boring")
ElseIf car.Color Is Color.Red Then
    Console.WriteLine("Fire engine?")</pre>
            <div class="codesnippet-meta">StoringStaticFields.vb: Compare by reference
			<div class="codedownload"><a href="../../../CodeExamples/strategies/storingstatic/Example-strategies-storingstatic-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <p style="font-weight: normal;">When you enable the persist static fields option, the static fields are stored. This means that the object referenced in the static fields are loaded from the database and therefore the same instance. And the comparing the references works again.</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.ObjectClass(typeof (Color)).PersistStaticFieldValues();</pre>
            <div class="codesnippet-meta">StoringStaticFields.cs: Enable storing static fields for our color class
			<div class="codedownload"><a href="../../../CodeExamples/strategies/storingstatic/Example-strategies-storingstatic-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <div class="codesnippet" MadCap:conditions="Primary.VB.NET">
            <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.Common.ObjectClass(GetType(Color)).PersistStaticFieldValues()</pre>
            <div class="codesnippet-meta">StoringStaticFields.vb: Enable storing static fields for our color class
			<div class="codedownload"><a href="../../../CodeExamples/strategies/storingstatic/Example-strategies-storingstatic-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div>
        </div>
        <script type="text/javascript" src="../../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>