All good programmers will do their best to make the program they write more reusable, because it allows you to quickly write more robust and upgradeable programs.
There are two choices to make code reuse:
1. White box: The simplest one is to copy your tablet to another file.
2. Black box: It consists of connecting the compiled pieces of the program. So the compiled black box class library that the client can invoke is called a component.
. NET also provides developers with a way to build and expand components similar to COM. Developers are easily fooled by these two component-based development models, so let's take a look at these different development approaches so that we can eliminate the doubts.
the generation of COM
In the previous programming process, programmers placed their libraries in a separate file called the object file, in which the compiled code was included. When programmers want to use a special target file, they compile the client program into machine code, then rely on the dynamic link to connect the client program to the target file, and finally become a single executable file. The only benefit of this approach is that it saves time in compiling library functions. But it has many drawbacks, such as the fact that there is a library in each individual executable that is included, a lot of wasted storage space, and the maintenance of the application is very difficult, if a bug is found in the library, the entire executable is recompiled and distributed.
There's more than one serious limitation. A client application must be used together with a library of functions in the same language. For example, a client application written in QuickBasic cannot reference a library of functions written in C + +.
As a result, Microsoft has produced com,com just a specification. Whatever language the component is written in, it can be called by a client program written in any language, as long as the COM specification is met. In addition, programmers no longer have to worry about creating a single executable file because the component is identified by a GUID (Global unique identifier, the world's only identifier). The GUID is a 128-bit number, and some related information is placed in the registry of the system to uniquely identify the component. Client applications only dynamically build an instance of a component during run time and use the functionality of the component, so only a copy of the library is required. Its disadvantage is that we often refer to quot;dll hell. " This problem is raised when a DLL is to be replaced by a new version of the DLL. Developers have to eliminate the use of a reference to this component by shutting down all client applications (and, if not, shutting down the WWW service). Sometimes all the methods do not work, then you have to restart the server before you can replace the old DLL.
COM +
In order for an enterprise-class application to be able to use COM, it must have the following specific capabilities.
· Verification capabilities
· Objects pool (Object Pooling)
· Transaction processing
· Support for distributed architectures
To make it unnecessary for developers to add these capabilities to their components, Microsoft has produced DCOM (Distributed COM Distributed COM) and MTS (Microsoft Transaction server Microsoft Transaction Server). With these two technologies, developers can devote their energies to their business logic without having to put them on their components in the background.
DCOM is an RPC (Remote Procedure Call) protocol that is used for communication between distributed components. The client sends a request to the proxy class of a local machine, which is then implicitly given to the "root" class on the remote machine by the proxy class, and then the result is sent back to the proxy class, and the proxy class sends them back to the client. Therefore, the location of the client program is completely independent of the location of the component. The disadvantage of DCOM is that because DCOM uses a separate hardware port instead of the 80 port of the HTTP protocol, it is important to ensure that the port is open during communication between components. This is a serious security issue. So DCOM is not able to easily traverse firewalls.
In order to use MTS, programmers put special MTS hooks in their components and put them in the MTS package after compiling. Putting a related component in a single package has its own benefits. When a customer requests an instance of a component in a package, MTS ensures that a new dedicated thread is created for the package, a new instance of the component is built on the thread and the transaction service is applied. As for whether the object Pool service or the security Service is to be established, it depends on the developer's request.
MTS allows related job units to be treated as a transaction, which means that if all the job units are successfully completed, the entire transaction is treated as successful, whereas if a unit is not successfully completed, the entire transaction will be re-reincarnated.
After the client requests the object and releases the object, MTS still holds the object, so when another client requests the same component, MTS gives it the saved object. In this way, MTS reduces the number of times the server source is instantiated.
MTS allows developers to use security measures to assemble their components so that they have the ability to identify the customer requesting its service. This ensures that the functionality of the component is not available to unauthorized customers.
MTS is fully integrated into Microsoft's Windows 2000 operating system in the name of COM +, but COM + is not just MTS, it also includes some other services. MSMQ (Microsoft Message Queue Server), a service that is published with MTS, is also integrated into Windows 2000 in the name of COM +. MSMQ allows the server-side and client to synchronize traffic. The Event service is also added, which enables the server to communicate the occurrence of events synchronously with the client. Load Balancing Service (balancing) automatically instantiates request objects on the machine with the most resources on the server.
. NET
. NET provides a new way to build and expand components. It is the famous assemblies. With COM, the developer must register the component on the server, which means that the information for the components in the system registry needs to be updated. The purpose of this is to guarantee the central location of the component so that COM + can find the appropriate component. Use. NET assemblies, the assembly (Assembly) file presses all the required metadata (meta data) into a special segment called Manifests (list). In. NET, to make assembly effective for users, simply put them in a directory. When the client requests an instance of a particular component. NET runtime (runtime) searches for assembly in the same directory, and when found, analyzes the manifest in order to obtain information about the classes provided by this component. Since the component information is in manifest, there is no need for the developer to register the component with the server, so that several identical components can be allowed to coexist safely on the same machine.
Building a. Net assembly is not like building a VB6 component, and the only thing that bothers developers is the business logic, all of the backstage code. NET run time, and due to. NET runtime has the ability to fragment the collector, the component does not have to worry about its number of references (in COM, it relies on IUnknown's help). Simply put, building a assembly in. NET is much simpler than building a VB6 com.
Pure. NET assemblies cannot be registered under COM + services because they are binary standards that are different from COM. The prospect of facing. Net,assemblies is "advanced com" relative to COM. However, because of the reliability of the application currently architected on COM +, COM will continue for some time. That's probably why Microsoft is providing developers with the tools to develop. NET assemblies and COM.
The Type Library Importer (TLBIMP.exe) tool encapsulates COM components into. NET so that previous things can continue to be used in. NET Applications.
The typelib Exporter (Type Library Exporter (TLBEXP.exe)) tool will. NET components into COM, this tool is also very useful, if you want to use your. NET assemblies to replace the original COM component, it will need to use it. The services provided by COM + cannot be ignored, so encapsulating. NET assemblies as a COM component becomes quite important. As an option, developers can choose from the. NET base Class library to select more features.
The difference between COM, COM + and. NET