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.

Below you will get an example of a system where classes were saved to the database from a Java application and read and modified later from a .NET application. A vice versa example is reviewed in Cross-Platform Aliasing From .NET To Java.

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 IList<Pilot> result = db.Query<Pilot>(delegate(Pilot pilot) { 07 return pilot.Points % 2 == 0; 08 }); 09 for (int i = 0; i < result.Count; i++) { 10 Pilot pilot = result[i]; 11 pilot.Name = "Modified " + pilot.Name; 12 db.Set(pilot); 13 } 14 ListResult(result); 15 } 16 finally 17 { 18 db.Close(); 19 } 20 }
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.

We can read the database from the initial Java application again. Note, that no alias is required as the class definitions were created from Java:

InterLanguageExample.java: readObjects
1private static void readObjects(){ 2 ObjectContainer container = Db4o.openFile(DB4O_FILE_NAME); 3 try { 4 ObjectSet result = container.get(new Pilot(null, 0)); 5 listResult(result); 6 } finally { 7 container.close(); 8 } 9 }