The reviewed refactoring types are fairly easy. It gets more difficult when you need to change a field's type.
If you modify a field's type, db4o internally creates a new field of the same name, but with the new type. The values of the old typed field are still present, but hidden. If you will change the type back to the old type the old values will still be there.
You can access the values of the previous field data using StoredField API.
c#: IStoredClass#StoredField(name, type)
VB: IStoredClass#StoredField(name, type)
gives you access to the field, which type was changed.
c#: IStoredField#Get(Object)
VB: IStoredField#Get(Object)
allows you to get the old field value for the specified object.
To see how it works on example, let's change Pilot's field name from type string to type Identity:
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02
using System; 03
04
namespace Db4objects.Db4odoc.Refactoring.NewClasses 05
{ 06
public class Identity 07
{ 08
private string _name; 09
private string _id; 10
11
public Identity(string name, string id) 12
{ 13
_name = name; 14
_id = id; 15
} 16
17
public string Name 18
{ 19
get 20
{ 21
return _name; 22
} 23
set 24
{ 25
_name=value; 26
} 27
} 28
29
30
public String toString() 31
{ 32
return string.Format("{0}[{1}]", _name, _id); 33
} 34
} 35
}
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02
Imports System 03
04
Namespace Db4objects.Db4odoc.Refactoring.Newclasses 05
Public Class Identity 06
Private _name As String 07
Private _id As String 08
09
Public Sub New(ByVal name As String, ByVal id As String) 10
_name = name 11
_id = id 12
End Sub 13
14
Public Property Name() As String 15
Get 16
Return _name 17
End Get 18
Set(ByVal Value As String) 19
_name = Value 20
End Set 21
End Property 22
23
24
Public Function toString() As String 25
Return String.Format("{0}[{1}]", _name, _id) 26
End Function 27
End Class 28
End Namespace
Now to access old "name" values and transfer them to the new "name" we can use the following procedure:
01public static void TransferValues() 02
{ 03
IObjectContainer oc = Db4oFactory.OpenFile(YapFileName); 04
try 05
{ 06
IStoredClass sc = oc.Ext().StoredClass(typeof(Pilot)); 07
System.Console.WriteLine("Stored class: "+ sc.ToString()); 08
IStoredField sfOld = sc.StoredField("_name",typeof(string)); 09
System.Console.WriteLine("Old field: "+ sfOld.ToString()+";"+sfOld.GetStoredType()); 10
IQuery q = oc.Query(); 11
q.Constrain(typeof(Pilot)); 12
IObjectSet result = q.Execute(); 13
for (int i = 0; i< result.Size(); i++) 14
{ 15
Pilot pilot = (Pilot)result[i]; 16
pilot.Name = new Identity(sfOld.Get(pilot).ToString(),""); 17
System.Console.WriteLine("Pilot="+ pilot); 18
oc.Set(pilot); 19
} 20
} 21
finally 22
{ 23
oc.Close(); 24
} 25
}
01Public Shared Sub TransferValues() 02
Dim oc As IObjectContainer = Db4oFactory.OpenFile(YapFileName) 03
Try 04
Dim sc As IStoredClass = oc.Ext().StoredClass(GetType(Pilot)) 05
System.Console.WriteLine("Stored class: " + sc.GetName()) 06
Dim sfOld As IStoredField = sc.StoredField("_name", GetType(String)) 07
System.Console.WriteLine("Old field: " + sfOld.GetName() + ";" + sfOld.GetStoredType().GetName()) 08
Dim q As IQuery = oc.Query() 09
q.Constrain(GetType(Pilot)) 10
Dim result As IObjectSet = q.Execute() 11
Dim i As Integer 12
For i = 0 To result.Size() - 1 Step i + 1 13
Dim pilot As Pilot = CType(result(i), Pilot) 14
pilot.Name = New Identity(sfOld.Get(pilot).ToString(), "") 15
System.Console.WriteLine("Pilot=" + pilot.ToString()) 16
oc.Set(pilot) 17
Next 18
Finally 19
oc.Close() 20
End Try 21
End Sub
These are the basic refactoring types, which can help with any changes you will need to make.