Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать
☆

Any new technology is not a result of one day of work; it evolves. It is interesting to realize that we are part of the ever-evolving, fast-changing IT

industry — the only place where almost every dawn brings along not only a new

Y DCOM, XML — it is a never-ending list.LAlthough I will not be able to describe

technology but also an acronym for the same: DOS, OLE, COM, COM+,

all of these technologies (that is beyondFthe scope of this book), I will answer some questions of yours regarding COM,Msuch as what it is, why to use it, and how to implement it. I will also provide details of how COM+ evolved. But, before dis-

cussing these topics, I willAspend some time looking at the evolution of the various application models.E

When you analyzeTany application that you create or work with, you will find that it can be broken into three elements:

The presentation element that deals with the user interface

The business rule element that handles the implementation of the application’s logic

The data management element that handles the storage and retrieval of an application’s data

These three elements decide an application’s architecture. An application can follow a single-tier, two-tier, three-tier, or n-tier architecture model.

The applications that were designed some time ago, using C and COBOL, were monolithic. They are named so because they handle all three elements — the user interface, the business logic, and the data management — and run on a single computer. Such applications are said to follow the single-tier architecture, and thus are referred to as single-tier-based applications. Figure 17-1 depicts a single-tier- based application.

The following are the obvious disadvantages of this architecture:

The size of the executable is huge because it comprises all three elements

— the user interface, business logic, and database.

Upgrading these applications is time-consuming because for each upgrade the entire application has to be recompiled, debugged, and redistributed.

Team-Fly®

INTRODUCTION TO COM Chapter 17 561

Monolithic Applications

User Services

 

Business Services

 

Data Services

 

 

 

 

 

FIGURE 17-1 Single-tier-based application

In a multiuser environment, the application and the required database have to be installed on each user’s computer. The database has to be synchronized manually and this often results in inconsistent data.

These disadvantages, when analyzed, led to the evolution of the two-tier architecture. As the name indicates, an application based on this architecture is divided into two main components: the client and the server. Yes, this is when the ubiquitous client/server architecture came into existence. In this architecture, an application’s functionality is divided into two processes:

Client process. The client process handles the user interface.

Server process. The server process handles the business logic and the database.

Figure 17-2 depicts a two-tier-based application.

 

 

Fat Server

Fat Client

 

 

 

 

Server

Client

Business

 

 

Data

 

Logic

User tier

Tier

 

 

 

FIGURE 17-2 Two-tier-based application

562 Project 5 CREATING A COM COMPONENT USING ATL

In this architecture, the client and the server processes can either coexist on the same computer or, in a network, can exist on different computers. When these two processes exist on different computers, the data is passed to and fro, which implies that data accepted by the client process is passed to the server process for validation using the business rules. If the data is valid, then the server process updates the database and communicates to the client; otherwise, it passes an error message to the client process.

However, with an increase in the amount of data to be transferred between the client and the server, the network load increases. The load at the server end also increases because the server process has to handle all validations. To overcome this, some of the business rules were shifted to the client process, and thus evolved the distributed client/server environment.

NOTE

If a client handles business rules, it is referred to as a fat client; if a server handles the business rules, it is referred to as a fat server.

Although the two-tier architecture addresses the problems of data duplication and data inconsistency (of the single-tier architecture), this model also has some inherent disadvantages:

Increased load on the server. All clients access a central server, thereby increasing the load on the server. This leads to deterioration in the server’s performance.

Increased network traffic. Obviously, with the increase in the amount of data transfer between the client and server, the network traffic also increases, thereby deteriorating the network’s performance.

Implementation of incremental upgrades is tedious. Huge client-side executables don’t facilitate incremental upgrade. Particularly, in the case of a fat client, incremental upgrade is tedious because the application’s source code has to be modified, recompiled, and redistributed.

Applications tightly coupled with the database on the server. The clients are configured to access a specific database located on the server. As a result, any change in the database at the server side results in reconfiguring the clients.

INTRODUCTION TO COM Chapter 17 563

The outcome from the efforts to remedy the preceding drawbacks was the advent of the three-tier architecture. In this architecture, the three elements of an application are distributed across three layers of an application and are treated as services:

User services. The user services layer handles the user interface and all other user interactions.

Business services. The business services layer handles an application’s business logic. This layer encapsulates the implementation details of the business logic from the user. In addition, any changes in the business logic will henceforth affect only the business services layer and not the complete application. This layer acts as an interface between the user and the data services layer.

Data services. The data services layer handles the database of an application. This layer handles all database maintenance–related activities, such as maintaining data integrity.

Figure 17-3 depicts a three-tier-based application.

Client

User Services

Business Logic

Business

Services

FIGURE 17-3 Three-tier-based application

Data Store

Data

Services

The logical question now is, “Where does COM fit into this picture?” Read further to learn about the role of COM in implementing these three layers.

Evolution of COM

Before entering the world of COM, it is important to know about the evolution of the component technology — what COM is all about.

Way back, Microsoft came up with MS-DOS, which gave way to the advent of desktop applications. During the same time, the processors were also evolving.

564 Project 5 CREATING A COM COMPONENT USING ATL

With the then available limited-powered processors, you could run only a single application at a time. As more powerful processors were developed, Microsoft envisioned the potential for running multiple applications concurrently, and subsequently designed the Windows operating system. Slowly, the need for communication between these applications running in parallel arose. To address this, Microsoft enhanced the Windows operating system by adding the Clipboard feature, which introduced the much-used cut/copy and paste operation. Can you imagine working without this feature?

The advent of the Clipboard eased the handling of compound documents — documents that can store varied types of data, such as text, graphics, video, and so on. However, Clipboard has its own disadvantages, the most obvious being the lack of intelligent data transfer. For instance, after you copy data from an application, such as Excel, and paste it into another application, such as Word, any change in the source data will not be reflected in the copy. In other words, after you copy the desired data from the Excel sheet to the Word document, if you make any changes to the Excel sheet, the changes will not be reflected in the Word document. As a solution to this problem, Microsoft introduced object linking and embedding (OLE). OLE 1.0 was the first version released to the market in 1992.

OLE was Microsoft’s move toward object-oriented programming (OOP), which was the new paradigm that the IT industry was shifting to. Using OLE, you could link or embed, for instance, an Excel worksheet into a Word document. Considering the previous example, if you link the Excel worksheet with the Word document, then any changes to the source worksheet will automatically be updated in the Word document. The other advantage with OLE is that you can open the embedded or linked worksheet directly from within Word and edit it. Despite this advantage, OLE 1.0 was not well received by the software vendors or developers because of the complexity in its implementation. However, it led to the evolution of the component technology, which is a spin-off of the object-oriented technology. This technology treats each compound document object as a component. A component is a self-contained object that can be used in any application, thereby resulting in reusability. You can correlate this concept to the very popular Plug and Play feature.

Microsoft released the next version, OLE 2.0, in 1993. OLE 2.0 was an enhanced version of OLE 1.0. Besides the compound documents, OLE 2.0 encompassed a number of other object-based services. One of the most noted services was inplace activation, also called visual editing. This service enabled users to edit all data

INTRODUCTION TO COM Chapter 17 565

in a compound document from within the container application, without needing to open any other application. As it was realized later, OLE 2.0 was designed based on the component technology, which is now famous as the Component Object Model (COM).

Now that you have an idea of the objective addressed by COM — reusability — let me explain the role of COM in the application models that I earlier discussed. Based on the definition of a three-tiered application, it is evident that the business services layer should be independent of the user services layer and the data services layer. Besides being independent, business services should also be reusable, which implies that the code used to create a service should be reusable. The immediate technology that comes to mind as a solution provider is OOP.

OOP resulted in a pool of objects that couldn’t interact with each other “freely.” Interaction posed a problem because of the varied platforms, networks, and application types across which the objects had to interact. Although OOP promised to enable reusability, it couldn’t deliver results to its full potential. That is where COM gained its ground.

What Is COM?

Many schools of thought address this question. The two predominant ones that precisely describe COM are the following:

COM is a specification. COM is a specification put forth by Microsoft

— it is a document describing the method of creating applications based on components.

COM is a standard. COM is a binary standard specified by Microsoft for creating software components. This defines the standards and other specifications (as stated earlier) required to create components that can interact with each other regardless of the language and tool used to develop them.

In other words, COM provides the following features and functionality:

The binary standard with which you can create reusable components that can interact with each other.

COM services or APIs, available as part of the COM library. The COM library is part of the operating system for Win32 platforms, whereas it is available as a separate package for other operating systems.

566Project 5 CREATING A COM COMPONENT USING ATL

COM eases the upgrading process of your application.

COM provides location transparency, thereby enabling distributed processing.

COM components are cross-language compatible, meaning that a component in C++ can talk with another in Visual Basic.

NOTE

The interoperability between COM components is possible because of the binary standard followed commonly by these components. In simpler words, COM defines a standard that any program should adhere to after it is translated to machine code. As a result, a component written using Visual Basic can easily interact with a component using Visual C++ because both, when translated to machine language, will speak the same language.

Although the components are language independent, there is one restriction that COM enforces — the code generated using any language should be able to handle function calls through pointers. You will be able to appreciate this rule once you know the reason for it, which you will learn in the following discussion.

All of these features of COM make it the preferred technology to implement the business layer of an application.

Issues Addressed by COM

