You are here: Advanced Features > db4o Meta-Information

db4o Meta-Information

Db4o meta information API provides an access to the actual structure of db4o database file. Its primary use is refactoring.

You can access the meta information via extended object container. You can ask the object container for all stored classes or for a specific class. To find the meta information for a specific class you can provide the full name, the class itself or an instance of a particular type.

Note that db4o also returns information about internal db4o instances which have been stored.

// Get the information about all stored classes.
IStoredClass[] classesInDB = container.Ext().StoredClasses();
foreach (IStoredClass storedClass in classesInDB)
{
    Console.WriteLine(storedClass.GetName());
}

// Information for a certain class
IStoredClass metaInfo = container.Ext().StoredClass(typeof (Person));
MetaInfoExample.cs: All stored classes
' Get the information about all stored classes.
Dim classesInDB As IStoredClass() = container.Ext().StoredClasses()
For Each storedClass As IStoredClass In classesInDB
    Console.WriteLine(storedClass.GetName())
Next

' Information for a certain class
Dim metaInfo As IStoredClass = container.Ext().StoredClass(GetType(Person))
MetaInfoExample.vb: All stored classes

The stored class interface provides all meta information db4o knows about. You can get the name of the class, ask for the instance count, ask for a list of the ids and get the meta info for super classes.

The most important information about the stored classes meta info is the list of the field which are stored. You can get a list of all fields or ask for specific fields. Note that the meta information might return information for fields which don't exist anymore. This is useful for refactoring.

IStoredClass metaInfoForPerson = container.Ext().StoredClass(typeof (Person));
// Access all existing fields
foreach (IStoredField field in metaInfoForPerson.GetStoredFields())
{
    Console.WriteLine("Field: " + field.GetName());
}
// Accessing the field 'name' of any type.
IStoredField nameField = metaInfoForPerson.StoredField("name", null);
// Accessing the string field 'name'. Important if this field had another time in previous
// versions of the class model
IStoredField ageField = metaInfoForPerson.StoredField("age", typeof (int));

// Check if the field is indexed
bool isAgeFieldIndexed = ageField.HasIndex();

// Get the type of the field
String fieldType = ageField.GetStoredType().GetName();
MetaInfoExample.cs: Accessing stored fields
Dim metaInfoForPerson As IStoredClass = container.Ext().StoredClass(GetType(Person))
' Access all existing fields
For Each field As IStoredField In metaInfoForPerson.GetStoredFields()
    Console.WriteLine("Field: " & field.GetName())
Next
' Accessing the field 'name' of any type.
Dim nameField As IStoredField = metaInfoForPerson.StoredField("name", Nothing)
' Accessing the string field 'name'. Important if this field had another time in previous
' versions of the class model
Dim ageField As IStoredField = metaInfoForPerson.StoredField("age", GetType(Integer))

' Check if the field is indexed
Dim isAgeFieldIndexed As Boolean = ageField.HasIndex()

' Get the type of the field
Dim fieldType As String = ageField.GetStoredType().GetName()
MetaInfoExample.vb: Accessing stored fields

On a the field meta information you can find out the name, type and if the field has an index. And you also can access the values of a object via the stored field. This allows you to access information which is stored in the database but has been removed from the class model. This is useful for refactoring.

IStoredClass metaForPerson = container.Ext().StoredClass(typeof (Person));
IStoredField metaNameField = metaForPerson.StoredField("name", null);

IList<Person> persons = container.Query<Person>();
foreach (Person person in persons)
{
    string name = (string) metaNameField.Get(person);
    Console.WriteLine("Name is " + name);
}
MetaInfoExample.cs: Access via meta data
Dim metaForPerson As IStoredClass = container.Ext().StoredClass(GetType(Person))
Dim metaNameField As IStoredField = metaForPerson.StoredField("name", Nothing)

Dim persons As IList(Of Person) = container.Query(Of Person)()
For Each person As Person In persons
    Dim name As String = DirectCast(metaNameField.Get(person), [String])
    Console.WriteLine("Name is " & name)
MetaInfoExample.vb: Access via meta data