<?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="Platform Specific Issues" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Class Name Format 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#platform_specific_issues/class_name_format_in_.net.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../platform_specific_issues.htm">Platform Specific Issues</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Class Name Format 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>Class Name Format In .NET</h1> <p>Db4o uses full class name to distinguish classes within the database file. In .NET full class name has the following format:</p> <p><code>Namespace.ClassName, AssemblyName</code> </p> <p>Effectively that means that the same class definition within different assemblies (applications or libraries) will be recognized as two different classes by db4o. You should keep this in mind in the following cases:</p> <ul> <li value="1">2 or more applications working with the same database file;</li> <li value="2">client/server application with the classes deployed on both the client and the server;</li> <li value="3"><a href="asp.net.htm">ASP.NET2</a> application with dynamic compilation</li> </ul> <p>Let's use an example to see what happens in these cases. We will create 2 applications Test1.exe and Test2.exe. Both will have a simplest class definition:</p> <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 */ namespace Db4objects.Db4odoc.ClassNameFormat { class Test { public override string ToString() { return "Test"; } } }</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 Namespace Db4objects.Db4odoc.ClassNameFormat Class TestClass Test Public Overloads Overrides Function ToString() As String Return "Test" End Function End Class End Namespace</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>Test1 application will store one object of Test class to the database:</p> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">ClassNameExample1.cs: StoreObjects private static void StoreObjects() { File.Delete(Db4oFileName); IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); try { // Store a simple class to the database Test test = new Test(); container.Store(test); } finally { container.Commit(); } }</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">ClassNameExample1.vb: StoreObjects Private Shared Sub StoreObjects() File.Delete(Db4oFileName) Dim container As IObjectContainer = _ Db4oFactory.OpenFile(Db4oFileName) Try ' Store a simple class to the database Dim test As Test = New Test container.Store(test) Finally container.Commit() End Try End Sub</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>Another application (Test2) will try to read this object from the same database file. To check how the Test object was actually stored in the database we will use StoredClass API:</p> <p MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"> <pre class="prettyprint" xml:space="preserve">ClassNameExample2.cs: CheckDatabase public static void CheckDatabase() { IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); try { // Read db4o contents from another application IObjectSet result = container.QueryByExample(typeof(Test)); ListResult(result); // Check what classes are actualy stored in the database IStoredClass[] storedClasses = container.Ext().StoredClasses(); foreach (IStoredClass storedClass in storedClasses) { System.Console.WriteLine("Stored class: " + storedClass.GetName()); } } finally { container.Commit(); } }</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">ClassNameExample2.vb: CheckDatabase Private Shared Sub CheckDatabase() Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) Try ' Read db4o contents from another application Dim result As IObjectSet = container.QueryByExample(GetType(Test)) ListResult(result) ' Check what classes are actualy stored in the database Dim storedClasses As IStoredClass() = container.Ext.StoredClasses Dim storedClass As IStoredClass For Each storedClass In storedClasses System.Console.WriteLine("Stored class: " + storedClass.GetName) Next Finally container.Commit() End Try End Sub</pre> </p> <p MadCap:conditions="Global.Primary:vb" /> <p>From the example we can see that though the class has been stored to the database, it cannot be retrieved from the Test2 application, as the assembly name is different from the original.</p> <p>In order to make your classes readable from another assembly you should use one of the existing workarounds:</p> <ul> <li value="1">keep your persistent classes in a separate class library, which should be available for your application assemblies (for the example above compile the Test class into Persistent.dll);</li> <li value="2">use db4o <a href="../configuration/common/alias.htm">Aliases</a>.</li> </ul> <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="classnameformatvb.zip">VB.NET </a> </MadCap:conditionalText> <MadCap:conditionalText MadCap:conditions="Primary..NET,Primary.c#,Primary.All languages"><a href="classnameformatcs.zip">c# </a> </MadCap:conditionalText> </p> <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>