Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of

Querying the Customer

When the mapping and connection files are in place, the client can write code against the persistent class using the XmlObjectSpace or SqlObjectSpace classes. For example, to query a single customer, you could write the following code:

Dim os As New SqlObjectSpace("connect.xml", "map.xml")
Dim myCustomer As Customer
Dim strName As String

myCustomer = CType(os.GetObject(GetType(Customer), "Id = 1"), Customer)
strName = myCustomer.Name

First, the SqlObjectSpace object is instantiated and passed the connection and mapping files shown previously. SqlObjectSpace is used because the data exists in a relational database. Next, the myCustomer object is populated using the GetObject method of SqlObjectSpace inherited from the IObjectSpace interface. You'll notice that the string "Id = 1" is passed as the second argument. This syntax is referred to as OPath, a derivative of XPath that Microsoft developed specifically for the ObjectSpaces framework. Behind the scenes, the ObjectSpaces framework uses the combination of the OPath query and the mapping and connect files to connect to the database and formulate a SELECT statement to retrieve the customer identified with the CustomerID of 1. The data is then retrieved from the database, loaded into a DataSet, and mapped into the myCustomer object, where the properties such as Name are then available.

The inclusion of OPath for querying is different from using EJB, in which the standard behavior is to expose a single findByPrimaryKey method, although different vendors can augment this by allowing other findBy methods to be implemented and mapped to the data source at deployment time. The interesting aspect of this approach is that it is dynamic and requires no administrative or other action on the part of the user.

In addition, through the GetObjects method of the IObjectSpace interface, you can automatically query multiple objects like this:

For Each myCustomer In os.GetObjects( _
 GetType(Customer), "City = 'Richmond'")
 Console.WriteLine(vbCrLf & "Customer Id: " & myCustomer.Id & _
   vbCrLf & "Name: " & myCustomer.Name & _
   vbCrLf & "City: " & myCustomer.City & _
   vbCrLf & "PostalCode: " & myCustomer.PostalCode & vbCrLf)
Next

Again, the framework formulates the correct SELECT statement based on the OPath query and configuration files.

Because both the XmlObjectSpace and SqlObjectSpace classes inherit the IObjectSpace interface, you can also program against either one polymorphically by simply referencing a variable of type IObjectSpace and using the CreateObjectSpace method of the ObjectSpaceFactory class:

Dim os As IObjectSpace = ObjectSpaceFactory.CreateObjectSpace("customer.xml")

In this case, customer.xml is a file that specifies the ObjectSpace object to use and the arguments to pass to its constructor, adding an additional layer of abstraction.

<objectspace type="Microsoft.ObjectSpaces.SqlObjectSpace"
 xmlns="http://www.microsoft.com/ObjectSpaces-v1">
  <arg>connect.xml</arg>
  <arg>map.xml</arg>
</objectspace>
  • + Share This
  • 🔖 Save To Your Account