Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using VBScript Classes in ASP Applications - example of job posting site

0 views
Skip to first unread message

Computer Staff

unread,
Jun 29, 2000, 3:00:00 AM6/29/00
to
Using VBScript Classes in ASP Applications
Most ASP applications can be improved by incorporating COM objects and
object-oriented programming design principles. This improvement comes in the
form of code reuse and in making the application easier to modify and extend
as business needs change. Normally I develop COM objects in Visual Basic
because the code is compiled, variables are strongly typed, and performance
is greatly improved over VBScript. However, there are times when it is
impractical or even impossible to incorporate compiled COM objects in your
applications. This could be for any of the following reasons:

Limitations imposed by the development or deployment environment. If you are
developing an ASP application that will be deployed on an ISP where you are
not permitted to register DLLs, you don't really have a way to incorporate
COM objects in your code. This can be frustrating if you are used to
developing a good deal of your code in Visual Basic or C++.

Prototyping or Development Stage. Many times when you are developing an ASP
application, you need to prototype an application before you develop the
final 'deployable' version, possibly for a review by your boss or the
client. In this stage of development, it is often not practical to develop
an application that contains a good deal of compiled objects. Also, when you
are developing the architecture and flow of the application, it can be much
easier to rapidly write the code using script instead of with a programming
language such as VB.
Lack of experience with VB or OO Programming Principles. Programming an ASP
application using VB COM objects can be an extremely daunting task if you
have previously programmed exclusively using scripting languages and as a
result are unfamiliar with VB's language and IDE. Trying to incorporate
Object-oriented design principles into the architecture of your ASP
Application can exacerbate this.

Today, I am going to outline a solution to just these situations. However,
since one of the common reasons for failing to use COM objects in ASP
applications is a lack of familiarity with object-oriented design, we will
also look very briefly at some of the benefits of utilizing object-oriented
principles. We will also look at some of the types of objects that are
commonly used in ASP applications. I will end the article by examining a
sample application that I have developed that demonstrates many of the
principles discussed here.

Even when it is impractical to develop your program using compiled COM
objects, there is a way to incorporate OO design principles and objects in
your ASP applications. You can achieve this by using classes created in
VBScript, a new feature in VBScript 5.0. VBScript 5.0 ships with IE 5 and is
downloadable from Microsoft. While classes created in VBScript do not
perform nearly as well as classes developed in VB, they do allow you to take
advantage of many of the benefits of Object Oriented or Object based
programming. They also give you the benefits of rapid development using a
non-compiled scripting language which can speed prototyping and application
planning, you can develop classes using your favorite text editor instead of
having to learn a programming language's IDE, and best of all, if you do not
have the ability to register DLLs on the deployment server, you can simply
upload the VBScript class and there are no registration issues to worry
about. Also, when the time comes to convert the VBScript classes to VB COM
objects, the process consists of doing little more than cutting and pasting
the VBScript code into a VB DLL and changing the variable types in the
variable declarations.

Disclaimer - (Object Oriented Programming is a topic too complex to even
begin to outline with any degree of detail in this column. I use the term
Object Oriented Programming in an extremely non-purist fashion. Object
Oriented programming is normally very different in the context of ASP
applications than it is, for example, in C++ programming. The biggest impact
of this can be seen with the function that objects or components written in
script perform. An example of a design that would be very inefficient
modeled in a scripting environment would be a parent object containing a
collection class encapsulating a collection of child objects. While OO
Programming deals with modeling the physical domain to some degree or
another with classes, i.e. parent classes containing collections of child
classes, classes written in script are more effective if they are written
from a functionality standpoint, i.e. classes that center around
accomplishing a certain process. This leads to an object model that does not
always perfectly model the physical domain).

Benefits of OO Design Principles
There are many reasons why ASP applications can benefit from incorporating
object-oriented principles. While the purpose of this article is to
demonstrate how VBScript Classes can allow a developer to take advantage of
these benefits even when it is impractical, for whatever reason, to use
compiled classes in an application, this is not intended to be a detailed
discussion of what you gain by incorporating object-oriented principles in
your development. Let's briefly give an overview of some of the benefits
that OO design can bring to your ASP application because these can be
realized whether you implement classes using VB COM objects or VBScript.

Classes Encapsulate Data and Related Methods
Well-designed classes give you the greatest control over your application's
data. By data, I mean the variables that your program manipulates. You are
able to package and control the use of this data in classes. One obvious
benefit of the control brought by encapsulating an object's data is the
improved ease of debugging.

Scalability of Code
By encapsulating an application's code in self-contained classes, the
deployment of this code can be changed as more demands are placed on the
application. For example, an ASP application that uses VBScript classes to
contain the majority of the program's logic could easily be moved to VB COM
classes and MTS to support more users.

Reusability of Code
Possibly the best reason to incorporate OO design principles into your ASP
application is the reuse of common code across different pages and even
different applications. Object Oriented languages offer inheritance as a
method of accomplishing code reuse. While VBScript does not support either
interface or implementation inheritance, code reuse is achieved with the ASP
Include statement. This allows a single class to be shared unedited across
multiple asp pages.

Types of Classes
There are three general types of classes of which a typical application can
take advantage. These are very broad categories, but I have found that most
of the classes that I have developed fall into one of these groups.

Utility Classes
Utility classes provide a specific set of functionality. Normally, they
encapsulate some highly used routines that are used over and over again in
your code. The obvious benefit to utilizing utility classes is that this
much-used code is contained within one unit, namely a class, and you can
access it in the same way over and over again. An example of this is a class
that I have used in many ASP applications when, for any number of reasons, I
wasn't able to use VB COM objects. It is the clsDALayer.asp file. This class
exposes three functions, RetrieveRS, ExecuteQuery, and SaveRS. These
functions encapsulate 99% of the data access code that I normally use in ASP
projects. Using this class permits me to write the ADO code once and use it
hundreds of times.

From a design standpoint, it is definitely better to include common
functionality in a utility class. For example, don't put all the routines
common to your application in one class. Instead, separate them into groups
of functions that are related. Some examples of utility classes would be:

Data Access classes
Classes that contain custom String Parsing routines
Classes that manipulate Cookies
Business Classes
Business classes, or business objects, represent real-world business
entities. They generally contain the data that is owned by that business
entity, as well as methods to manipulate that data. One common way of
creating business objects is encapsulating a row from the related database
table within the class. In the included sample application, there are three
business objects, the Contact, Job and Location class. Each contains
Property Get and Let procedures exposing data contained by the object as
well as a Load method that loads the object's data from the database.

Collection Classes
These classes contain a collection of a specific type of class. For example,
a Persons collection class would contain a collection of Person objects. I
am not going to go into depth regarding the use of collection classes
because given the uncompiled nature of VBScript classes, using collection
classes has the potential to become very expensive in terms of server
resources.

Using VBScript Classes
Let's look briefly at how to create, design and use classes in VBScript.
Classes in Visual Basic are stand-alone .cls files that are normally part of
an ActiveX DLL or EXE project. In VBScript, classes can be defined in their
own files or as part of a larger script. But in order to be used across
multiple ASP pages, each class should be created in its own file. As with VB
COM objects, VBScript classes should never be given Session or Application
scope. In fact, if you attempt to assign a reference to a VBScript class in
the Session or Application object in IIS 5, an error will occur.

The syntax for a VBScript classes is relatively simple. To create a class,
you must use the following template:

Class ClassName
'Define Properties
'and Methods Here
End Class

Everything contained between the Class and End Class statements make up the
class. This can consist of both public and private methods and properties.
Similar to VB, if a method is not explicitly declared as private, its
definition will be public. The property statements are also like property
statements in VB, Property Let/Set procedures assign a value to the property
while Property Get procedures retrieve the value of the property.

There are two events that can be captured by event procedures in a VBScript
class, the Class_Initialize and the Class_Terminate events. Just as in VB,
the code written in these event procedures will be executed as soon as the
class is created and as soon as the class is set to nothing. Here is how the
Class_Initialize event is implemented.

Private Sub Class_Initialize()
'your code here
End Sub

After being defined, VBScript classes are instantiated by using the NEW
keyword like this:

Set objVariable = NEW ClassName

Finally, to effectively implement VBScript classes in an ASP application, it
is best to define each class in a separate file. These files can then be
included on an as-needed basis in ASP pages using include files.

<!--#include file="clsContact.asp"-->

For a competent VBScript programmer, classes are relatively simple to
develop and implement in ASP applications.

The Job Post Application
Description
The sample application that I have included to demonstrate the use of
VBScript classes is a job posting site, something that most programmers are
all too familiar with. It allows individuals to search for jobs that match
their custom search criteria, as seen in the screenshot below:

The application consists of three main ASP pages, jobsearch.asp which allows
a user to build a search, joblist.asp that displays all jobs that match the
search, and jobdetail.asp that displays the details of a specific job. These
ASP pages are little more than HTML templates in which VBScript classes have
been included to provide the data and processing necessary for the page's
dynamic elements to be displayed correctly. The include statements come
towards the top of the script like this:

<%@ LANGUAGE="VBSCRIPT"%>
<%
Option Explicit
%>
<!--#include file="includes/clsDALayer.asp"-->
<!--#include file="includes/clsJob.asp"-->

The classes that are included are then created before any ASP code is
written. Then, their functions and methods are called, eliminating most of
the script that is embedded in each page.

<%
Dim objJP
Set objJP = New Job
Call objJP.Load(Request.QueryString("job"))
%>

Object Model
The object model of the Job Post application is relatively simple. Let's
examine the different types of classes that are used here.

Utility Classes
There are three utility classes incorporated into this project, two that are
specific to this project. Class Lookup exposes functions that write lookup
tables from the database as <SELECT> list options, and Class Search, which
constructs the SQL query used to search for jobs. The third, Class DALayer,
is a generic utility class that encapsulates the ADO data access code that I
use the most frequently in my ASP applications.

Let's look at how Class Search is constructed. It begins with the normal
class declaration:

Class GetLookups

This is followed by the function declarations:

Private Sub Class_Initialize()
Set gobjData = New DALayer
gobjData.ConnectionString = TRIM(Application("ConnectionString"))
End Sub
Private Sub Class_Terminate()
If IsObject(gobjData) Then Set gobjData = Nothing
End Sub
Public Sub WriteLocation()
'Implement Code to write SELECT lists
'of Lookup Values from the Database
End Sub

As you can see, this is a fairly simple class that only contains one method
as well as the Initialize and Terminate event handlers. Its only purpose is
to write out a SELECT list of lookup values from the database. It is an
example of the type of code that is a perfect candidate for becoming
encapsulated into a class since in a typical application you would have to
create this same SELECT list of lookup items on several pages. Using this
class, I am able to write the code in one file and call it throughout the
application like this, assuming the file containing the class has already
been included in the ASP page:

<select name="location" size="7" multiple>
<%
Dim objJP
Set objJP = New GetLookups
objJP.WriteLocation
%>
</select>

Business Classes
This application also contains three business objects, which are classes
that use Property Get/Let procedures to expose one record from the related
table in the database. For example, you would write the following code for
each database field:

Public Property Get Rate 'Substitute with the name of the actual field
Rate = gstrRate
End Property
Public Property Let Rate(strRate)
gstrRate = strRate
End Property

These routines allow you to store the value of the field, in this case Rate,
in the private variable gstrRate. The Property Let procedure updates this
variable while Property Get retrieves the current value of gstrRate. Using
Property procedures allows you to expose the data associated with one
business object in an easy to use format.

The business objects are related in a hierarchical structure with the Job
class as the topmost object and it contains an instance of both the Location
and Contact classes, which are both accessible through property get
procedures. This allows a user to create the Job class and then have access
to everything contained by that job, including its location and detailed
contact info.

How is the initial data loaded into the private variables that are exposed
by the property procedures? Each business object contains a Load method that
normally accepts a parameter of the ID column of the database row that it
should load. This load method uses the DALayer class to connect to the
database and retrieve the object's data. Then, it loads up the objects
variables with this data. Now the object is ready to use. Although I am just
using these business objects to expose a record from the database, normally
you would add a Save or ApplyEdit method to the class so that you could edit
its data through the property procedures and then save the updated data back
to the database.

Conclusion
If you utilize object-oriented design principles in your ASP applications,
chances are good that you use COM objects developed in Visual Basic or C++.
However there will inevitably be times when it is impractical or even
impossible to implement COM objects due to available development time or
environment restrictions. In these situations, VBScript classes offer a good
compromise between COM objects and straight VBScript code. While not always
appropriate for a high-performance environment, classes created in VBScript
offer many of the advantages of object-oriented design while never leaving
your ASP development environment.


Larry Bergstrom
Computer Staff, LLC
1701 W. Northwest Highway
Grapevine, Texas 76051-8105
EMAIL LB...@Flash.Net (prefer Word97 or RTF attachments)
OFFICE PHONE metro 817-329-5009
FAX metro 817-329-5091
HOME PHONE: 817-251-2029
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Information Technology Contract & Permanent Placement Services
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 new messages