Listing 1 shows the database-related code in the Bouncer class. The DeleteAllBouncers routine simply removes all of the Bouncer objects from the Bouncers table. A database tool module provides the ExecuteNonQuery subroutine. As its name implies, this routine executes a non-query SQL statement. The database module doesn't do anything directly with serializations, so it's not covered here.
The SaveBouncer method uses an XmlSerializer object to create the Bouncer's serialization. It composes an SQL INSERT statement and executes it to insert the new object into the database.
Function FetchBouncers retrieves a collection of Bouncer objects from the database. It adds a condition passed as a parameter to a standard SQL SELECT statement to find the objects it needs. For instance, a condition phrase to select red objects would be the following:
WHERE Serialization LIKE '%<ColorName>red</ColorName>%'
Function FetchBouncers uses this condition to produce the query:
SELECT Serialization FROM Bouncers WHERE Serialization LIKE '%<ColorName>red</ColorName>%'
FetchBouncers uses the database routine ExecuteQuery to perform the query and fetch the Bouncer information. It deserializes the Bouncers, and adds them to a collection that it returns to the calling routine.
Listing 1. The Bouncer Class Uses this Code to Save and Restore Objects in the Database
' Delete all Bouncers from the database. Public Shared Sub DeleteAllBouncers() ExecuteNonQuery("DELETE FROM Bouncers") End Sub ' Save the Bouncer into the database. Public Sub SaveBouncer() Dim xml_serializer As XmlSerializer Dim string_writer As New StringWriter() Dim serialization As String ' Get the Bouncer's serialization. xml_serializer = New XmlSerializer(GetType(Bouncer)) xml_serializer.Serialize(string_writer, Me) serialization = string_writer.ToString ' Create the new Bouncers record. ExecuteNonQuery( _ "INSERT INTO Bouncers (Serialization) VALUES ('" & _ serialization & "')") End Sub ' Fetch the Bouncers that meet some condition. Public Shared Function FetchBouncers(ByVal condition As String) As Collection Dim bouncers As Collection Dim query As String Dim data_set As DataSet Dim new_bouncer As Bouncer Dim serialization As String Dim string_reader As StringReader Dim xml_serializer As New XmlSerializer(GetType(Bouncer)) Dim i As Integer ' Start a new collection. bouncers = New Collection() ' Get the data. query = "SELECT Serialization " & _ "FROM Bouncers " & condition data_set = ExecuteQuery(query) ' If we got nothing, do no more. If data_set.Tables(0).Rows.Count = 0 Then Return bouncers ' Deserialize the Bouncers. For i = 0 To data_set.Tables(0).Rows.Count - 1 ' Deserialize the Bouncer. serialization = data_set.Tables(0).Rows(i).Item(0) string_reader = New StringReader(serialization) new_bouncer = xml_serializer.Deserialize(string_reader) bouncers.Add(new_bouncer) Next i Return bouncers End Function
The other details of the BouncingObjects program are not particularly relevant to a discussion of serialization, so they are not described here. Download the program and see how it works: