Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of

Creating the Mapping Files

When the persistent class is complete, you can create an XML mapping file and source file that defines both how columns in a database map to the properties of the persistent class and the data store connection information for that class. The mapping file and the connection file shown next are directly analogous to the vendor-specific deployment tools provided by J2EE products for creating entity beans that use CMP.


As an alternative to simply creating the mapping file by hand, the ObjectSpaces framework will ship with a graphical tool that can be accessed by right-clicking on the project in VS .NET and selecting the template item under Add New Item.

The simple mapping file for the Customer persistent class is shown in Listing 3.

Listing 3: XML Mapping File

<map xmlns="http://www.microsoft.com/ObjectSpaces-v1">
 <type name="Customer" dataSource="Customers" source="CompuBooks">
  <uniqueKey name="Id" mappedBy="autoIncrement" 
  <property name="FName" dataSource="FName"/>
  <property name="LName" dataSource="LName"/>
  <property name="Address" dataSource="Address"/>
  <property name="City" dataSource="City"/>
  <property name="_stateProv" dataSource="StateProv"/>
  <property name="PostalCode" dataSource="PostalCode"/>
  <property name="EmailAddress" dataSource="EmailAddress"/>

You'll notice from Listing 3 that the type element is used to reference the persistent class in addition to specifying the name of the table (dataSource) and the connection (source) to use to get to the table. Each property element maps a property of the persistent class (name) to the name of a column in the table (dataSource). In addition, here the primary key is identified using the uniqueKey element and mapping it to the CustomerId column in the table. The mappedBy attribute indicates that the column is generated automatically by the data source for use with IDENTITY columns in SQL Server and sequences in Oracle. Because the Id property is generated from the data source, the property definition in Listing 2 is set to ReadOnly.

In addition to creating the mapping file, you provide a connection file as shown here:

<sources xmlns="http://www.microsoft.com/ObjectSpaces-v1">
 <source name="CompuBooks" adapter="sql" 
  connection="Data Source=ssosa; Integrated Security=SSPI; 

The connection file simply identifies the connection (source) referenced in the mapping file, in addition to the .NET data provider to use (sql or oledb) and the connection string to pass to the database server. By putting the connection information in an XML file, you can abstract it from the client code as well.

  • + Share This
  • 🔖 Save To Your Account