You are here: Advanced Features > Refactoring And Schema Evolution > Refactoring Class Hierarchy > Removing Class From A Hierarchy

Removing Class From A Hierarchy

In this example we have a Human class which inherits from the Primate class. Now we want to remove the Primate class and let the Human class inherit directly from the Mammal class.

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 a copy of the Human class, for example HumanNew!
  2. Change the inheritance of the HumanNew-class to inherit directly from the Mammal-class.
  3. 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);
}
RemoveClassFromHierarchy.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)
RemoveClassFromHierarchy.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.