Class Name Format In .NET

This topic applies to .NET version only 

Db4o uses full class name to distinguish classes within the database file. In .NET full class name has the following format:

Namespace.ClassName, AssemblyName

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:

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:

Test.cs
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02namespace Db4objects.Db4odoc.ClassNameFormat 03{ 04 class Test 05 { 06 public override string ToString() 07 { 08 return "Test"; 09 } 10 } 11}
Test.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02Namespace Db4objects.Db4odoc.ClassNameFormat 03 04 Class Test 05 06 Public Overloads Overrides Function ToString() As String 07 Return "Test" 08 End Function 09 End Class 10End Namespace

Test1 application will store one object of Test class to the database:

ClassNameExample1.cs: StoreObjects
01private static void StoreObjects() 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); 05 try 06 { 07 // Store a simple class to the database 08 Test test = new Test(); 09 container.Set(test); 10 } 11 finally 12 { 13 container.Commit(); 14 } 15 }
ClassNameExample1.vb: StoreObjects
01Private Shared Sub StoreObjects() 02 File.Delete(Db4oFileName) 03 Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 04 Try 05 ' Store a simple class to the database 06 Dim test As Test = New Test 07 container.Set(test) 08 Finally 09 container.Commit() 10 End Try 11 End Sub

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:

ClassNameExample2.cs: CheckDatabase
01public static void CheckDatabase() 02 { 03 IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); 04 try 05 { 06 // Read db4o contents from another application 07 IObjectSet result = container.Get(typeof(Test)); 08 ListResult(result); 09 // Check what classes are actualy stored in the database 10 IStoredClass[] storedClasses = container.Ext().StoredClasses(); 11 foreach (IStoredClass storedClass in storedClasses) 12 { 13 System.Console.WriteLine("Stored class: " + storedClass.GetName()); 14 } 15 } 16 finally 17 { 18 container.Commit(); 19 } 20 }
ClassNameExample2.vb: CheckDatabase
01Private Shared Sub CheckDatabase() 02 Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 03 Try 04 ' Read db4o contents from another application 05 Dim result As IObjectSet = container.Get(GetType(Test)) 06 ListResult(result) 07 ' Check what classes are actualy stored in the database 08 Dim storedClasses As IStoredClass() = container.Ext.StoredClasses 09 Dim storedClass As IStoredClass 10 For Each storedClass In storedClasses 11 System.Console.WriteLine("Stored class: " + storedClass.GetName) 12 Next 13 Finally 14 container.Commit() 15 End Try 16 End Sub

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.

In order to make your classes readable from another assembly you should use one of the existing workarounds: