Data Binding

This topic applies to .NET version only

One common question we get from our users is:

Can I still take advantage of data aware control mechanisms with my objects?

The answer is quite simply yes. .NET data binding also works with plain objects.

This is also to say that using db4o is completely orthogonal to the use of data binding.

The usual pattern would be something like the following:

Let's take a very simple example that illustrates the points above.

Our business class:

Customer.cs
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02namespace Db4objects.Db4odoc.Binding 03{ 04 /// <summary> 05 /// A simple business class. 06 /// </summary> 07 public class Customer 08 { 09 string _name; 10 string _phoneNumber; 11 12 public Customer(string name, string phoneNumber) 13 { 14 _name = name; 15 _phoneNumber = phoneNumber; 16 } 17 18 public string Name 19 { 20 get 21 { 22 return _name; 23 } 24 25 set 26 { 27 _name = value; 28 } 29 } 30 31 public string PhoneNumber 32 { 33 get 34 { 35 return _phoneNumber; 36 } 37 38 set 39 { 40 _phoneNumber = value; 41 } 42 } 43 } 44}

Customer.vb
01' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 02 03Namespace Db4objects.Db4odoc.Binding 04 Public Class Customer 05 Dim _name As String 06 Dim _phoneNumber As String 07 08 Public Sub New(ByVal name As String, ByVal phoneNumber As String) 09 _name = name 10 _phoneNumber = phoneNumber 11 End Sub 12 13 Public Property Name() As String 14 Get 15 Return _name 16 End Get 17 Set(ByVal Value As String) 18 _name = Value 19 End Set 20 End Property 21 22 Public Property PhoneNumber() As String 23 Get 24 Return _phoneNumber 25 End Get 26 Set(ByVal Value As String) 27 _phoneNumber = Value 28 End Set 29 End Property 30 End Class 31End Namespace

The Form class:

CustomerForm.cs
001using System; 002using System.IO; 003using System.Drawing; 004using System.Collections; 005using System.ComponentModel; 006using System.Windows.Forms; 007using Db4objects.Db4o; 008 009namespace Db4objects.Db4odoc.Binding 010{ 011 /// <summary> 012 /// Summary description for CustomerForm. 013 /// </summary> 014 public class CustomerForm : System.Windows.Forms.Form 015 { 016 internal System.Windows.Forms.TextBox _txtPhoneNumber; 017 internal System.Windows.Forms.ComboBox _cmbCustomers; 018 019 /// <summary> 020 /// Required designer variable. 021 /// </summary> 022 private System.ComponentModel.Container components = null; 023 024 internal System.Windows.Forms.Label _labelPhone; 025 internal System.Windows.Forms.Label _labelName; 026 027 private IObjectContainer _container = null; 028 029 private ArrayList _customers = new ArrayList(); 030 031 public CustomerForm() 032 { 033 // 034 // Required for Windows Form Designer support 035 // 036 InitializeComponent(); 037 038 OpenDataFile(); 039 SetUpDataBindings(); 040 } 041 042 void OpenDataFile() 043 { 044 string dataFile = Path.Combine(Application.UserAppDataPath, "data.yap"); 045 _container = Db4oFactory.OpenFile(dataFile); 046 } 047 048 void SetUpDataBindings() 049 { 050 ObjectSet os = _container.Get(typeof(Customer)); 051 if (0 == os.Size()) 052 { 053 // Generate some initial data 054 _customers.Add(new Customer("John Cleese", "55-98763333")); 055 _customers.Add(new Customer("Herman Hesse","32-33335555")); 056 _customers.Add(new Customer("Douglas Adams", "42-42424242")); 057 } 058 else 059 { 060 while (os.HasNext()) 061 { 062 _customers.Add(os.Next()); 063 } 064 } 065 _cmbCustomers.DisplayMember = "Name"; 066 _cmbCustomers.DataSource = _customers; 067 _txtPhoneNumber.DataBindings.Add("Text", _customers, "PhoneNumber"); 068 } 069 070 void PersistChanges() 071 { 072 foreach (Customer customer in _customers) 073 { 074 _container.Set(customer); 075 } 076 } 077 078 protected override void OnClosing(CancelEventArgs e) 079 { 080 PersistChanges(); 081 _container.Close(); 082 base.OnClosing (e); 083 } 084 085 /// <summary> 086 /// Clean up any resources being used. 087 /// </summary> 088 protected override void Dispose( bool disposing ) 089 { 090 if( disposing ) 091 { 092 if (components != null) 093 { 094 components.Dispose(); 095 } 096 } 097 base.Dispose( disposing ); 098 } 099 100 Windows Form Designer generated code 162 } 163}

CustomerForm.vb
001Imports System 002Imports System.IO 003Imports Db4objects.Db4o 004 005' Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com 006Namespace Db4objects.Db4odoc.Binding 007 008 Public Class CustomerForm 009 Inherits System.Windows.Forms.Form 010 Private _container As IObjectContainer = Nothing 011 Private _customers As ArrayList = New ArrayList() 012 013 Public Sub New() 014 InitializeComponent() 015 OpenDataFile() 016 SetUpDataBindings() 017 End Sub 018 019 Private Sub OpenDataFile() 020 Dim dataFile As String = Path.Combine(Application.UserAppDataPath, "data.yap") 021 _container = Db4oFactory.OpenFile(dataFile) 022 End Sub 023 024 Private Sub SetUpDataBindings() 025 Dim os As ObjectSet = _container.Get(GetType(Customer)) 026 If 0 = os.Size() Then 027 ' Generate some initial data 028 _customers.Add(New Customer("John Cleese", "55-98763333")) 029 _customers.Add(New Customer("Herman Hesse", "32-33335555")) 030 _customers.Add(New Customer("Douglas Adams", "42-42424242")) 031 Else 032 While os.HasNext() 033 _customers.Add(os.Next()) 034 End While 035 End If 036 _cmbCustomers.DisplayMember = "Name" 037 _cmbCustomers.DataSource = _customers 038 _txtPhoneNumber.DataBindings.Add("Text", _customers, "PhoneNumber") 039 End Sub 040 041 Private Sub PersistChanges() 042 Dim customer As Customer 043 For Each customer In _customers 044 _container.Set(customer) 045 Next 046 End Sub 047 048 Friend WithEvents _labelName As System.Windows.Forms.Label 049 Friend WithEvents _labelPhone As System.Windows.Forms.Label 050 Friend WithEvents _cmbCustomers As System.Windows.Forms.ComboBox 051 Friend WithEvents _txtPhoneNumber As System.Windows.Forms.TextBox 052 053 Private Sub InitializeComponent() 054 Me._labelName = New System.Windows.Forms.Label 055 Me._labelPhone = New System.Windows.Forms.Label 056 Me._cmbCustomers = New System.Windows.Forms.ComboBox 057 Me._txtPhoneNumber = New System.Windows.Forms.TextBox 058 Me.SuspendLayout() 059 ' 060 '_labelName 061 ' 062 Me._labelName.AutoSize = True 063 Me._labelName.Location = New System.Drawing.Point(12, 9) 064 Me._labelName.Name = "_labelName" 065 Me._labelName.Size = New System.Drawing.Size(51, 13) 066 Me._labelName.TabIndex = 0 067 Me._labelName.Text = "Customer" 068 ' 069 '_labelPhone 070 ' 071 Me._labelPhone.AutoSize = True 072 Me._labelPhone.Location = New System.Drawing.Point(12, 40) 073 Me._labelPhone.Name = "_labelPhone" 074 Me._labelPhone.Size = New System.Drawing.Size(45, 13) 075 Me._labelPhone.TabIndex = 1 076 Me._labelPhone.Text = "Phone#" 077 ' 078 '_cmbCustomers 079 ' 080 Me._cmbCustomers.FormattingEnabled = True 081 Me._cmbCustomers.Location = New System.Drawing.Point(103, 6) 082 Me._cmbCustomers.Name = "_cmbCustomers" 083 Me._cmbCustomers.Size = New System.Drawing.Size(177, 21) 084 Me._cmbCustomers.TabIndex = 2 085 ' 086 '_txtPhoneNumber 087 ' 088 Me._txtPhoneNumber.Location = New System.Drawing.Point(103, 37) 089 Me._txtPhoneNumber.Name = "_txtPhoneNumber" 090 Me._txtPhoneNumber.Size = New System.Drawing.Size(177, 20) 091 Me._txtPhoneNumber.TabIndex = 3 092 ' 093 'CustomerForm 094 ' 095 Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) 096 Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font 097 Me.ClientSize = New System.Drawing.Size(292, 66) 098 Me.Controls.Add(Me._txtPhoneNumber) 099 Me.Controls.Add(Me._cmbCustomers) 100 Me.Controls.Add(Me._labelPhone) 101 Me.Controls.Add(Me._labelName) 102 Me.Name = "CustomerForm" 103 Me.Text = "Db4o Data Binding" 104 Me.ResumeLayout(False) 105 Me.PerformLayout() 106 107 End Sub 108 109 Private Sub CustomerForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing 110 PersistChanges() 111 _container.Close() 112 MyBase.OnClosing(e) 113 End Sub 114 End Class 115End Namespace