TypeAlias

TypeAlias constructor accepts 2 parameters:

Note that the runtimeType class should exist in your application when you configure the alias.

The alias matches are found by comparing full names of the stored and runtime classes

Let's declare a new TypeAlias

c#:

private static TypeAlias tAlias;

VB:

Private Shared tAlias As TypeAlias

The following method will initialize tAlias and configure db4o to use it:

AliasExample.cs: ConfigureClassAlias
01private static IConfiguration ConfigureClassAlias() 02 { 03 // create a new alias 04 tAlias = new TypeAlias("Db4objects.Db4odoc.Aliases.Pilot, Db4objects.Db4odoc", "Db4objects.Db4odoc.Aliases.Driver, Db4objects.Db4odoc"); 05 // add the alias to the db4o configuration 06 IConfiguration configuration = Db4oFactory.NewConfiguration(); 07 configuration.AddAlias(tAlias); 08 // check how does the alias resolve 09 Console.WriteLine("Stored name for Db4objects.Db4odoc.Aliases.Driver: " + tAlias.ResolveRuntimeName("Db4objects.Db4odoc.Aliases.Driver, Db4objects.Db4odoc")); 10 Console.WriteLine("Runtime name for Db4objects.Db4odoc.Aliases.Pilot: " + tAlias.ResolveStoredName("Db4objects.Db4odoc.Aliases.Pilot, Db4objects.Db4odoc")); 11 return configuration; 12 }
AliasExample.vb: ConfigureClassAlias
01Private Shared Function ConfigureClassAlias() As IConfiguration 02 ' create a new alias 03 tAlias = New TypeAlias("Db4objects.Db4odoc.Aliases.Pilot, Db4objects.Db4odoc", "Db4objects.Db4odoc.Aliases.Driver, Db4objects.Db4odoc") 04 ' add the alias to the db4o configuration 05 Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 06 configuration.AddAlias(tAlias) 07 ' check how does the alias resolve 08 Console.WriteLine("Stored name for Db4objects.Db4odoc.Aliases.Driver: " + tAlias.ResolveRuntimeName("Db4objects.Db4odoc.Aliases.Driver, Db4objects.Db4odoc")) 09 Console.WriteLine("Runtime name for Db4objects.Db4odoc.Aliases.Pilot: " + tAlias.ResolveStoredName("Db4objects.Db4odoc.Aliases.Pilot, Db4objects.Db4odoc")) 10 Return configuration 11 End Function

We can always check the results of adding an alias using resolveRuntimeName and resolveStoredName as you see in the example. Basically the same methods are used internally by db4o to resolve aliased names.

AliasExample.cs: SaveDrivers
01private static void SaveDrivers(IConfiguration configuration) 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer db = Db4oFactory.OpenFile(configuration, Db4oFileName); 05 try 06 { 07 Driver driver = new Driver("David Barrichello", 99); 08 db.Set(driver); 09 driver = new Driver("Kimi Raikkonen", 100); 10 db.Set(driver); 11 } 12 finally 13 { 14 db.Close(); 15 } 16 }
AliasExample.vb: SaveDrivers
01Private Shared Sub SaveDrivers(ByVal configuration As IConfiguration) 02 File.Delete(Db4oFileName) 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 04 Try 05 Dim driver As Driver = New Driver("David Barrichello", 99) 06 db.Set(driver) 07 driver = New Driver("Kimi Raikkonen", 100) 08 db.Set(driver) 09 Finally 10 db.Close() 11 End Try 12 End Sub

Due to the alias configured the database will have Pilot classes saved. You can check it using ObjectManager or you can remove alias and read it from the database:

AliasExample.cs: RemoveClassAlias
1private static void RemoveClassAlias(IConfiguration configuration) 2 { 3 configuration.RemoveAlias(tAlias); 4 }
AliasExample.cs: GetPilots
01private static void GetPilots(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 }

AliasExample.vb: RemoveClassAlias
1Private Shared Sub RemoveClassAlias(ByRef configuration As IConfiguration) 2 configuration.RemoveAlias(tAlias) 3 End Sub
AliasExample.vb: GetPilots
1Private Shared Sub GetPilots(ByVal configuration As IConfiguration) 2 Dim db As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 3 Try 4 Dim result As IObjectSet = db.Get(GetType(Db4objects.Db4odoc.Aliases.Pilot)) 5 ListResult(result) 6 Finally 7 db.Close() 8 End Try 9 End Sub

Obvioulsly you can install the same alias again and read the stored objects using Driver class.