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
01public static void 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 Db4oFactory.Configure().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 }

 

AliasExample.vb: ConfigureClassAlias
1Public Shared Sub ConfigureClassAlias() 2 ' create a new alias 3 tAlias = New TypeAlias("Db4objects.Db4odoc.Aliases.Pilot, Db4objects.Db4odoc", "Db4objects.Db4odoc.Aliases.Driver, Db4objects.Db4odoc") 4 ' add the alias to the db4o configuration 5 Db4oFactory.Configure.AddAlias(tAlias) 6 ' check how does the alias resolve 7 Console.WriteLine("Stored name for Db4objects.Db4odoc.Aliases.Driver: " + tAlias.ResolveRuntimeName("Db4objects.Db4odoc.Aliases.Driver, Db4objects.Db4odoc")) 8 Console.WriteLine("Runtime name for Db4objects.Db4odoc.Aliases.Pilot: " + tAlias.ResolveStoredName("Db4objects.Db4odoc.Aliases.Pilot, Db4objects.Db4odoc")) 9 End Sub


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
01public static void SaveDrivers() 02 { 03 File.Delete(YapFileName); 04 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 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
01Public Shared Sub SaveDrivers() 02 File.Delete(YapFileName) 03 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 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
1public static void RemoveClassAlias() 2 { 3 Db4oFactory.Configure().RemoveAlias(tAlias); 4 }
AliasExample.cs: GetPilots
01public static void GetPilots() 02 { 03 IObjectContainer db = Db4oFactory.OpenFile(YapFileName); 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
1Public Shared Sub RemoveClassAlias() 2 Db4oFactory.Configure.RemoveAlias(tAlias) 3 End Sub
AliasExample.vb: GetPilots
1Public Shared Sub GetPilots() 2 Dim db As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 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.