Skip navigation

Practical COM

Speed up application development and performance

Microsoft's Component Object Model (COM) gives you a new, practical technique for building objects and code libraries. COM components contain classes that become objects when another application uses them. The objects have methods—functions and subprocedures—that you can call from EXE files, from OLE DLL files, or from within your applications.

In this component model, which application is the client and which is the server? It doesn’t matter because you can reuse components anywhere on the network. You can move components to more powerful servers to scale your applications. And you can even use Windows Load Balancing Service—or with Windows 2000 (Win2K), Network Load Balancing Service—with any TCP/IP application to balance workloads across Microsoft IIS Web servers running COM components.

Components also encapsulate methods in a format you can't easily change. Few developers would try to alter an EXE or DLL file directly, making both formats ideal as internal code libraries. Although Visual Basic (VB) 3.0 and earlier versions keep internal code libraries in BAS modules that you can easily and accidentally change, VB 4.0 and later versions let you use EXEs and DLLs to encapsulate methods and properties. In addition, encapsulating methods and properties in EXEs and DLLs forces you to use standard object notation (i.e., object.method) for object or method references, a practice that prepares your applications for the object world of tomorrow.

Creating Components


Creating a COM component is easy. Just select ActiveX DLL as the VB project type, add your code to the class, then use File Make to compile the project and generate a DLL that you can use from any application. The hard part is deciding which parts of an n-tier application to move to components and why.

I recently had to analyze a client's application and determine whether and where components could yield the most benefits. The application, written in Active Server Pages (ASP), used ADO as the interface to SQL Server. The highly structured application ran well except that certain pages took a long time to load the first time a user visited them. The code for these pages contained many include files, and IIS had to load, compile, and put all this code in the ASP cache. However, after the page was in the cache, it screamed.

You could speed up parts of this application by moving some code from ASP to COM components. For example, the application used include files to store the standardized routines for all ADO calls. By moving the routines to a VB class called ADOExecute in a component called Data, you could set all standard properties in ADOExecute without having to set unnecessary connections, cursors, or other properties. All you'd have to do is register the Data component containing the ADOExecute class on each Web server and the developers’ workstations. The developers could then use that component to develop applications more quickly. Your Web application should also execute more quickly, because it would be using compiled code along with ADO.

What else could you use COM for? Remember the ASP cache that IIS uses? Let's say you moved 200 lines of code from an include file to a COM class. IIS wouldn't need to load the include file because that code is now in the component. ASP wouldn't need to compile the ASP and ADO code because you would have already compiled the code into the component. And ASP wouldn't need to process or cache those lines the next time it loads the page. With less code for IIS to store in the cache, the page would load more quickly and more pages would stay in the cache.

If 10 pages used that include file, you could see dramatic performance improvements by moving the code to a component. Consider that when IIS processes each page, it pulls in the include file and adds it to the ASP code. The resulting ASP code for the page is the total of all the code in the original page plus all the ASP code in any pages the original includes. So by simply moving 200 lines of include code into a component, you may effectively reduce the ASP code in your application by 2000 lines. When was the last time you optimized an application with so little effort?

You can also put any type of special searching logic or routines in components. You might want to put this code in a class called Search, then add that class to the Data component. As you look for other functions to move into components, consider the types of applications you have and their structure. I typically look for code modules that model objects (such as Customer.inc) or functions that perform tasks related to objects (such as InsertCustomer and RetrieveCustomer). Functions that relate to Customer can go in a Customer class.

Migrating Over Time


You can extend components throughout your application one piece at a time, adding functionality along the way. Because you use utility functions in other functions, you should migrate utility functions first. For example, because most Customer functions already use the ADO functions, migrating the ADO functions first is critical. After you migrate the ADO functions to a class, you can change the customer ASP code to use that class. Then, when you migrate the Customer functions to a class, you’ve already made the changes necessary for the Customer and ADO classes to work together.

What should you watch for as you move ASP code to VB components? First, consider your ASP code's modularity. If the code has lots of well-structured functions, you can usually migrate it easily. Just remember to first modify functions that will make the application work as a class, then modify functions that will improve application performance.

As you move to components, also try to write classes with methods that are loosely coupled. A method should accept a set of parameters, perform its work, then return data as either the return value of the method or as a by reference parameter. Replace with parameters any session variables your code uses. Change all Server.CreateObject references to CreateObject, and replace any other ASP-specific functions so that your code returns values instead of calling the ASP object's methods. Alternatively, you could wire the ASP-specific functions into a component by using the Active Server Pages Library; just reference the library in the component, then use that reference to access the ASP objects. This approach lets your application directly access session variables and other ASP features. After you've made these modifications, you can compile the component.

Before you publish the component, however, be sure to strongly type all variables. For example, compare the following ASP variable definition:

Dim sCustomerName

with this definition, which is strongly typed in VB:

Dim sCustomerName as String

The second definition is best because it forces variable declaration as a string type and will result in faster execution than the first, which results in a variant type—the only variable type you can set in VBScript in an ASP.

You should also set explicit references to any objects, such as ADO, that your application uses. You can set the reference in the Project, References dialog box. You can then explicitly define in your code the variable that references the object:

Dim rs As ADODB.Recordset

Now when you use CreateObject to create an instance of the component, the component will use early binding to access that instance. Again, this results in faster execution because you compiled the references into the component.

The Future of Application Development


COM can ease application development, speed application performance, and set you well on the path of modern application development. Microsoft and most of the rest of the world are embracing COM, Distributed COM (DCOM), and other component models, such as Common Object Request Broker Architecture (CORBA) as the future of application development. Microsoft has invested lavishly in COM and is betting heavily on its performance. In fact, Win2K's COM+ takes COM to a new level, integrating Microsoft Transaction Server (MTS), Microsoft Message Queue Server (MSMQ), and many other functions into a complete environment for building high-performance multitier applications. Reap the benefits of components today, and get ready to migrate to the next generation of these powerful object models.

TAGS: SQL
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish