[This functionality is deprecated]
db4o provides you with a possibility to create a mapping from a class in the database to a runtime class.
c#:
IObjectClass.ReadAs(object clazz)
VB:
IObjectClass.ReadAs(Type clazz)
clazz
parameter specifies a runtime class,
which will be used to instantiate objects from the database.
The use-case is the following:
c#:
Db4oFactory.Configure().ObjectClass(typeof(A)).readAs(typeof(B))
VB:
IObjectClass.ReadAs(Type clazz)
This configuration should be set before opening a database file.
The mapping functionality is similar to Aliases, but more limited.
Let's look at an example.
We will use 2 identical classes Pilot and PilotReplacement.
Objects of Pilot class will be saved to the database:
01private static void StoreObjects() 02
{ 03
File.Delete(Db4oFileName); 04
IObjectContainer container = Db4oFactory.OpenFile(Db4oFileName); 05
try 06
{ 07
Pilot pilot = new Pilot("Michael Schumacher", 100); 08
container.Set(pilot); 09
pilot = new Pilot("Rubens Barichello", 99); 10
container.Set(pilot); 11
} 12
finally 13
{ 14
container.Close(); 15
} 16
}
01Private Shared Sub StoreObjects() 02
File.Delete(Db4oFileName) 03
Dim container As IObjectContainer = Db4oFactory.OpenFile(Db4oFileName) 04
Try 05
Dim pilot As Pilot = New Pilot("Michael Schumacher", 100) 06
container.Set(pilot) 07
pilot = New Pilot("Rubens Barichello", 99) 08
container.Set(pilot) 09
Finally 10
container.Close() 11
End Try 12
End Sub
Let's try to retrieve the persisted objects using PilotReplacement class:
01private static void RetrieveObjects() 02
{ 03
IConfiguration configuration = Db4oFactory.NewConfiguration(); 04
configuration.ObjectClass(typeof(Pilot)).ReadAs(typeof(PilotReplacement)); 05
IObjectContainer container = Db4oFactory.OpenFile(configuration, Db4oFileName); 06
try 07
{ 08
IQuery query = container.Query(); 09
query.Constrain(typeof(PilotReplacement)); 10
IObjectSet result = query.Execute(); 11
ListResult(result); 12
} 13
finally 14
{ 15
container.Close(); 16
} 17
}
01Private Shared Sub RetrieveObjects() 02
Dim configuration As IConfiguration 03
configuration.ObjectClass(GetType(Pilot)).ReadAs(GetType(PilotReplacement)) 04
Dim container As IObjectContainer = Db4oFactory.OpenFile(configuration, Db4oFileName) 05
Try 06
Dim query As IQuery = container.Query 07
query.Constrain(GetType(PilotReplacement)) 08
Dim result As IObjectSet = query.Execute 09
ListResult(result) 10
Finally 11
container.Close() 12
End Try 13
End Sub
If meta information
for this mapping class has been stored before to the database file,
readAs
method will have no effect.