Home > Articles > Programming > Windows Programming

Working with .NET ObjectSpaces

  • Print
  • + Share This
Microsoft has revealed preliminary plans to introduce an object-mapping layer dubbed the ObjectSpaces framework. In this article, Dan Fox walks through an example of using ObjectSpaces to map a Customer table in SQL Server 2000 using VB .NET.
From the author of

In my previous article, I reviewed the architectures of J2EE and its EJB specification and Microsoft's COM+ and .NET platforms. Currently, one of the differences in the offerings is the inclusion in the EJB specification of container-managed and bean-managed persistence using entity beans. Briefly, this concept allows either the J2EE vendor or the developer to map data in a data store to bean classes (referred to as entity beans), thereby providing an object-oriented programming model. Now, however, Microsoft has revealed preliminary plans to introduce an object-mapping layer of its own, dubbed the ObjectSpaces framework. In this article, I'll walk through a simple example of using ObjectSpaces to map the contents of a Customer table in SQL Server 2000 using VB .NET.

Creating the Persistent Class

As with an entity bean in EJB, the first task in exposing data in this model is to define the interface of the entity that you want to expose. In EJB, this is accomplished by creating a remote interface containing methods that expose the data that will be used by the client. For example, a customer interface for a hypothetical company called CompuBooks might look like the definition in Listing 1.

Listing 1: A Hypothetical Remote Interface for a Customer Entity Bean in Java

package com.CompuBooks.customer;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface Customer extends java.ejb.EJBObject {
  public String getFName() throws RemoteException;
  pubic void setFName(String fname) throws RemoteException;
  public String getLName() throws RemoteException;
  pubic void setLName(String lname) throws RemoteException;
  public String getAddres() throws RemoteException;
  public void setAddress(String address) throws RemoteException;
  public String getCity() throws RemoteException;
  pubic void setCity(String city) throws RemoteException;
  public String getStateProv() throws RemoteException;
  pubic void setStateProv(String stateprov) throws RemoteException;
  public String getPostalCode() throws RemoteException;
  pubic void setPostalCode(String postalcode) throws RemoteException;
  public String getEmailAddress() throws RemoteException;
  pubic void setEmailAddress(String emailaddress) 
   throws RemoteException;

Additionally, in EJB you need to specify a primary key field either by creating a custom class that encapsulates the key or by using the <primarykey-field> element (in EJB 1.1 or later) in the deployment descriptor. In either case, you must define a unique field so that the container knows how to uniquely identify the object. In addition, you must define a home interface so that the container can create, locate (find), and remove objects as needed. Finally, you must create a class that implements the methods of the remote and home interfaces.

In the ObjectSpaces framework, defining the object is accomplished by creating a single persistent class. Rather than separating the interface from the implementation as in EJB, a persistent class performs the functions of the remote and home interfaces in addition to the implementation class. The persistent class is actually an abstract class (defined with MustInherit in VB .NET and abstract in C#) that defines the properties, fields, methods, and events used by the client. Listing 2 shows the persistent class for the Customer object written in Visual Basic .NET.

Listing 2: A Persistent Customer Class

Namespace CompuBooks.Data
 Public MustInherit Class CustomerOS

  Public MustOverride ReadOnly Property Id() As Integer
  Public MustOverride Property FName() As String
  Public MustOverride Property LName() As String
  Public MustOverride Property Address() As String
  Public MustOverride Property City() As String
  Protected MustOverride Property _stateProv() As String
  Public MustOverride Property PostalCode() As String
  Public MustOverride Property EmailAddress() As String

  <AliasAttribute("_stateProv")> _
  Public Property StateProv() As String
    Return _stateProv
   End Get
   Set(ByVal Value As String)
    If Len(Trim(Value)) <> 2 Then
     Throw New ArgumentException("State must be 2 characters")
     _stateProv = Trim(Value)
    End If
   End Set
  End Property

  Public ReadOnly Property Name() As String
    Return Trim(Me.FName) & " " & Trim(Me.LName)
   End Get
  End Property

  Public Sub OnCreate() 'can accept arguments
   ' called at object creation
  End Sub

  Public Sub OnMaterialize()
   ' called the first time an object is retrieved from the data store
  End Sub

  Public Sub OnDelete()
   ' called when the object is deleted from the ObjectSpace
  End Sub

 End Class
End Namespace

At runtime, the ObjectSpaces framework creates a derived class from the persistent class and maps the data from a DataSet that it manages behind the scenes into and out of the members of the class. In addition, you'll notice that you can provide your own properties and methods (such as Name in Listing 2) that can be calculated from other members or can perform other business functions. This is analogous to creating calculated or non-persistent properties in container-managed entity beans by providing code in the ejbLoad method of the EntityBean interface within the implementation class.

Two of the interesting features of the persistent class are that you can insert business logic into the Get and Set blocks of a property and that you can reference remote methods to abstract business logic. The former feature is illustrated in the StateProv property that aliases the abstract_stateProv property by including the AliasAttribute and includes logic to validate the property as it is populated in the Set block. The latter feature is beyond the scope of this article, but it entails creating an abstract method in the persistent class and then referencing the remote method in the XML mapping file discussed later.

Another interesting feature of the persistent class is that it supports the OnCreate, OnMaterialize, and OnDelete methods called by the ObjectSpaces framework when the object is created, populated from the data store, and removed (respectively). Although it is not shown in Listing 2, the OnCreate method is particularly useful for passing arguments to the class as it is instantiated. For example, it can be used to assign a client-generated primary key value to the object. The OnCreate method is analogous to the create methods that you can define on the home interface for an entity bean.

Although not shown here, the persistent class can also include attributes that identify the primary key field and that link persistent classes in a parent child relationship. In that way, when the ObjectSpaces framework instantiates an object of type Customer, for example, the client can traverse the orders for that customer as well, assuming that you created a persistent class for Orders. This functionality is not readily available in EJB implementations.

  • + Share This
  • 🔖 Save To Your Account