Business Logic Framework - Tutorial 4:
Our fourth tutorial shows you one of the very essential features that loads an object data from the database and persists any changes back to the database. The user enters a value for CustomerID (which is the primary key for the Customers table) and the Load method is called which loads all the values from the Customer record into each property. This sample also shows another small but very feature i.e. The ability to directly parse any field's value from a string value. This is consistent throughout the framework and the same capability is provided by each field type object including the XmlDocumentField.
Here is the example code that loads the Customer object's values from the database:
| if (this.TextBox_CustomerID.Text.Trim().Length > 0) | |
| { | |
| using (Customers customer = new Customers()) | |
| { | |
| // the following line displays a very useful method on each field type object | |
| // Parse method takes any string value and converts it to the correct type | |
| // and assigns the converted value into the Value property. | |
| customer.CustomerID.Parse(this.TextBox_CustomerID.Text); | |
| // Load method expects that the primary key field(s) of the business object | |
| // have already been assigned values. Once the value is assigned | |
| // to the primary key fields, calling the Load method will load all field values | |
| // into the business object. | |
| if (customer.Load()) | |
| { | |
| this.TextBox_CustomerID.Enabled = false; | |
| this.Button_Save.Enabled = true; | |
| // Since we have already called the Load method and it was successful | |
| // we can now access the values of all the fields. | |
| this.TextBox_FirstName.Text = customer.FirstName.Value; | |
| this.TextBox_LastName.Text = customer.LastName.Value; | |
| } | |
| else | |
| { | |
| this.ResetTextBoxes(); | |
| } | |
| } | |
| } | |
| else | |
| { | |
| this.ResetTextBoxes(); | |
| } | |
| If Me.TextBox_CustomerID.Text.Trim().Length > 0 Then | |
| Using customer As Customers = New Customers() | |
| ' the following line displays a very useful method on each field type object | |
| ' Parse method takes any string value and converts it to the correct type | |
| ' and assigns the converted value into the Value property. | |
| customer.CustomerID.Parse(Me.TextBox_CustomerID.Text) | |
| ' Load method expects that the primary key field(s) of the business object | |
| ' have already been assigned values. Once the value is assigned | |
| ' to the primary key fields, calling the Load method will load all field values | |
| ' into the business object. | |
| If customer.Load() Then | |
| Me.TextBox_CustomerID.Enabled = False | |
| Me.Button_Save.Enabled = True | |
| ' Since we have already called the Load method and it was successful | |
| ' we can now access the values of all the fields. | |
| Me.TextBox_FirstName.Text = customer.FirstName.Value | |
| Me.TextBox_LastName.Text = customer.LastName.Value | |
| Else | |
| Me.ResetTextBoxes() | |
| End If | |
| End Using | |
| Else | |
| Me.ResetTextBoxes() | |
| End If | |
This code above shows that just by writing a three lines of code we are able to get an object's all the values loaded into its properties. The Load method returns a Boolean value indicating weather the the values were loaded or not, but the Business Object also sets its IsLoaded property so you are able to refer to that property at a later stage as well. |
The following code shows how you can save changes to an object instance.
| if (!this.TextBox_CustomerID.Enabled) | |
| { | |
| using (Customers customer = new Customers()) | |
| { | |
| // Setting the ObjectMode property of an object allow the object to | |
| // know what it is supposed to do with the data you are supplying it with. | |
| // As shown in the example below, once the ObjectMode is set to Save | |
| // each value that you assign to this business object's (customer) properties | |
| // will be used to update the underlying data base record for this object. | |
| customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Save; | |
| // Assigning the value to the Primary Key field(s) is required for the object | |
| // to be able to update. | |
| customer.CustomerID.Parse(this.TextBox_CustomerID.Text); | |
| customer.FirstName.Value = this.TextBox_FirstName.Text; | |
| customer.LastName.Value = this.TextBox_LastName.Text; | |
| // Calling Update method is all you need after assigning the values that need to be updated. | |
| if (customer.Update()) | |
| { | |
| MessageBox.Show("Customer Saved!"); | |
| this.ResetTextBoxes(); | |
| } | |
| else | |
| { | |
| // ErrorString property is set anytime an operation has an exception. | |
| MessageBox.Show(customer.ErrorString); | |
| } | |
| } | |
| } |
| If Not Me.TextBox_CustomerID.Enabled Then | |
| Using customer = New Customers() | |
| ' Setting the ObjectMode property of an object allow the object to | |
| ' know what it is supposed to do with the data you are supplying it with. | |
| ' As shown in the example below, once the ObjectMode is set to Save | |
| ' each value that you assign to this business object's (customer) properties | |
| ' will be used to update the underlying data base record for this object. | |
| customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Save | |
| ' Assigning the value to the Primary Key field(s) is required for the object | |
| ' to be able to update. | |
| customer.CustomerID.Parse(Me.TextBox_CustomerID.Text) | |
| customer.FirstName.Value = Me.TextBox_FirstName.Text | |
| customer.LastName.Value = Me.TextBox_LastName.Text | |
| ' Calling Update method is all you need after assigning the values that need to be updated. | |
| If customer.Update() Then | |
| MessageBox.Show("Customer Saved!") | |
| Me.ResetTextBoxes() | |
| Else | |
| ' ErrorString property is set anytime an operation has an exception. | |
| MessageBox.Show(customer.ErrorString) | |
| End If | |
| End Using | |
| End If | |

This code above shows that just by writing a three lines of code we are able to get an object's all the values loaded into its properties. The Load method returns a Boolean value indicating weather the the values were loaded or not, but the Business Object also sets its IsLoaded property so you are able to refer to that property at a later stage as well.
Click to enlarge