Business Logic Framework - Tutorial 12:
Our twelfth tutorial shows you how to serialize a business object into a Xml file (in web samples the xml is saved in the view state instead of saving it in a file). The Xml Serialization has one limitation that the join and field collections etc are lost. However, on Deserialization of the business object automatically reconstructs the field collections but Join collections are not reconstructed. If you serialized an object that had created joins with other related objects, then right after deserialization you should call the ReInitializeCollections (Starting v3.x you no longer need to re initialize field and primary key collections, so you can pass false for the first two parameters and pass true for the last one).
The following code demonstrate the ability of the business logic framework classes to support xml serialization and deserialization.
| private void Button_LoadCustomer_Click(object sender, EventArgs e) | |
| { | |
| 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()) | |
| { | |
| // 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(); | |
| MessageBox.Show("Customer not found"); | |
| } | |
| } | |
| } | |
| else | |
| { | |
| this.ResetTextBoxes(); | |
| } | |
| } | |
| private void Button_Serialize_Click(object sender, EventArgs e) | |
| { | |
| 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()) | |
| { | |
| try | |
| { | |
| //create a serializer to persist the orignal Customer object | |
| XmlSerializer xmlserialer = new XmlSerializer(customer.GetType()); | |
| //TextWriter is used to write the xml string representation of customer object. | |
| TextWriter textWriter = new StringWriter(); | |
| //Serialize the object and write the string on writer object. So that string | |
| xmlserialer.Serialize(textWriter, customer); | |
| XmlDocument xdoc = new XmlDocument(); | |
| // Load the XmlDocument with the serialized xml. | |
| xdoc.LoadXml(textWriter.ToString()); | |
| // Save the xml document to a file called customer.xml. | |
| xdoc.Save(Environment.CurrentDirectory + "\\customer.xml"); | |
| this.ResetTextBoxes(); | |
| MessageBox.Show("Customer Serialized & Persisted in an Xml File."); | |
| } | |
| catch (Exception ex) | |
| { | |
| MessageBox.Show(ex.Message); | |
| } | |
| } | |
| else | |
| { | |
| MessageBox.Show(customer.ErrorString); | |
| } | |
| } | |
| } | |
| private void Button_Deserialize_Click(object sender, EventArgs e) | |
| { | |
| XmlDocument xdoc = new XmlDocument(); | |
| try | |
| { | |
| // Load the XmlDocument from the file. | |
| xdoc.Load(Environment.CurrentDirectory + "\\customer.xml"); | |
| XmlSerializer xmlserialer = new XmlSerializer(typeof(Customers)); | |
| TextWriter tw = new StringWriter(); | |
| StringReader streader = new StringReader(xdoc.OuterXml); | |
| // Deserialize the xml data back into an instance of Customer class. | |
| Customers customer = xmlserialer.Deserialize(streader) as Customers; | |
| if (customer != null) | |
| { | |
| this.TextBox_CustomerID.Text = customer.CustomerID.Value.ToString(); | |
| this.TextBox_FirstName.Text = customer.FirstName.Value; | |
| this.TextBox_LastName.Text = customer.LastName.Value; | |
| MessageBox.Show("Customer Deserialized successfully."); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| MessageBox.Show(ex.Message); | |
| } | |
| } | |
| Private Sub Button_LoadCustomer_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_LoadCustomer.Click | |
| 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 | |
| ' 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() | |
| MessageBox.Show("Customer not found") | |
| End If | |
| End Using | |
| Else | |
| Me.ResetTextBoxes() | |
| End If | |
| End Sub | |
| Private Sub Button_Serialize_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Serialize.Click | |
| 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 | |
| Try | |
| 'create a serializer to persist the orignal Customer object | |
| Dim xmlserialer As XmlSerializer = New XmlSerializer(customer.GetType()) | |
| 'TextWriter is used to write the xml string representation of customer object. | |
| Dim textWriter As TextWriter = New StringWriter() | |
| 'Serialize the object and write the string on writer object. So that string | |
| xmlserialer.Serialize(textWriter, customer) | |
| Dim xdoc As XmlDocument = New XmlDocument() | |
| ' Load the XmlDocument with the serialized xml. | |
| xdoc.LoadXml(textWriter.ToString()) | |
| ' Save the xml document to a file called customer.xml. | |
| xdoc.Save(Environment.CurrentDirectory + "\\customer.xml") | |
| Me.ResetTextBoxes() | |
| MessageBox.Show("Customer Serialized & Persisted in an Xml File.") | |
| Catch ex As Exception | |
| MessageBox.Show(ex.Message) | |
| End Try | |
| Else | |
| MessageBox.Show(customer.ErrorString) | |
| End If | |
| End Using | |
| End Sub | |
| Private Sub Button_Deserialize_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Deserialize.Click | |
| Dim xdoc As XmlDocument = New XmlDocument() | |
| Try | |
| ' Load the XmlDocument from the file. | |
| xdoc.Load(Environment.CurrentDirectory + "\\customer.xml") | |
| Dim xmlserialer As XmlSerializer = New XmlSerializer(GetType(Customers)) | |
| Dim tw As TextWriter = New StringWriter() | |
| Dim streader As StringReader = New StringReader(xdoc.OuterXml) | |
| ' Deserialize the xml data back into an instance of Customer class. | |
| Dim customer As Customers = xmlserialer.Deserialize(streader) | |
| If Not customer Is Nothing Then | |
| Me.TextBox_CustomerID.Text = customer.CustomerID.Value.ToString() | |
| Me.TextBox_FirstName.Text = customer.FirstName.Value | |
| Me.TextBox_LastName.Text = customer.LastName.Value | |
| MessageBox.Show("Customer Deserialized successfully.") | |
| End If | |
| Catch ex As Exception | |
| MessageBox.Show(ex.Message) | |
| End Try | |
| End Sub | |
