Using Separate Interfaces from Script

[Previous] [Next]

In Chapter 9, "Using Separate COM Interfaces," we advocate the use of separate COM interfaces for objects in the main business tier. This isn't easy to do for a script client. Here's the reason: Say that you have a class CA in a component PA. A client program wants to create an object from this class, planning to communicate with it through the IA interface, as shown in Figure 7-3.

Figure 7-3. Client uses object of class CA through interface IA.

In Visual Basic, you specify the interface you want to use when you declare the variable used to refer to the object in question. Then you create the object in the usual manner, using the ProgId of the class you want to create the object from.

The funny part is that the variable is declared with one data type that represents the interface, and then the object is created with another data type representing the class. See the boldface type in the following code snippet:

Dim objA As IA Set objA = CreateObject("PA.CA")

The declaration specifies the interface; the CreateObject statement specifies the class implementing the interface.

One of the problems with script code is that you can't do this. You can't specify for a variable which data type it should hold. All script variables are Variants. Here's the script equivalent of the code above:

Dim objA Set objA = ADS1.CreateObject("PA.CA")

Because you haven't specified a separate interface, the script creates the object and makes it available through its default interface.

You can, however, use interface casting to specify a separate interface even in a script. To do this, your class must have an interface casting method that will return a specific interface to the object created. Through it, script clients can use the returned interface. The following is an example of an interface casting method:

Public Function GetIA () As IA Set GetIA = Me End Function

This function returns a reference to the IA interface. A script client can use this method, as in the next example:

Dim objA Set objA = ADS1.CreateObject("PA.CA") Set mRs = objA.GetIA.GetById(100)

As you can see, the object is implicitly declared as a Variant, which is the only way to declare it in script, and then created in the usual way. After creating it, the script has access to it through its default interface. Thanks to the public GetIA function, the script can then call the GetById method through the IA interface, even though that method isn't exposed by the object's default interface.



Designing for scalability with Microsoft Windows DNA
Designing for Scalability with Microsoft Windows DNA (DV-MPS Designing)
ISBN: 0735609683
EAN: 2147483647
Year: 2000
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net