To understand the facilities provided by COM, and the need for COM, you need to understand the rationale for its existence. Microsoft has proposed COM as a solution for the following list of problems:

People have been reusing binary code for a long time — it started with the libraries of C and C++. However, until Windows popularized the use of dynamic link libraries (DLLs), most of the code reuse was static in nature, because the reused code was appended to the main executable, which resulted in bigger executables. DLLs allowed reusable code to be packaged into different DLLs, and these DLLs could be loaded by the main executable when required. This resulted in several advantages, such as smaller executables, the ability to load DLLs dynamically, and the use of shared DLLs.

INTRODUCTION TO COM Chapter 17 567

Libraries created using C/C++ compilers were found to be incompatible with executables created using a different C/C++ compiler. The factor contributing to this incompatibility is the mangling process that a compiler follows to support method overloading. In this process, whenever method names are overloaded, the compiler changes the names internally so they are actually different. This process was done differently in different compilers.

The purpose of compatibility with earlier versions of DLLs and executables was not achieved, because of memory allocation problems — changed DLLs often broke executables designed for an earlier version of the DLL. For example, consider a class that required n bytes of memory underwent a change and now required n+2 bytes. A client compiled with an earlier version of the DLL would still allocate n bytes, and hence would crash if a newer DLL requiring n+2 bytes were loaded. Versioning of the DLLs was mainly done using the file name, such as MFC40.dll and MFC42.dll, which was not very reliable.

Most importantly, only those languages that understood C/C++ interfaces could use a reusable piece of code written in C or C++.

To address the preceding issues, Microsoft had to come up with a binary standard and a set of related services — COM. In a nutshell, the following are the significant benefits of COM:

Standardized functionality across all components irrespective of the language in which they are developed

Interoperability between components, thus making reusability a reality

A good versioning mechanism that allows one system component to be updated without requiring updates to other components

Easily replaceable, and thus ideal for changing business scenarios

Now that you are aware of the evolution of COM and its benefits, you will next take a closer look at what constitutes COM.

COM Components

As stated earlier, COM is also based on the core concepts of OOP. It is a mix and match of the best features of OOP and some other enhanced features. It is this mixed blend that contributes to the creation of flexible and powerful components.

568 Project 5 CREATING A COM COMPONENT USING ATL

COM components are nothing but objects. The only difference between COM objects and other object-oriented objects is that the COM objects sign a “treaty” to follow common standards, thus ensuring that they all speak the same language. This is the feature that makes interoperability possible.

Now the question is, what makes all this possible? What unites the otherwise varied components (variations in terms of language)? All this is possible because of one prominent feature of any COM component — the interface.

Before looking at what are interfaces and how to implement them, you need to first learn about the types of COM components that you can create.

Types of COM Components

A COM component can be either a server or a client. A component that uses the services of another component (through its interfaces) is referred to as a COM client and the other component that offers the service is referred as the COM server. For instance, while working in Microsoft Word, if you choose Insert, Object, a list of COM components is displayed. These components are classified as COM servers because they expose functionality that can be consumed by other components. Suppose that you choose a component from the list, such as the Calendar control. In such an instance, the Calendar control is the COM server and the current Microsoft Word document, in which you are inserting the control, is the COM client.

Earlier in the chapter, I stated that COM provides transparency in terms of the location of components. To achieve this, based on the location of components, COM servers are classified as in-process servers and out-of-process servers. Out- of-process servers can further be either local servers or remote servers.

In-process servers are implemented as DLLs and are loaded into the client’s process space.

DLLs are libraries that are loaded dynamically into the process space allocated for the client (which invokes the DLL). Because these DLLs reside in the same address space as that of the client, the inherent advantage is the speed of servers. Implementing a DLL has the following advantages:

DLLs are faster than EXEs because they reside in the same address space as that of the client.

DLLs are efficient because the client has a direct access to the server component.

INTRODUCTION TO COM Chapter 17 569

The following are the disadvantages of implementing a DLL:

DLLs are not robust, because if the server component crashes, the client also crashes. This is due to the fact that they reside in the same address space.

DLL, the server component, has to comply with the security settings of the client.

The Kernel and the GDI that are part of the Windows operating system are implemented as DLLs.

Figure 17-4 illustrates an in-process server.

Client

In-process

Server

FIGURE 17-4 An in-process server

Out-of-process servers are implemented as EXEs, and thus have their own process space.

EXEs are loaded in a separate process space. Because these reside in a different address space from that of the client, the inherent advantage is that such processes can function as stand-alone applications and are not affected by any problems with the clients. Implementing an EXE has the following disadvantages:

EXEs are slower when compared to DLLs because they reside in a different address space from that of the client.

EXEs are less efficient because the client has to go through a proxy/stub to access the server component.

The following are the advantages of implementing an EXE: