|
|
The Key Technology for ASP componentsWindows Script Components (WSC) is the technology that makes it possible to create COM objects using a scripting language. An ASP component is nothing more than a WSC component that implements the interfaces to access ASP objects. To understand how to arrange ASP components, it's necessary to take a closer look at how WSC files are defined and how they work under the hood. A WSC is a XML file that follows a precise syntax. The XML code is the glue that puts together at least two different types of information: the description of the component and its code. The description of a COM component includes the progid and/or the CLSID, a descriptive string, a version number, plus its programming interface with the list of methods, properties and possibly events. To specify all this information you use two tags: <registration> and <public>. The former is meant to provide all the data to register and identify the component. The latter enumerates the methods and properties. It's by using an interface like this that you drive the behavior of the component and enable it to access ASP objects. Consequently, all ASP objects have a <public> section. The World's Simplest WSCThe following listing shows a very simple WSC component. It exposes a single method called Welcome(). The method is declared in the <public> section and the body is a successive <script> block. The <registration> content is minimal but what shown next is what you really need to provide: a progid string. <component> <registration progid="HelloWorld.WSC"/> <public> <method name="Welcome"/> </public> <script language="VBScript"> Function Welcome MsgBox "Hello, World" End Function </script> </component> This component is invoked as shown below: Dim wsc Set wsc = CreateObject("HelloWorld.WSC") wsc.Welcome A client application sees a WSC as it is a regular COM object, and actually a WSC component is a regular COM object. However, there are a few objections to answer I think: Ø A COM object is a binary file Ø A COM object has a precise binary layout Ø A COM object exposes interfaces Ø How can a client module distinguish between a binary and a script COM object? Ø Does this violate the purity of COM? To all these questions (and more) there's just one answer. The script code you write through a WSC file is just code that gets interpreted by a binary module that exposes all the needed interfaces to callers. All the WSCs refer to the same binary file (scrobj.dll). The link between the progid and the script file is kept in an additional registry key called ScriptletURL. There's only one moment in which a WSC is seen as a different entity from, say, a C++ COM object: when the regsvr32.exe program registers it. This program if detects it's processing a WSC file it adds the ScriptletURL key in the proper registry subtree. This is the only difference. A Layered ComponentA WSC component can be seen following a 3-tiered schema: description, script code and the underlyling machinery that lets the callers issue calls to the object. The run-time engine implements natively a fixed number of interfaces that delimit what you can do, and what you can't do through WSC's. Through the use of WSC's we are able to use: Ø Automation Ø ASP Ø IE5 Behaviors To expose an automation interface, just add a <public> tag and define the prototype you want for your methods, properties and events. After all, the <public> tag is a script-level wrapper for IDispatch and the outgoing interfaces for the called to sink events. To access the ASP objects such as Session, Request and Response you need to require a special support. Obtain it through the <implements> tag: <implements type="ASP" /> This tag complements <public> and they are not exclusive. An ASP component, in fact, must expose an externally callable set of functions. Furthermore, to make sure that the code of these functions can manage ASP objects and update the calling page, it must add an <implements> tag too. To complete the discussion, an object that needs to work as an IE5 behavior and modify the run-time behavior of HTML tags must insert <implements type="Behavior" /> In general, a behavior doesn’t need an automation interface (that is a <public> section) because it is called by Internet Explorer only and just through the Behavior-specific interfaces. However, sometimes a behavior also needs an automation interface. Why ASP Components?A real-world Internet project often grows in size over (a short) time up to several dozens of pages. It's clear that you simply can't keep up maintaining pages singularly. For one thing, you need an integrated development environment. Secondly, you should start reusing code across pages. One evergreen technique is based on server-side includes (SSI) (files that you include in your ASP pages making them functioning as if the code had been written at the top of the current page) For example: <!-- #include file="c:\scripts\myLib.vbs" --> <!-- #include virtual="/scripts/myLib.vbs" --> You're telling the ASP parser to look also those files up for locating the functions invoked throughout the page. The module with the external functions is separated from the page that's using it, so it doesn’t pose any particular maintenance problem in case of bugs or just improvements. What you're importing, though, is just text. It could be pure text or HTML as well as VBScript or JScript functions enclosed in <% … %> blocks. In addition, SSI's get processed before the ASP parser looks at the page so there's no way to control them programmatically. ASP components are a more modern and powerful way to make server-side includes in ASP pages. With ASP components not only do you solve the problem of writing reusable and object-based script code, but you also get real (even if not binary) COM objects that can be used in any other COM-aware environments such as the Windows Script Host. The Power of ReusabilityThe most compelling use of ASP components is to isolate relatively small and frequently used pieces of code to be used across pages. For example, if you have several pages that process forms you might want to write a unique component, give it a pretty general prototype and use it wherever possible. Input validation, XML strings processing, database access, complex formatting procedures are all good candidates to become ASP components. Also any text that you use repeatedly in your ASP pages can be wrapped in a component's method. Don’t forget that the ultimate goal of an ASP component is to output text through the Response object. Thus, any text used with a high frequency is automatically a candidate to become an ASP component. On the other hand, don’t let it get out of control - you don't want to have a new component for each line of code! Forget about C++ and Visual Basic?Today if you want to write ASP components you should rely on C++ or Visual Basic. While Visual Basic is not that much different from VBScript and provides a far better performance, you still have to learn a brand new tool and a slightly different language. With C++ things are even worse, since C++ is a language that requires a solid and specific knowledge. It's true that Visual Studio 98 provides a great facility to write ASP components, but you need to know the ATL library in addition to the language. Script-based ASP components definitely represent a solution for better ASP programming. An Example of Script-based ASP ComponentThe component is capable of retrieving data from a query and return the records formatted in two possible ways: as a HTML table of records or as a treeview. The files are:
The query uses the Northwind Access database through a DSN of NW. |
|
|
|
|
|
| |
Information Online swimming pool contractor chicago web site design help desk Domain Names help desk services Dolce&gabbana sunglasses answering service |
Email TopXML
|
|
Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP |