Serializing into XML

The function GetChairState produces an XML document that describes the class instance of clsChair. The XML is a simple, single-element XML document. The element Chair contains two attributes, ID and Color. To use the XML object, a reference must be added to the project for the MSXML library. (Figure 13-7 shows the References dialog box.)

For use in this project, the Microsoft XML v3.0 library was chosen, but many other versions exist. At the time of publication, version 4 existed as well. The subroutine could have also used string concatenation to build the XML and return the string from the class. However, there is danger in using string concatenation for the purposes of constructing XML, since most XML parsers will fail easily if any portion of the XML is malformed. Creating the XML using the parser will ensure that the XML is built perfectly. Listing 13-12 shows the source code in the function GetChairState.

Listing 13-12: Function GetChairState

start example
 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '   ChairID - read only '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public Property Get ChairID() As String     ChairID = Trim(m_sID) End Property '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'GetChairState 'serializes object state into XML DOM 'to look like this <Chair ID="" Color="" /> ' 'in: nothing 'out: returns DOMDocument30 object filled '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public Function GetChairState() As DOMDocument30 On Error GoTo Sub_Error_Handler Const ERROR_MESSAGE_INFO = "GetChairState" Const CHAIR_ELEMENT = "Chair" Const CHAIR_ID_ATTRIBUTE = "ID" Const CHAIR_COLOR_ATTRIBUTE = "Color" Dim xmlChair As MSXML2.DOMDocument30 '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'note : Constructing the XML using 'string concatenation and using the 'XML object to validate is another 'strategy often times used. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     'refresh the object     OpenChair Me.ChairID     'build XML document     Set xmlChair = New DOMDocument30             'create the element for Chair     Set xmlChair.documentElement = _         xmlChair.createElement(CHAIR_ELEMENT)         'add the Chair attributes     xmlChair.documentElement.setAttribute _                              (CHAIR_ID_ATTRIBUTE), ChairID     xmlChair.documentElement.setAttribute                               (CHAIR_COLOR_ATTRIBUTE), color            '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ sub_Exit_Done:     'return object     Set GetChairState = xmlChair             On Error Resume Next     'destroy objects     Set xmlChair = Nothing         Exit Function Sub_Error_Handler:     ProcessErr " Failure serializing chair into XML. "     End Function
end example

After observing the code in Listing 13-12, it is clear why you might want to use string concatenation as a means of building XML. The use of the DOM Document is not intuitive and seems like a lot of work to build a formatted string. Version 1 of MSXML.DLL, the XML library, was an extremely slow and resource intensive as well. A savvy programmer could produce XML much faster by concatenating strings together. In addition, VB is inefficient at performing string concatenation. Minimizing the changes that occur in a string as much as possible will greatly improve performance. Because the technologies related to XML are rather new, the software that supports the technology is fluxing a great deal as well. Testing the hypotheses of manufacturer's claims and author's assertions in articles and books is a must for the programmer working with this technology because of the continuous changes occurring.

Enhanced Test Harness

The new clsChair may be unit tested after the test harness shown in Figure 13-8 is enhanced to test the new interface recently added. Command buttons Get XML, Open a new chair instance, and Create New chair instance were added to the test harness, as shown in Figure 13-19. Before you click the Open button, a chair ID argument must be entered into the text box.

click to expand
Figure 13-19: Enhanced test harness for clsChair

Setting Binary Compatibility in VB

If the ConfigSeat.dll is recompiled and the test harness is not compiled after ConfigSeat.dll is compiled, the test harness might fail to create a class instance. This condition may exist for any COM object if the COM object is recompiled and the other software that uses the COM object has references to the COM object inside it. In VB, an ActiveX DLL project is set to have project compatibility by default in the Project Properties window's Component tab. If the ActiveX DLL project in question is recompiled, the compatibility is not set to binary compatibility, and other software with references compiled to the DLL in question is run, the software referencing the ActiveX DLL will fail.

To illustrate this point, consider this scenario: ConfigSeat.dll was compiled at 0900 this morning. At noon, a test harness named testconfigseat.exe was compiled. The test harness testconfigseat.exe has specific references to ConfigSeat.dll and it names the class specifically in the class instantiation code. At 1800, the ConfigSeat.dll project was recompiled and project compatibility was set. When testconfigseat.exe was run at 1808, it failed because it could not find ConfigSeat.dll because the CLSID was different. Each time ConfigSeat is compiled without binary compatibility, a new CLSID is generated. Setting the component to use binary compatibility, as shown in Figure 13-20, will make VB use the same CLSID each time the DLL is recompiled, so all other dependent software will continue to function.

click to expand
Figure 13-20: Component tab of the Project Properties window with Binary Compatibility set

An application consuming an ActiveX DLL expects an interface for a given CLSID. Changing a class's interface in any way except to add a new function or property will cause consuming applications that rely on the class to fail. New functions may be added, but nothing in existence may be changed or removed. The CLSID must also remain the same for the name of the library and class, which is what compiling with binary capability will accomplish. The benefit of using project compatibility is that the programmer can reference an ActiveX DLL project and change the interfaces of the classes inside easily during a development effort. Since the interface may change during development, project compatibility will cause VB to allow the changes to take place without warning the programmer of breaking compatibility if a public function is deleted or changed.

Deploy to COM+ with Constructor String

ConfigSeat.dll may be copied to the next server environment and placed into the COM+ application, replacing the previous version of ConfigSeat.dll. The COM+ application in Component Services might have the DLL locked. Performing a shutdown on the application in Component Services will release the DLL so that it can be deleted. If the DLL were participating in a web application, the web server would no longer be utilizing the clsChair class after the classes in the DLL are deleted from the COM+ application.

The next effort that should take place is to load the new version of clsClass to the COM+ application New ConfigSeat Web.

  1. From the Component Services management console, delete the old version of the ConfigSeat.clsClass by right-clicking the component and choosing Delete.

  2. Add the new version of the component the same way that the first version was added, by using the COM+ Component Install Wizard.

  3. After the wizard is finished, right-click the class in the Component Services management console and choose Properties to open the Properties window for the ConfigSeat.clsClass class.

  4. Select the Activation tab and check the box Enable Object Construction. Then enter the text that the class should obtain from the event subroutine IObjectConstruct_Construct that was shown in Listing 13-4. In the specific case of ConfigSeat.clsClass, the constructor string must be set to an acceptable ADO connection string so that ConfigSeat.clsClass can access the data in the database, as shown in Figure 13-18.

  5. Verify that the class clsChair was loaded by running a quick unit test on it. The test harness might do a nice job of providing a quick unit test clarification of the class's functionality within the new environment. The executable could be copied to the host and started up. A simpler test could also be run using the DescribeChair.asp (see Listing 13-2) and adding a few new function calls to stress the component. Listing 13-13 shows an enhanced version of DescribeChair.asp called DescribeChairFinal.asp.

Listing 13-13: DescribeChairFinal.asp used to demonstrate added interface of clsChair.

start example
 <%@ Language=VBScript %> <HTML> <HEAD> </HEAD> <BODY> <% dim i dim o dim oXML      Response.Write("<p>start Test</p>")      'create the object since this is hard work      set o = Server.CreateObject("ConfigSeat.clsChair")      'demonstrate that values were changing      Response.Write("<p> color=" & o.color & "</p>")      Response.Write("<p>change color to blue</p>")      o.color = "blue"      Response.Write("<p> color=" & o.color & "</p>")      if o.CreateChair() then       Response.Write("<p> new </p>")      else       Response.Write("<p> possible error=" & o.ChairError & "</p>")      end if      set oXML = o.GetChairState()      Response.Write("<p> xml=" & oXML.XML & "</p>")      Response.Write("<p> open new chair</p>")      if not o.OpenChair("20021129095221") then       Response.Write("<p> possible error=" & o.ChairError & "</p>")      end if      Response.Write("<p> new chair color should be purple</p>")      Response.Write("<p> new chair color =" & o.color & "</p>")      'destroy object so it can be created again      set o = nothing %> </BODY> </HTML>
end example

Run the load test script for the Microsoft Application Center Test tool that was created earlier in this chapter, and the results demonstrate that the work of the ASP to COM to database application was much more demanding than the simple ASP to COM web application. The previous version of clsChair did not possess any interaction with a database, and the database is hosted on the same server that hosts the ASP and the COM object clsChair. Simulating the same 100 concurrent users for 5 minutes, the application was able to maintain an average demand of 45 page requests per second.

Figure 13-17 shows Component Services displaying the status of the previous test. In the first test performed on the previous version of clsChair, the In Call and Call Time headings never registered any value other than 0. In the latest version of clsChair, the In Call showed 0 to 1 consistently and the Call Time was as high as 15 milliseconds during the load test. Clearly, the performance of the component dropped significantly from the load test run previously. As expected, the additional burden of reading the database diminishes the efficiency of the web application.




IIS 6(c) The Complete Reference
IIS 6: The Complete Reference
ISBN: 0072224959
EAN: 2147483647
Year: 2005
Pages: 193

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