Collection Example

This example will show how to use TA for classes having collections as a field. TA project provides a simple TA collection implementation in a PagedList class. This collection is a very basic implementation and most probably will be replaced with a more mature collection in the final release. However, it can give you an idea on how to work with a collection.

We will use a Team class with a collection of Pilot objects:

 

Team.cs
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02using System.Collections; 03 04using Db4objects.Db4o; 05using Db4objects.Db4o.Activation; 06using Db4objects.Db4o.TA; 07using Db4objects.Db4o.TA.Tests.Collections; 08 09namespace Db4ojects.Db4odoc.TAExamples 10{ 11 public class Team : IActivatable 12 { 13 14 IList _pilots = new PagedList(); 15 16 string _name; 17 18 //TA Activator 19 [System.NonSerialized] 20 IActivator _activator; 21 22 // Bind the class to an object container 23 public void Bind(IActivator activator) 24 { 25 if (null != _activator) 26 { 27 throw new System.InvalidOperationException(); 28 } 29 _activator = activator; 30 } 31 32 // activate object fields 33 protected void Activate() 34 { 35 if (_activator == null) return; 36 _activator.Activate(); 37 } 38 39 public void AddPilot(Pilot pilot) 40 { 41 _pilots.Add(pilot); 42 } 43 44 public void ListAllPilots() 45 { 46 // activate before printing the collection members 47 Activate(); 48 49 for (IEnumerator iter = _pilots.GetEnumerator(); iter.MoveNext(); ) 50 { 51 Pilot pilot = (Pilot)iter.Current; 52 System.Console.WriteLine(pilot); 53 } 54 } 55 } 56}
Pilot.cs
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02using Db4objects.Db4o; 03using Db4objects.Db4o.Activation; 04using Db4objects.Db4o.TA; 05 06namespace Db4ojects.Db4odoc.TAExamples 07{ 08 public class Pilot : IActivatable 09 { 10 11 private const string Extention = ".jpg"; 12 13 private string _name; 14 15 private Image _image; 16 17 [System.NonSerialized] 18 IActivator _activator; 19 20 public Pilot(string name) 21 { 22 _name = name; 23 _image = new Image(name + Extention); 24 } 25 26 // Bind the class to an object container 27 public void Bind(IActivator activator) 28 { 29 if (null != _activator) 30 { 31 throw new System.InvalidOperationException(); 32 } 33 _activator = activator; 34 } 35 36 // activate the object fields 37 protected void Activate() 38 { 39 if (_activator == null) 40 return; 41 _activator.Activate(); 42 } 43 44 public string Name 45 { 46 get 47 { 48 // even simple string needs to be activated 49 Activate(); 50 return _name; 51 } 52 } 53 54 public override string ToString() 55 { 56 // use Name property, which already contains activation call 57 return Name; 58 } 59 } 60 61}

 

Team.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02Imports System.Collections 03Imports Db4objects.Db4o 04Imports Db4objects.Db4o.Activation 05Imports Db4objects.Db4o.TA 06Imports Db4objects.Db4o.TA.Tests.Collections 07 08Namespace Db4ojects.Db4odoc.TAExamples 09 10 Public Class Team 11 Implements IActivatable 12 13 Private _pilots As IList = New PagedList 14 Private _name As String 15 16 ' TA Activator 17 <Transient()> _ 18 Private _activator As IActivator 19 20 ' Bind the class to an object container 21 Public Sub Bind(ByVal activator As IActivator) Implements IActivatable.Bind 22 If Not Nothing Is _activator Then 23 Throw New System.InvalidOperationException() 24 End If 25 _activator = activator 26 End Sub 27 28 ' activate object fields 29 Protected Sub Activate() 30 If _activator Is Nothing Then 31 Return 32 End If 33 _activator.Activate() 34 End Sub 35 36 Public Sub AddPilot(ByVal pilot As Pilot) 37 _pilots.Add(pilot) 38 End Sub 39 40 Public Sub ListAllPilots() 41 ' activate before printing the collection members 42 Activate() 43 Dim iter As IEnumerator = _pilots.GetEnumerator 44 While iter.MoveNext 45 Dim pilot As Pilot = CType(iter.Current, Pilot) 46 System.Console.WriteLine(pilot) 47 End While 48 End Sub 49 50 End Class 51End Namespace
Pilot.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02Imports Db4objects.Db4o 03Imports Db4objects.Db4o.Activation 04Imports Db4objects.Db4o.TA 05 06Namespace Db4ojects.Db4odoc.TAExamples 07 08 Public Class Pilot 09 Implements IActivatable 10 Private Const Extention As String = ".jpg" 11 Private _name As String 12 Private _image As Image 13 <Transient()> Private _activator As Db4objects.Db4o.Activation.IActivator 14 15 Public Sub New(ByVal name As String) 16 _name = name 17 _image = New Image(name + Extention) 18 End Sub 19 20 ' Bind the class to an object container 21 Public Sub Bind(ByVal activator As Activation.IActivator) Implements IActivatable.Bind 22 If Not Nothing Is _activator Then 23 Throw New System.InvalidOperationException() 24 End If 25 _activator = activator 26 End Sub 27 28 ' activate the object fields 29 Protected Sub Activate() 30 If _activator Is Nothing Then 31 Return 32 End If 33 _activator.Activate() 34 End Sub 35 36 Public ReadOnly Property Name() As String 37 Get 38 ' even simple string needs to be activated 39 Activate() 40 Return _name 41 End Get 42 End Property 43 44 Public Overloads Overrides Function ToString() As String 45 ' use Name property, which already contains activation call 46 Return Name 47 End Function 48 49 End Class 50End Namespace

Store and retrieve using the same configuration as in the previous exampleupdated.

 

TAExample.cs: StoreCollection
01private static void StoreCollection() 02 { 03 File.Delete(Db4oFileName); 04 IObjectContainer container = Database(ConfigureTA()); 05 if (container != null) 06 { 07 try 08 { 09 Team team = new Team(); 10 for (int i = 0; i < 10; i++) 11 { 12 team.AddPilot(new Pilot("Pilot #" + i)); 13 } 14 container.Set(team); 15 container.Commit(); 16 } 17 catch (Exception ex) 18 { 19 System.Console.WriteLine(ex.StackTrace); 20 } 21 finally 22 { 23 CloseDatabase(); 24 } 25 } 26 }

TAExample.cs: TestCollectionActivation
01private static void TestCollectionActivation() 02 { 03 StoreCollection(); 04 IObjectContainer container = Database(ConfigureTA()); 05 if (container != null) 06 { 07 try 08 { 09 Team team = (Team)container.Get(new Team()).Next(); 10 // this method will activate all the members in the collection 11 team.ListAllPilots(); 12 } 13 catch (Exception ex) 14 { 15 System.Console.WriteLine(ex.StackTrace); 16 } 17 finally 18 { 19 CloseDatabase(); 20 } 21 } 22 }


 

TAExample.vb: StoreCollection
01Private Shared Sub StoreCollection() 02 File.Delete(Db4oFileName) 03 Dim container As IObjectContainer = Database(ConfigureTA) 04 If Not (container Is Nothing) Then 05 Try 06 Dim team As Team = New Team 07 Dim i As Integer = 0 08 While i < 10 09 team.AddPilot(New Pilot("Pilot #" + i.ToString())) 10 i = i + 1 11 End While 12 container.Set(team) 13 container.Commit() 14 Catch ex As Exception 15 System.Console.WriteLine(ex.StackTrace) 16 Finally 17 CloseDatabase() 18 End Try 19 End If 20 End Sub

TAExample.vb: TestCollectionActivation
01Private Shared Sub TestCollectionActivation() 02 StoreCollection() 03 Dim container As IObjectContainer = Database(ConfigureTA) 04 If Not (container Is Nothing) Then 05 Try 06 Dim team As Team = CType(container.Get(New Team).Next, Team) 07 ' this method will activate all the members in the collection 08 team.ListAllPilots() 09 Catch ex As Exception 10 System.Console.WriteLine(ex.StackTrace) 11 Finally 12 CloseDatabase() 13 End Try 14 End If 15 End Sub