Cross-Platform Aliasing

One of the most valuable aliases usecases is working with persistent Java classes from a .NET application and vice versa. You can use both TypeAlias and WildcardAlias depending on how many classes you need to access.

For example, Pilot objects are saved to a database from a Java application:

InterLanguageExample.java: saveObjects
01private static void saveObjects(){ 02 new File(DB4O_FILE_NAME ).delete(); 03 ObjectContainer container = Db4o.openFile(DB4O_FILE_NAME); 04 try { 05 Pilot pilot = new Pilot("David Barrichello",99); 06 container.set(pilot); 07 pilot = new Pilot("Michael Schumacher",100); 08 container.set(pilot); 09 } finally { 10 container.close(); 11 } 12 }

In order to read the saved objects successfully from a .NET application we will need to define an alias for persistent classes and an alias for the Db4oDatabase class. We will use a wildcard alias for all the persistent classes:

InterLanguageExample.cs: ConfigureAlias
1private static IConfiguration ConfigureAlias() 2 { 3 IConfiguration configuration = Db4oFactory.NewConfiguration(); 4 configuration.AddAlias(new WildcardAlias("com.db4odoc.aliases.*", "Db4objects.Db4odoc.Aliases.*, Db4objects.Db4odoc")); 5 configuration.AddAlias(new TypeAlias("com.db4o.ext.Db4oDatabase", "Db4objects.Db4o.Ext.Db4oDatabase, Db4objects.Db4o")); 6 return configuration; 7 }

InterLanguageExample.vb: ConfigureAlias
1Public Shared Function ConfigureAlias() As IConfiguration 2 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 3 configuration.AddAlias(New WildcardAlias("com.db4odoc.aliases.*", "Db4objects.Db4odoc.Aliases.*, Db4objects.Db4odoc")) 4 configuration.AddAlias(New TypeAlias("com.db4o.ext.Db4oDatabase", "Db4objects.Db4o.Ext.Db4oDatabase, Db4objects.Db4o")) 5 Return configuration 6 End Function

Now the objects are accessible from the .NET application:

InterLanguageExample.cs: GetObjects
01private static void GetObjects(IConfiguration configuration) 02 { 03 IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 04 try 05 { 06 IObjectSet result = db.Query(typeof(Pilot)); 07 ListResult(result); 08 } 09 finally 10 { 11 db.Close(); 12 } 13 }

InterLanguageExample.vb: GetObjects
1Public Shared Sub GetObjects(ByVal configuration As IConfiguration) 2 Dim db As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 3 Try 4 Dim result As IObjectSet = db.Query(GetType(Pilot)) 5 ListResult(result) 6 Finally 7 db.Close() 8 End Try 9 End Sub

One thing to remember: field names in class definitions in Java and .NET should be exactly the same.