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 /**//* Copyright (C) 2004 - 2007 Versant Inc. http://www.db4o.com */ namespace Db4objects.Db4odoc.ClassNameFormat { class Test { public override string ToString() { return "Test"; } } }
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
Test1 application will store one object of Test class to the database:
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(); } }
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
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 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(); } }
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
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:
Download example code: