Working With Structured Objects

In real world objects are referenced by each other creating deep reference structures.

This chapter will give you an overview of how db4o deals with structured objects.

For an example we will use a simple model, where Pilot class is referenced from Car class.

Pilot.cs
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02namespace Db4objects.Db4odoc.Structured 03{ 04 public class Pilot 05 { 06 string _name; 07 int _points; 08 09 public Pilot(string name, int points) 10 { 11 _name = name; 12 _points = points; 13 } 14 15 public int Points 16 { 17 get 18 { 19 return _points; 20 } 21 } 22 23 public void AddPoints(int points) 24 { 25 _points += points; 26 } 27 28 public string Name 29 { 30 get 31 { 32 return _name; 33 } 34 } 35 36 override public string ToString() 37 { 38 return string.Format("{0}/{1}", _name, _points); 39 } 40 } 41}
Car.cs
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02namespace Db4objects.Db4odoc.Structured 03{ 04 public class Car 05 { 06 string _model; 07 Pilot _pilot; 08 09 public Car(string model) 10 { 11 _model = model; 12 _pilot = null; 13 } 14 15 public Pilot Pilot 16 { 17 get 18 { 19 return _pilot; 20 } 21 22 set 23 { 24 _pilot = value; 25 } 26 } 27 28 public string Model 29 { 30 get 31 { 32 return _model; 33 } 34 } 35 36 override public string ToString() 37 { 38 return string.Format("{0}[{1}]", _model, _pilot); 39 } 40 } 41}

Pilot.vb
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02Namespace Db4objects.Db4odoc.Structured 03 Public Class Pilot 04 Private _name As String 05 06 Private _points As Integer 07 08 Public Sub New(ByVal name As String, ByVal points As Integer) 09 _name = name 10 _points = points 11 End Sub 12 13 Public ReadOnly Property Points() As Integer 14 Get 15 Return _points 16 End Get 17 End Property 18 19 Public Sub AddPoints(ByVal points As Integer) 20 _points += points 21 End Sub 22 23 Public ReadOnly Property Name() As String 24 Get 25 Return _name 26 End Get 27 End Property 28 29 Public Overloads Overrides Function ToString() As String 30 Return String.Format("{0}/{1}", _name, _points) 31 End Function 32 33 End Class 34End Namespace
Car.vb
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02Namespace Db4objects.Db4odoc.Structured 03 Public Class Car 04 Private _model As String 05 06 Private _pilot As Pilot 07 08 Public Sub New(ByVal model As String) 09 _model = model 10 _pilot = Nothing 11 End Sub 12 13 Public Property Pilot() As Pilot 14 Get 15 Return _pilot 16 End Get 17 Set(ByVal value As Pilot) 18 _pilot = value 19 End Set 20 End Property 21 22 Public ReadOnly Property Model() As String 23 Get 24 Return _model 25 End Get 26 End Property 27 28 Public Overloads Overrides Function ToString() As String 29 Return String.Format("{0}[{1}]", _model, _pilot) 30 End Function 31 32 End Class 33End Namespace