 |
Search
IT Courses |
|
|
|
|
|
|
|
|
|
|
|
IT
dart News Letter
|
Get
ITdart.com weekly headlines before it's published on our site! Subscribe
and receive the articles delivered to your inbox!
|
|
|
Creating a COM component in VFP
VFP, VB, VC, PowerBuilder and Delphi are some of the commonly used front-ends tools today. The trend nowadays is to create the business logic components separately from the visual interface. This provides higher maintainability and allows for changing business needs without having to recompile full applications very often. In addition, the business layer is typically used as a common validation by all the applications. These applications may have been written in different languages. The only common factor required is that the development tool must support OLE Automation.
As VFP is a highly efficient and powerful data handling engine, here is an illustration of how a business component can be created using VFP and then invoked from any other OLE compliant language or tool.
Example: For this purpose, we will consider an example where the business validation is fairly simpleÑchecking whether the given value is within a specified range. The range could have been stored in another database or the validation could have been more complex. However for illustration purposes the method of creating business object remains similar.
Steps involved:
Defining the contents of the business object:
We will create a class called validations and flag it as a COM class.
* myclass.prg define class validations as custom olepublic function isvalidage lparameters m.age * Checks whether the given value * is a valid number for age. * The range is 1 to 110 if between(m.age, 1, 110) return .t. else return .f. endif endfunc enddefine That's all there is to it.
Create a VFP project with the required class library included:
Now create a new VFP project and add this class (myclass.prg) file to the project. The olepublic argument specifies that this class must be converted to a COM object. The project manager understands this and provides options for either converting this project to a standard VFP EXE or APP, or converting it to an OLE EXE or DLL. Save the project as FOXOBJ. Now choose the Build option and Build OLE DLL option.
Provide information about the usage of the COM component to the project:
Now choose Project Info from the Project pull down menu and choose the Servers tab. Here ensure that the validations server class appears. Type an English title for the server component in the Typelib Description field. Ensure that the instancing is Multi Use.
Compile the project and create the COM component:
Now build the OLE DLL again. This process creates an OLE DLL and FOXOBJ.DLL. It also creates some other files. Here's what each file does:
FOXCOM.TLB is the type library used by development tools for early binding, and providing information about the objects, properties and methods within the DLL. This file is used to provide Intellisense popups in VB and VC.
FOXCOM.VBR is a registration file for registering this component on a remote computer. This file contains registry entries. If you want to run the component from the server but want to invoke it from a remote LAN client, you must copy this VBR file on the client machine and perform client registration using the CLIREG utility.
Test the component from within VFP:
You can invoke the component as a native VFP class as follows:
set classlib to myclass x = createobject('validations') ?x.isvalidage(10) .t. ?x.isvalidage (333) .f. Here we have not used the COM component at all. To use the COM component we just built you will need to use the following syntax:
x =createobject('foxcom.validations') ?x.isvalidage(10) .t. ?x.isvalidage(333) .f. Invoke the component from another tool, (Word in this case):
The best part of creating components in this manner is that these can now be invoked from practically any development tool which supports COM. To illustrate this point, we will invoke this component from Word, which uses VBA as a development tool.
Follow these steps:
Start Word (Office 97 onlyÐthis will not work in earlier versions.) Choose Tools - Macros - Visual Basic Editor From the Insert menu choose Module. From the View menu choose View Code. Choose References from the Project menu. A list of all COM components available is shown here. Choose the component we have created. Now type the following code: sub comdemo() Dim obj1 As New foxcom.validations Debug.Print obj1.isvalidage(10) Debug.Print obj1.isvalidage(333) end sub You will notice that the Intellisense feature works while coding on account of the information stored in the type library.
Discuss remote automation:
In order that this object can be created from another machine, you need to run the CLIREG32.EXE file on that computer with the FOXCOM.VBR file. This will ask you to provide details of the network protocol and the server address. Generally, you will use the TCP/IP protocol and provide the IP address of the server. Once this is completed, a remote computer can use the same syntax as above to create the object on a central server and utilise the properties and methods.
As you can see, using this method, the business validations are now being performed centrally rather than on the client. If all the applications requiring the same validations refer to the same server object, maintenance is very simple. Changing the object on the server will automatically update all the applications using it without you having to recompile the applications at all.
|
|
|