|
Introduction
Everything Microsoft seems to revolve around an object model (OM).
Formerly known as a "programming model," there is an ASP object model, an
ADO object model, and there's even an object model for every
application in the Microsoft Office suite. As important and pervasive as
object models are in Microsoft products, many beginning programmers
don't understand or leverage the power afforded by OMs. So this
article will explain why it is important to comprehend the OM paradigm and
how to leverage its strength.
What is an Object Model?
An object model is basically a group of related objects that work in
concert to complete a set of related task(s). For example, the ADO object
model is used to access data. Each object in the ADO object model plays a
specific role in data access (e.g., the Connection object's properties
and methods are used to create a connection to a data source.)
So, what is an object? Well, without going into the great O-O detail
(since that is not the intent of this article), an object is basically a
code-based abstraction of a real-world entity or relationship. Objects
consist of data and a set of behaviors. With Active Server Pages (ASP) we
access an object's data and behaviors through an interface. Objects
are the building blocks of object models. (If that doesn't make sense,
you might want take a quick break from this article to read one of the
numerous 15Seconds articles about programming Visual Basic [VB] objects.)
There are actually two different types of object models. The first, or the
"classic" model, consists of a group objects that are related in a
hierarchical, or "parent-child," fashion. The Microsoft Word object model
is an example of a classic object model. The second, or "logical" model,
consists of a group of objects that are only related logically. The ASP
object model is an example of a logical object model.
Reading an Object Model
The first thing I do when I begin using a Microsoft application is to get a
copy of the object model application programmer's interface (API). I
request a complete object model API, not just a little color-coded picture
with a few boxes representing objects. I want the real deal. I want the
little colored picture, but I also want a list of the properties, methods,
and events associated with each object in the model. Without this
information, I find an object model to be fairly useless. It's kind
of like Superman without his cape. He's still Superman, but he
can't do very much.
Creating An Object Model
Now that we know what object models are and what they're capable of,
let's create our own. In this example, we create a simple contacts
list object using VB 6 that will have its own object model (for the complete VB source, click here.). Let's get
started.
Below is a diagram of the object model we will create:
It's simple, but it'll do the job. As you can see the Contacts
object can contain multiple instances of the Contact object.
Here is the API:
|
Contacts |
|
Properties |
| |
Parameters |
Return Type |
Description |
|
Count |
N/A |
Integer |
Number of contacts in the Contacts collection |
|
Methods |
|
|
|
| |
Parameters |
Return Type |
Description |
|
Add |
conTemp as Contact |
N/A |
Adds a Contact to the Contacts collection. |
|
Save |
sXMLPath as String |
N/A |
Save the Contacts collection in XML format to the
provided path, sXMLPath. |
|
Load |
sXMLPath as String |
N/A |
Loads an XML file containing a list of Contacts from
the provided location, sXMLPath |
| |
|
|
|
|
Contact Object |
|
Properties |
|
|
|
| |
Parameters |
Return Type |
Description |
|
FirstName |
sFirstName as String |
String |
Sets or returns a Contacts FirstName value |
|
LastName |
sLastName as String |
String |
Sets or returns a Contacts LastName value |
|
PhoneNumber |
sPhoneNumber as String |
String |
Sets or returns a Contacts PhoneNumber value |
|
Email |
sEmail as String |
String |
Sets or returns a Contacts Email value |
|
Methods |
|
|
|
| |
Parameters |
Return Type |
Description |
|
GetHTML |
|
String |
Returns XML string representing a Contact |
OK, so let's create our object.
Start by creating a Visual Basic 6.0 Active DLL. Set the Project Name to OMExample and create a
reference to the Microsoft XML, version 2.0 DLL.
Contacts Class:
1. Create a class model named "Contact."
2. Add the following code to the Declarations section of the Contact class
module.
Option Explicit
Private m_sFirstName As String
Private m_sLastName As String
Private m_sPhoneNumber As String
Private m_sEmail As String
This code simply declares several private variables to hold a
contact's properties.
3. Create Property Get and Let statements for each of the properties we
want to expose.
Public Property Get FirstName() As String
FirstName = m_sFirstName
End Property
Public Property Let FirstName(ByVal sFirstName As String)
m_sFirstName = sFirstName
End Property
Public Property Get LastName() As String
LastName = m_sLastName
End Property
Public Property Let LastName(ByVal sLastName As String)
m_sLastName = sLastName
End Property
Public Property Get PhoneNumber() As String
PhoneNumber = m_sPhoneNumber
End Property
Public Property Let PhoneNumber(ByVal sPhoneNumber As String)
m_sPhoneNumber = sPhoneNumber
End Property
Public Property Get Email() As String
Email = m_sEmail
End Property
Public Property Let Email(ByVal sEmail As String)
m_sEmail = sEmail
End Property
4. Create a method that will return an XML string representation of the
contact. This method could have been created as a Friend function, but I
thought a developer might want an XML string for a Contact.
Public Function GetXML() As String
GetXML = "<CONTACT>"
GetXML = GetXML & "<FIRSTNAME>" & m_sFirstName &
"</FIRSTNAME>"
GetXML = GetXML & "<LASTNAME>" & m_sLastName &
"</LASTNAME>"
GetXML = GetXML & "<PHONENUMBER>" & m_sPhoneNumber &
"</PHONENUMBER>"
GetXML = GetXML & "<EMAIL>" & m_sEmail & "</EMAIL>"
GetXML = GetXML & "</CONTACT>"
End Function
5. Now let's create the default value for our Contact properties.
Let's set all of default values to null strings.
Private Sub Class_Initialize()
m_sFirstName = ""
m_sLastName = ""
m_sPhoneNumber = ""
m_sEmail = ""
End Sub
Contacts (collection) Class:
1. Create a class model named "Contacts."
2. Add the following code to the Declarations section of the Contacts class
module.
Option Explicit
Private m_Contacts As Collection
All of this creates a private collection that can contain multiple
instances of the Contact class.
3. Create the read-only Count property. We make this property read-only by
only adding a Property Get function and not a Property Let function.
Public Property Get Count() As Integer
Count = m_Contacts.Count
End Property
4. Create the Add method. The add method will accept a Contact as a
parameter and add it to the private m_Contacts collection.
Public Sub Add(ByVal conTemp As Contact)
m_Contacts.Add conTemp
End Sub
5. Create the Save and GetXML methods. The Save method: (a) calls the
GetXML function which returns an XML string containing all of the contacts
and (b) persists the XML to a file, sXMLPath. Again, I could have declared
the GetXML function as private, but thought I would give developers access
to this functionality.
Public Sub Save(ByVal sXMLPath As String)
Dim oXML As New MSXML.DOMDocument
Dim sXML As String
oXML.loadXML (GetXML)
oXML.Save (sXMLPath)
Set oXML = Nothing
End Sub
Public Function GetXML() As String
Dim conTemp As New Contact
Dim sXML As String
Dim x As Integer
GetXML = "<?xml version=""1.0""?>"
GetXML = sXML & "<CONTACTS>"
For x = 1 To m_Contacts.Count
Set conTemp = m_Contacts.Item(x)
GetXML = GetXML & conTemp.GetXML
Next
GetXML = GetXML & "</CONTACTS>"
Set conTemp = Nothing
End Function
6. Create the Load method. The Load method accepts an XML file path in the
form of a string. The method then opens the XML file and adds a Contact
object to the Contacts objects for each node in the file. I will not go
into detail about the XML processing. It's only important to note that
I am using the MSXML version 2.0 object model to parse the XML.
Public Sub Load(ByVal sXMLPath As String)
Dim oXML As New MSXML.DOMDocument
Dim oRootNode As MSXML.IXMLDOMElement
Dim oContactNode As MSXML.IXMLDOMElement
Dim conTemp As Contact
Dim i As Integer
oXML.async = False
oXML.Load (sXMLPath)
Set oRootNode = oXML.documentElement
For i = 0 To oRootNode.childNodes.length - 1
Set conTemp = New Contact
Set oContactNode = oRootNode.childNodes(i)
With oContactNode
conTemp.FirstName = .childNodes(0).Text
conTemp.LastName = .childNodes(1).Text
conTemp.PhoneNumber = .childNodes(2).Text
conTemp.Email = .childNodes(3).Text
End With
Add conTemp
Set conTemp = Nothing
Next
Set oContactNode = Nothing
Set oRootNode = Nothing
Set oXML = Nothing
End Sub
7. Next we will create the GetHTML function which returns an HTML string.
The function (a) calls the GetXML function which returns an XML string
containing all of the contacts and (b) transforms the XML to HTML using an
XSL style sheet. A sample XSL style sheet, OMexample.xsl, is used for
this example.
Public Function GetHTML(ByVal sXSLPath As String) As String
Dim oXML As New MSXML.DOMDocument
Dim oXSL As New MSXML.DOMDocument
oXML.async = False
oXML.loadXML (GetXML)
oXSL.async = False
oXSL.Load (sXSLPath)
GetHTML = oXML.transformNode(oXSL)
End Function
8. Finally, we add just a little class initialization and termination code.
The Class_Initialize code creates a new collection when the object is first
called. The Class_Terminate code releases the collection's resources
when we are done with the Contacts object.
Private Sub Class_Initialize()
Set m_Contacts = New Collection
End Sub
Private Sub Class_Terminate()
Set m_Contacts = Nothing
End Sub
Now we must compile the object and register it (if it's not going to
be used on the machine on which it is compiled).
The object can be called from our ASP. Below is a fairly simple example of
instantiating the objects and accessing some of their properties and
methods.
<% @LANGUAGE="VBSCRIPT" %>
<% Option Explicit
On Error Resume Next
Dim oContacts, oContact
Set oContacts = Server.CreateObject("OMExample.Contacts")
Set oContact = Server.CreateObject("OMExample.Contact")
With oContact
.FirstName = "Jason"
.LastName = "Butler"
.PhoneNumber = "(xxx)xxx-xxxx"
.Email = "jmbutler@hotmail.com"
End With
oContacts.Add oContact
With oContact
.FirstName = "John"
.LastName = "Smith"
.PhoneNumber = "(xxx)xxx-xxxx"
.Email = "jsmith@yyyy.com"
End With
oContacts.Add oContact
Response.Write(oContacts.GetHTML(Server.MapPath(".") & "\OMexample.xsl"))
Set oContact = Nothing
Set oContacts = Nothing
%>
About the Author
Jason Butler, a graduate of Virginia Tech., is a technical manager for a
"Big 5" consulting firm. He has built numerous Microsoft-centric
Web/E-commerce applications for Fortune 500 and dot-com clients. To contact
Jason with questions or comments, please email him at
jason_m_butler@hotmail.com.
|