You are here: Advanced Features > Refactoring And Schema Evolution > Refactoring Class Hierarchy > Inserting Class Into a Hierarchy

Inserting Class Into A Hierarchy

In this example we have a Human-class witch inherits from the Mammal class. Now we want to introduce a new Primate class and let the Human class inherit from it.

Unfortunately db4o doesn't support this kind of refactoring. We need to use a work-around. Basically we create a copy of the Human-class with the new Inheritance-hierarchy and the copy the existing data over.

Step by Step

  1. Create the new Primate-class
  2. Create a copy of the Human-class, for example HumanNew!
  3. Change the HumanNew class to inherit from the new Primate-class instead of the Mammal-class.
  4. After that, load all existing Human-instances, copy the values over to HumanNew-instances. Store the HumanNew-instance and delete the old Human-instances

Now the objects have the new inheritance hierarchy. You can delete the old Human-class.

IList<Human> allMammals = container.Query<Human>();
foreach (Human oldHuman in allMammals)
{
    HumanNew newHuman = new HumanNew("");
    newHuman.BodyTemperature = oldHuman.BodyTemperature;
    newHuman.IQ = oldHuman.IQ;
    newHuman.Name = oldHuman.Name;

    container.Store(newHuman);
    container.Delete(oldHuman);
}
AddClassToHierarchy.cs: copy the data from the old type to the new one
Dim allMammals As IList(Of Human) = container.Query(Of Human)()
For Each oldHuman As Human In allMammals
    Dim newHuman As New HumanNew("")
    newHuman.BodyTemperature = oldHuman.BodyTemperature
    newHuman.IQ = oldHuman.IQ
    newHuman.Name = oldHuman.Name

    container.Store(newHuman)
    container.Delete(oldHuman)
Next
AddClassToHierarchy.vb: copy the data from the old type to the new one

Note that this example doesn't change existing references from the old instances to the new ones. You need to do this manually as well.