Component Tools Guide略読ノート(2):General Principles of Component Design


MSDN-2001-OCT:Visual Tools and Languages/Visual Studio 6.0 Documentation/Visual Basic Documentation/Using Visual Basic/Component Tools Guide/General Principles of Component Design
1.部品基礎
(1) An application or component that uses objects provided by another component is called a client. Components are characterized by their location relative to clients.
An out-of-process component is an .exe file that runs in its own process, with its own thread of execution.
An in-process component, such as a .dll or .ocx file, runs in the same process as the client.
(2) The names you select for your class modules and for their properties, methods, and events make up the interface(s) by which your component will be accessed.
(3)工程の種類を特定する(比較的詳細な議論があるが、メモ(1)で取ったものを略読してはっきり言っていないと思う).
(4)工事の属性を設定する.新しいcomponentプロジェクトを開始すると、name、description、startup object(初期化コードの場所を指定)を設定するプロパティがあります.version number、コンパイル、ヘルプファイル、version compatibilityなどを設定することもできます.
2.interfaceの概念及びCLSID
(1) Information about the classes provided by your component is contained in a type library. In Visual Basic, the type library is included as a resource in the compiled component. Clients access the type library by setting references to it.
(2) The combination of project name and class name is sometimes referred to as a fully qualified class name, or as a programmatic ID. Public gwdgDriveLink As SmallMechanicals.Widget
(3) Every class provided by your component has at least one interface, called the default interface, which is composed of all the properties and methods you declare in the class module.
The default interface is usually referred to by the same name as the class, though its actual name is the class name preceded by an underscore. The underscore prefix is a convention, signifying that the name is hidden in the type library.
If the class raises events, it also has an IConnectionPointContainer interface that enumerates those events. Events are outgoing interfaces, as opposed to the incoming interfaces composed of properties and methods. In other words, clients make requests by calling into your class’s properties and methods, while the events raised by your class call out to event handlers in clients.
したがって、デフォルトはincoming interfaceであり、eventsはout-going interfacesである.
(4) GUID (pronounced goo-id) stands for Globally Unique IDentifier, a 128-bit (16-byte) number generated by an algorithm designed to ensure its uniqueness.
GUIDs are used to uniquely identify entries in the Windows registry. For example, Visual Basic automatically generates a GUID that identifies your type library in the Windows registry.
Visual Basic also automatically generates a GUID for each public class and interface in your component. These are usually referred to as class IDs (CLSID) and interface IDs (IID). Class IDs and interface IDs are the keys to version compatibility for components authored using Visual Basic.
Note   You may also see GUIDs referred to as UUIDs, or Universally Unique IDentifiers.
IIDは、バージョンの互換性を維持するのに役立ちます.あなたのバージョンに互換性のない変更をする可能性がある場合、システムはあなたに警告し、適切に名前を変更することをお勧めします.
3. Starting and Ending a Component
(1)ActiveX dllやActiveX exeでは,formsを起動対象として使用することは許されない.これらは、オブジェクトの初期化が長すぎるためtime-out.
(2)lengthy initializationの処理にはいくつかの方法がある:Class_Initialize eventまたはバックグラウンドでusing a call-back timerをします.しかし,いずれの方法もグローバルフラグビットを用いて反復初期化を防止することに注意しなければならない.
(3)in-processとout-of-processコンポーネントのshut downプロセスは異なる.
4. Adding Classes to Components
(1)a programmatic ID or ProgID:コンポーネント名+クラス名、例えばFinance.BusinessRule(2)コンポーネントと他のアプリケーションとの違いは、コンポーネントがclientプログラムによって制御を使用することができる少なくとも1つの共通クラスを提供することである.
(3) Public or Instancing Property: UserControl classes have a Public property that determines whether the class is public or private. 他の種類の部品にも、クラスが外部呼び出し可能かどうかを決定するための設定があります.
5. Instancing for Classes Provided by ActiveX Components
(1)Private,PublicNotCreatable,MultiUse,GlobalMultiUse,SingleUse,GlobalSingleUseがある
(2) Dependent Objects (PublicNotCreatable)
(3) MultiUse and Multithreading
(4)Global Objectsはアプリケーションのようなオブジェクトに似ています.オブジェクトを宣言せずに直接メソッド属性などを使うことができます.面白い特性ですが、activex controlは後のいくつかのインスタンス化方法をサポートしていないので、私は使えないようです.
 
6. Coding Robust Initialize and Terminate Events
(1) Errors that occur in the Class_Initialize event procedure are returned to the point in the client at which the object was requested.
(2) Errors in the Terminate event require careful handling. Because the Terminate event is not called by the client application, there is no procedure above it on the call stack. This means that an unhandled error in a Terminate event will cause a fatal error in the component
You should always handle errors in the Class_Terminate event procedure. Errors in Class_Terminate cannot be handled by applications that use your component, and will therefore be fatal to the application.
注意:Terminaterイベントは、オブジェクトの参照カウントが0のときにvb実行環境によってオブジェクトを破棄する前に自動的に実行されます.当時、ユーザーサブルーチン環境ではなく、スタックの状況が分からず、現在のenabled error handlerがどれなのか分かりません.したがって,オブジェクトの外部にon error gotoやon error resumeがあってもこのようなエラーをキャプチャすることはできず,アプリケーションに致命的である.したがってterminateイベント内部でのみ処理できます.したがって,Terminalイベント以外の時間にはこのような捕捉不能な状況は存在しない.この投稿の討論を見て、wangmu 7206に感謝します.
7. Standard Modules vs. Class Modules
(1) Class module data, on the other hand, exists separately for each instance of the class. クラスモジュールで標準モジュールの共通変数に依存することは避けたほうがいい.論理的なエラーを引き起こす可能性があるからだ.
(2)クラスの静的データ
However, there may be occasions when you want a data member to be shared among all objects created from a class module. This deliberate violation of encapsulation is sometimes referred to as static class data.
You can implement static class data in a Visual Basic class module by using Property procedures to set and return the value of a Public data member in a standard module, as in the following code fragment:
' Read-only property returning application name.
Property Get ComponentName() As String
   ' The variable gstrComponentName is stored in a 
   '   standard module, and declared Public.
   ComponentName = gstrComponentName
End Property

8. Adding Properties and Methods to Classes
(1)Implementing Properties in Components:共通変数ではなくpropertiesをできるだけ使うのは、主に安定した考えからです.
一方、Internally、Visual Basic generates a pair of property procedures for every public variable you declare.For this reason, declaring public variables doesn’t provide any size or performance benefits.
(2)Persisting a Component's Data:クラスの属性の初期値を保存する方法.
While ActiveX controls have always been able to persist their data; persistence for ActiveX components is slightly different. A control stores property settings inside it's .cls file, but a component can't do that. Instead, it uses a PropertyBag object that can be saved just about anywhere – in a file, a database, a cell in a spreadsheet, or even in the registry.
(3)Data Type-Allowed in Properties and Methods:後続ノートを参照.
9. Providing Named Constants for Your Component
(1) Although an enumeration must appear in a module that defines a class, it always has global scope in the type library. It is not limited to, or associated in any other way with the class in which you declared it.
(2) General Purpose Enumerations
(3) When you declare a variable using the name of an Enum as the data type, you’re effectively declaring the variable As Long.
(4) Occasionally you may need to provide a string constant, or a constant that isn’t an integer value. Visual Basic doesn’t provide a mechanism for adding such values to your type library as public constants, but you can get a similar effect using a global object with read-only properties.