Quick Objects Documentation Akal Tech Logo
Serialization and Deserialization of Business Logic Objects.

Glossary Item Box

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.

1 private void Button_LoadCustomer_Click(object sender, EventArgs e)  
2 {  
3     if (this.TextBox_CustomerID.Text.Trim().Length > 0)  
4     {  
5         using (Customers customer = new Customers())  
6         {  
7             // the following line displays a very useful method on each field type object  
8             // Parse method takes any string value and converts it to the correct type  
9             // and assigns the converted value into the Value property.  
10             customer.CustomerID.Parse(this.TextBox_CustomerID.Text);  
11  
12             // Load method expects that the primary key field(s) of the business object  
13             // have already been assigned values. Once the value is assigned   
14             // to the primary key fields, calling the Load method will load all field values  
15             // into the business object.  
16             if (customer.Load())  
17             {  
18                 // Since we have already called the Load method and it was successful  
19                 // we can now access the values of all the fields.  
20                 this.TextBox_FirstName.Text = customer.FirstName.Value;  
21                 this.TextBox_LastName.Text = customer.LastName.Value;  
22             }  
23             else 
24             {  
25                 this.ResetTextBoxes();  
26                 MessageBox.Show("Customer not found");  
27             }  
28         }  
29     }  
30     else 
31     {  
32         this.ResetTextBoxes();  
33     }  
34 }  
35  
36 private void Button_Serialize_Click(object sender, EventArgs e)  
37 {  
38     using (Customers customer = new Customers())  
39     {  
40         // the following line displays a very useful method on each field type object  
41         // Parse method takes any string value and converts it to the correct type  
42         // and assigns the converted value into the Value property.  
43         customer.CustomerID.Parse(this.TextBox_CustomerID.Text);  
44  
45         // Load method expects that the primary key field(s) of the business object  
46         // have already been assigned values. Once the value is assigned   
47         // to the primary key fields, calling the Load method will load all field values  
48         // into the business object.  
49         if (customer.Load())  
50         {  
51             try 
52             {  
53                 //create a serializer to persist the orignal Customer object  
54                 XmlSerializer xmlserialer = new XmlSerializer(customer.GetType());  
55                 //TextWriter is used to write the xml string representation of customer object.  
56                 TextWriter textWriter = new StringWriter();  
57                 //Serialize the object and write the string on writer object. So that string   
58                 xmlserialer.Serialize(textWriter, customer);  
59                 XmlDocument xdoc = new XmlDocument();  
60                 // Load the XmlDocument with the serialized xml.  
61                 xdoc.LoadXml(textWriter.ToString());  
62                 // Save the xml document to a file called customer.xml.  
63                 xdoc.Save(Environment.CurrentDirectory + "\\customer.xml");  
64  
65                 this.ResetTextBoxes();  
66                 MessageBox.Show("Customer Serialized & Persisted in an Xml File.");  
67             }  
68             catch (Exception ex)  
69             {  
70                 MessageBox.Show(ex.Message);  
71             }  
72         }  
73         else 
74         {  
75             MessageBox.Show(customer.ErrorString);  
76         }  
77     }  
78 }  
79  
80 private void Button_Deserialize_Click(object sender, EventArgs e)  
81 {  
82     XmlDocument xdoc = new XmlDocument();  
83     try 
84     {  
85         // Load the XmlDocument from the file.  
86         xdoc.Load(Environment.CurrentDirectory + "\\customer.xml");  
87         XmlSerializer xmlserialer = new XmlSerializer(typeof(Customers));  
88         TextWriter tw = new StringWriter();  
89         StringReader streader = new StringReader(xdoc.OuterXml);  
90         // Deserialize the xml data back into an instance of Customer class.  
91         Customers customer = xmlserialer.Deserialize(streader) as Customers;  
92         if (customer != null)  
93         {  
94             this.TextBox_CustomerID.Text = customer.CustomerID.Value.ToString();  
95             this.TextBox_FirstName.Text = customer.FirstName.Value;  
96             this.TextBox_LastName.Text = customer.LastName.Value;  
97             MessageBox.Show("Customer Deserialized successfully.");  
98         }  
99     }  
100     catch (Exception ex)  
101     {  
102         MessageBox.Show(ex.Message);  
103     }  
104 }  
105  
1 Private Sub Button_LoadCustomer_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button_LoadCustomer.Click  
2     If Me.TextBox_CustomerID.Text.Trim().Length > 0 Then 
3         Using customer As Customers = New Customers()  
4             ' the following line displays a very useful method on each field type object  
5             ' Parse method takes any string value and converts it to the correct type  
6             ' and assigns the converted value into the Value property.  
7             customer.CustomerID.Parse(Me.TextBox_CustomerID.Text)  
8  
9             ' Load method expects that the primary key field(s) of the business object  
10             ' have already been assigned values. Once the value is assigned   
11             ' to the primary key fields, calling the Load method will load all field values  
12             ' into the business object.  
13             If customer.Load() Then 
14                 ' Since we have already called the Load method and it was successful  
15                 ' we can now access the values of all the fields.  
16                 Me.TextBox_FirstName.Text = customer.FirstName.Value  
17                 Me.TextBox_LastName.Text = customer.LastName.Value  
18             Else 
19                 Me.ResetTextBoxes()  
20                 MessageBox.Show("Customer not found")  
21             End If 
22         End Using  
23     Else 
24         Me.ResetTextBoxes()  
25     End If 
26 End Sub 
27  
28 Private Sub Button_Serialize_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button_Serialize.Click  
29     Using customer As Customers = New Customers()  
30         ' the following line displays a very useful method on each field type object  
31         ' Parse method takes any string value and converts it to the correct type  
32         ' and assigns the converted value into the Value property.  
33         customer.CustomerID.Parse(Me.TextBox_CustomerID.Text)  
34  
35         ' Load method expects that the primary key field(s) of the business object  
36         ' have already been assigned values. Once the value is assigned   
37         ' to the primary key fields, calling the Load method will load all field values  
38         ' into the business object.  
39         If customer.Load() Then 
40             Try 
41                 'create a serializer to persist the orignal Customer object  
42                 Dim xmlserialer As XmlSerializer = New XmlSerializer(customer.GetType())  
43                 'TextWriter is used to write the xml string representation of customer object.  
44                 Dim textWriter As TextWriter = New StringWriter()  
45                 'Serialize the object and write the string on writer object. So that string   
46                 xmlserialer.Serialize(textWriter, customer)  
47                 Dim xdoc As XmlDocument = New XmlDocument()  
48                 ' Load the XmlDocument with the serialized xml.  
49                 xdoc.LoadXml(textWriter.ToString())  
50                 ' Save the xml document to a file called customer.xml.  
51                 xdoc.Save(Environment.CurrentDirectory + "\\customer.xml")  
52  
53                 Me.ResetTextBoxes()  
54                 MessageBox.Show("Customer Serialized & Persisted in an Xml File.")  
55             Catch ex As Exception  
56                 MessageBox.Show(ex.Message)  
57             End Try 
58         Else 
59             MessageBox.Show(customer.ErrorString)  
60         End If 
61     End Using  
62 End Sub 
63  
64 Private Sub Button_Deserialize_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button_Deserialize.Click  
65     Dim xdoc As XmlDocument = New XmlDocument()  
66     Try 
67         ' Load the XmlDocument from the file.  
68         xdoc.Load(Environment.CurrentDirectory + "\\customer.xml")  
69         Dim xmlserialer As XmlSerializer = New XmlSerializer(GetType(Customers))  
70         Dim tw As TextWriter = New StringWriter()  
71         Dim streader As StringReader = New StringReader(xdoc.OuterXml)  
72         ' Deserialize the xml data back into an instance of Customer class.  
73         Dim customer As Customers = xmlserialer.Deserialize(streader)  
74         If Not customer Is Nothing Then 
75             Me.TextBox_CustomerID.Text = customer.CustomerID.Value.ToString()  
76             Me.TextBox_FirstName.Text = customer.FirstName.Value  
77             Me.TextBox_LastName.Text = customer.LastName.Value  
78             MessageBox.Show("Customer Deserialized successfully.")  
79         End If 
80     Catch ex As Exception  
81         MessageBox.Show(ex.Message)  
82     End Try 
83 End Sub 
84