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

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

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

590 Project 5 CREATING A COM COMPONENT USING ATL

STA. Objects in an STA can be accessed by only one thread at a time. If multiple threads attempt to access these objects simultaneously, then those requests are queued up (in the message queues). When a thread releases its hold on the objects, the next thread in the queue gains access. Therefore, a component following this model will never be accessed more than one thread concurrently.

MTA. Multiple threads can access objects in an MTA simultaneously.

 

 

 

 

 

Y

 

As a result, in this model, an object’s data is prone to concurrent access,

 

 

 

 

L

 

 

and hence to damage. The onus to secure the data lies entirely with the

 

programmer.

 

F

 

 

 

 

 

 

 

 

 

 

M

 

 

 

TIP

 

A

 

 

A process can have a combination of the STA and MTA models. The number of STAs

 

 

T

 

 

 

and MTAs that each of these models supports is listed here:

Single-threaded

 

1 STA

0

MTAs

Apartment-threaded

E1 or more STAs

0

MTAs

Mixed

 

 

1 or more STAs

1

MTA

Free

 

 

0 STA

1

MTA

Now that you have gained enough footage in COM, you are ready to learn about COM+. COM+ is a set of services provided for easing the development of distributed applications. To understand COM+, you need to first have a clear understanding of Windows Distributed interNetwork Applications Architecture (Windows DNA), which is a distributed computing architecture.

Windows DNA

Besides being a technology for code reuse, COM also plays a significant role in Microsoft’s architecture for building n-tier applications, Windows DNA. Windows DNA is a distributed computing architecture that provides a platform and a set of services that facilitate the creation of tightly coupled Windows-based applications. Windows DNA enables you to build scalable and n-tier applications that can be delivered over any network. These applications are also flexible and dynamic, and thus can be easily tailored to meet the ever-changing business

Team-Fly®

INTRODUCTION TO COM Chapter 17 591

needs. In addition, Windows DNA applications can be easily integrated with existing systems. The basic structure of the Windows DNA architecture consists of three tiers, the same as the three-tier architecture introduced at the beginning of this chapter:

Presentation tier or UI tier. This includes all those applications that can be used to access the services offered by an application. The presentation tier can include either a thin client, such as a browser, or a rich client, such as Visual C++ or Visual Basic, as the interface to an application.

Application services or the business logic tier or middle tier. This consists of software components that bind the presentation and the data tiers. Some of the technologies involved in this tier are IIS, COM, and COM+ services. As the name indicates, the business logic and other application services reside in this layer.

Data access and storage services or database tier. This layer aids an application in handling data storage and retrieval operations. This comprises the data access technologies, such as OLE DB and ADO.

What follows is a description of the basic principles that acted as guidelines in designing the Windows DNA architecture:

Exploiting Web computing. Windows DNA was designed keeping in mind the need for solutions that have a global reach.

Interoperability. Another important principle was to enable organizations to integrate new applications with existing ones. Organizations needed an architecture that would simply extend existing applications with new functionality instead of rewriting them. In addition, new applications needed to be built in a manner such that they are able to integrate other vendor applications.

Integrated solution. Features such as security, management, and transaction monitoring are integral parts of the Windows DNA architecture. This ensures successful deployment of scalable and manageable distributed applications.

Lower cost of ownership. One prime principle on which the Windows DNA architecture was based was that the application’s deployment and maintenance cost should be low — developers should be able to update applications easily, and at the same pace as its associated technologies changes, and the cost involved should also be low.

592 Project 5 CREATING A COM COMPONENT USING ATL

From the preceding discussion, it is evident that COM+ services are part of the middle tier of the Windows DNA architecture. Next I will address the question

— What are COM+ services and why are they required?

COM+

The primary goal of COM was “efficient code reusability.” As distributed applications were fast gaining popularity, the need to extend the functionality of COM to work with distributed applications was felt. To address this need, Microsoft enhanced COM slightly and released it as Distributed COM (DCOM).

Following DCOM, Microsoft Transaction Server (MTS) was introduced. MTS is a runtime environment that can host COM-based components. MTS was mainly designed as a tool to assist the middle-tier issues. The main focus area of MTS is to handle all transaction-related issues in the middle tier so that developers can concentrate only on providing apt business services. MTS also provides developers with server-side services, such as handling security and resource pooling.

MTS, which was mainly designed to offer middle-tier services, was a tremendous success. Microsoft soon realized the dependency of the developers on such mid- dle-tier services. As a result, it decided to provide these services as part of the operating systems. This led to the introduction of COM+ services. Very often, COM+ is defined as follows:

COM+ = COM + MTS

Remember that COM+ is a separate entity and not the same as COM. COM+ is a combination of MTS and other such services that facilitates the creation of distributed applications. COM+ enhances the transaction services, security services, and synchronization services of MTS. It offers additional services, such as event services, load-balancing services, queued components services, and in-memory database services.

Besides these services, COM+ also supports distributed transactions and rolebased security. Additionally, COM+ implements a new threading model, the neutral apartment-threading model, and also provides role-based security. COM+ uses attribute programming as its programming model. I will now discuss some of the prominent features in brief.

INTRODUCTION TO COM Chapter 17 593

In-Memory Database

In-Memory Database (IMDB) services of COM+ enable you to provide fast and secure access to data residing in the database. IMDB enables caching of the frequently used database objects in the memory, thereby increasing the speed of data access. Like the other COM+ services, IMDB is available to the middle tier.

Load Balancing

One of the main concerns of an enterprise application is to maintain a balance between the available number of servers and the number of clients accessing them. The number of clients accessing the server and the load of the server are directly proportional to each other. So, it’s absolutely important to maintain the balance between these two factors so that there is no overflow in the load at the server side. The load balancing service of COM+ helps to handle such a scenario efficiently. Load balancing dynamically distributes the load across the available servers in a server pool.

Role-based Security

One of the built-in services that COM+ provides is role-based security. Roles enable you to provide controlled access to the application’s resources. You can assign appropriate roles to different users. Each role has some associated rights that govern the type of access the user has for a particular resource. A developer can code to check for security based on roles. For instance, if the logged-on user is an administrator, then he/she can modify the database. As an administrator, you can deploy role-based security by creating user accounts and making them members of the appropriate groups, such as Power Users and Administrators.

New Threading Model

The neutral apartment-threading model works on the grounds of multithreaded apartments. The only difference between the two is that in the neutral model, objects can execute on any type of thread. However, a process can have only one neutral apartment, just like a process can support only one MTA.

594 Project 5 CREATING A COM COMPONENT USING ATL

Object Pooling

Object pooling is one of the automatic services provided by COM+. This service enables an application to create objects and maintain a pool of the same as required by the application. You have the facility to administer the pool of objects of the available components and configure the pool size and other attributes of the pool. When you execute an application, the onus of maintenance of the pool lies with COM+.

Queued Components

Queued Components service provides an easy and fast mechanism to create and execute components. These services are based on MSMQ. Queuing enables connectionless communication; hence, processing can take place without having to bother about whether or not the sender is available at the other end. The information lies in the queue until the receiver receives it.

Queued Components service allows you to take advantage of MSMQ without having to explicitly program using the MSMQ API.

COM+ Events

Events are used to manage a connection between a publisher and one or more subscribers and then manage the delivery of events to those subscribers. Using the COM+ Queued Components service, you can queue the publisher’s message and later relay it to the subscriber, thereby saving processing time. Whether or not you need to use the Queued Components service depends on the underlying business logic of your application. If you need to have events that are time independent, you can create them by composing COM+ events with COM+ Queued Components service.

In a typical distributed scenario, one main concern is to keep the parties involved updated with all changes. Earlier, a process called polling the publisher accomplished this, wherein the update was provided through a periodic method call. The inherent drawback of this method was the time lag involved in the update reaching the subscribers.

COM+ events are often referred to as loosely coupled events (LCEs). In this model, the publishers send out an event notification corresponding to each change, and these events are stored in a COM+ catalog. The subscribers can then refer to this catalog and get the required update.

INTRODUCTION TO COM Chapter 17 595

NOTE

COM and MTS use the Windows registry for storing all configuration information. In COM+ all this will be stored in a new database called COM+ Catalog. COM+ Catalog combines the COM and MTS registration models and provides administrative services for the components.

Automatic Transactions

The concept of Automatic Transactions is based on the assumption that all components are either transaction-aware or transaction-unaware. The components falling under the former category are called transactional components. As stated earlier, COM+ programming is based on attributed programming. Therefore, the requirements of these components are provided as a set of declarative attributes, such as the transaction attribute.

Summary

This chapter gave you insight into the evolution of COM and what COM is all about. The chapter discussed the concepts that you need to understand to create a COM component. You also learned about the threading model used by COM. Finally, the chapter discussed the evolution of COM+ services and their uses.

In the next chapter, you will learn to create a COM component using ATL 7.0.

This page intentionally left blank

Chapter 18

Introduction to Attribute Based Programming

Programming has never been simpler, thanks to the introduction of attributes. This is especially true for Visual C++ programmers. You will be able to appreciate this feature once you see it working. Chapter 19 describes the implementa-

tion of attributes. This chapter provides the basic details about attributes.

Introduction to Attributes and Reflection

Attributes are elements that you can add to an application to provide more information about certain sections of the program. The data could be properties of your methods, your classes, your applications, compiler instructions, and so on.

Although attributes are very useful, they are easy to implement, unlike some other useful features. For example, in Chapter 23 on creating Web services you will realize that the way to declare a method as one that will be exposed by the Web service, you will simply use the [WebMethod] attribute. This attribute tells the compiler that this is a method meant to be exposed by a Web service and that the SOAP messages are to be created for it. If all this discussion of Web services sounds strange to you, just understand that attributes are an easy way to do many complex tasks.

Most project templates in Visual C++ support attributes. However, it is an optional feature that you can turn off by unchecking a check box. Try creating a COM component with the attributed option turned off and once with it turned on. Compare the code the wizard creates for you, and you will appreciate the use of this new feature.

The .NET Framework predefines certain attribute types and uses them to control the runtime behavior of the applications. Some languages predefine attribute types to represent language features that are not directly represented in the common type system of the .NET Framework. In this chapter, you will look at the various categories of attributes provided by Visual C++.

INTRODUCTION TO ATTRIBUTE BASED PROGRAMMING Chapter 18 599

Categories of Attributes

Visual C++ provides various attributes that you can use. These attributes are organized into logical categories having related functionality. Table 18-1 lists the various categories of attributes and their corresponding description.

Table 18-1 Attribute Categories in Visual C++

Attribute Category

Description

COM Attributes

Insert code to support areas related to COM develop-

 

ment and .NET runtime development.

IDL Attributes

Allow you to modify the IDL file from within a source

 

code file.

ATL Server Attributes

Create a Web application or Web service request han-

 

dler, or a performance-monitoring object by inserting

 

code based on the ATL Server classes.

OLE DB Consumer Attributes

Create a working OLE DB consumer that performs

 

tasks, such as opening tables, executing commands, and

 

accessing data by inserting the code based on the OLE

 

DB Consumer Templates.

Compiler Attributes

Provide attributes supplied by the Visual C++ compiler.

 

 

NOTE

Some of the attributes listed in Table 18-1 are part of the .NET Framework and, as such, are useable from all .NET languages. However, some of them, such as the ATL Server attributes, are specific to Visual C++. In this book, you will be introduced to those specific attributes as and when you create a project involving them.

Refer to Appendix C for a list of some of the attributes available in the categories specified in Table 18-1.

The introduction of attributes not only has made C++ more dynamic, but also has introduced certain benefits: