You first use the RDS CreateObject method to create an instance of the DataSpace object and instruct it to initialize an instance of a given COM component on a specified Web server. The proxy serves as a local copy of the remote object, supporting the same methods and properties. The mirror object, however, doesn't execute any code in response to calls; it simply issues a remote method invocation to the RDS stub. This code creates a local object whose programming interface is identical to that of RDSServer.DataFactory. Remember that DataFactory is a server-side component, but the invoking code runs locally. Any exchanged data travels over the network through HTTP. And a new instance of DataFactory handles each call from the client to DataFactory.
Any method's return value represents data available on the client. If the return value is an ADO record set, your RDS-based application downloads in a single session all the needed records. This capability is particularly useful when you write highly interactive applications that need to show different perspectives or representations of the same data or group the data in various ways. RDS saves you from repeated trips to the server for these different views; you simply download the data once and use it until you close the application.
Using DataSpace to call COM components through HTTP is compelling, but you can also use DataSpace as a general-purpose framework for issuing COM calls. DataSpace supports a variety of protocols and in-process calls. Besides HTTP, you can issue COM calls through HTTP over Secure Sockets Layer (HTTPS) and Distributed COM (DCOM).
RDS-Enabled Business Objects
The JavaScript example uses the RDSServer.DataFactory object as the server-side component invoked through HTTP. DataFactory exemplifies the typical RDS-compliant business object for retrieving and packaging records from an OLE DB data source. Table 3 shows the component's programming interface, including the SubmitChanges method, which in a single call lets you update a record set locally, then reconnect it to the data source to apply changes remotely.
In place of DataFactory, you can call any other COM object—including custom components—installed and registered properly on the Web server. RDS requires only that the objects qualify as regular automation servers. Be careful, however, about the code you write for various methods and properties. Remember that the browser will always invoke your object in a stateless manner, meaning that no state will be maintained across successive calls. If you need to assign a nondefault value to an internal variable before invoking a method, avoid wrapping the variable with a property. Instead, when designing an RDS-compliant component's programming interface, use a method's parameters to set that internal variable and make sure that the method executes in the right context.
You must list all objects you want to call remotely through RDS in the server's Registry under the HKEY_LOCAL_ MACHINE\SYSTEM\CurrentControlSet\ Services\W3SVC\Parameters\ADCLaunch Registry key. If you have a COM object named MyRDSComp.DoSomething, the script in Listing 1 will register it properly for use with RDS.
The DataControl Object
The RDS DataControl object, RDS.DataControl, binds the content of an HTML tag (e.g., <TABLE>, <SPAN>, <INPUT>) with a data field. You might recognize a resemblance between DataControl's function and that of data-bound controls in VB and in rapid application development tools such as Delphi. Table 4 shows DataControl's methods and properties.
You can use DataControl in two ways. First, you can use it to download a record set from a data source on a given Web server. This functionality looks like the one DataSpace provides, but you'll find significant differences. On the plus side, DataControl retrieves a record set directly through ADO without involving other objects and downloads data asynchronously. On the minus side, it can return only a record set and can't apply any business or data transformation rules.
Second, you can use DataControl's data-binding capabilities to process and display the downloaded data properly. You embed DataControl as an ActiveX control in your client HTML pages and exploit its methods and properties to connect to an OLE DB data source. Note that OLE DB data sources also include all possible ODBC data sources. (For information about OLE DB and ODBC, see "OLE DB or ODBC?" November 1999.)
The following code inserts the DataControl object in an HTML page, making the object invisible and giving it an ID to use later:
<OBJECT style="display:none" id="rds"
classid="clsid:BD96C556-65A3-11D0-983A-00C04FC29E33">
</OBJECT>
Listing 2 shows a JavaScript function you can use to download a record set and populate the rest of the HTML page.
The Refresh method uses the values you set in Server, Connect, and Sql to connect to the server and instruct the RDS stub to provide the record set corresponding to the query. Although this example names the property Sql, you can assign it any string (SQL or not) that is meaningful to the OLE DB provider you're targeting.
When DataControl has downloaded the record set, it raises a notification event called ondatasetcomplete, which you can detect and handle with scripting. You can then safely use DataControl's Recordset property to access the data locally, as the following shows:
<script language="Javascript"
for="rds"
event="ondatasetcomplete">
alert(rds.recordset.recordCount);
</script>
Finally, you use the following code to bind record set fields to the HTML tags:
<INPUT TYPE="button"
DATASRC="#rds" DATAFLD="employeeID">
</INPUT>
The DATASRC attribute links the tag to an RDS data source. You must prefix the string with a pound sign (#), which tells IE that the ID addresses an RDS data source. The DATAFLD attribute points to the name of the field where the data should come from.
A third possible attribute, DATAFORMATAS, specifies how to render a field's content. If the attribute value is the string html, IE renders the content as HTML code; otherwise, it presents the content as raw text. For example, given the tag
<SPAN DATASRC="#rds"
DATAFLD="employeePhoto"
DATAFORMATAS="html">
</SPAN>
if the employeePhoto field contains the string
<img src=01023.gif>
RDS will tell IE to render the content as a GIF picture.
When the HTML tag bound to a data source field is , DataControl automatically handles the data repetition, as Listing 3 shows. This code automatically populates the table, adding as many rows as there are records in the record set. You just define the layout of the tag, and RDS repeats it.
Putting RDS to Work
The COM-based RDS, which you install on the both the client and the server, is a Windows-specific solution that is valuable for creating intranet and extranet applications in which you control the production environment. Remember that RDS also requires a browser capable of creating COM objects. So although RDS works well with IE 4.0 or later, you'll find it difficult to use with IE 3.x or Netscape browsers. Netscape Communicator 4.6, for example, supports the ECMAScript standard 1.0 for JavaScript and doesn't include COM. Although this article covers how to use RDS in scripts and HTML pages, you can also take advantage of RDS from VB or VC++, if those techniques better match your needs.