asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Object Models 101
By Jason Butler
Rating: 3.7 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    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.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Aug 7, 2002 - Using MySQL in the Win32 Environment
    Developers who don't want to spend a lot of money on SQL Server and who want a database that's more robust than Access may find MySQL to be a pleasant alternative. This introductory article covers the bare essentials for getting MySQL installed and running in the Win32 environment.
    [Read This Article]  [Top]
    Jul 17, 2002 - Software Development: Steps To Better Ensure Success
    There is never a guarantee of project success when endeavoring to build a sophisticated application. However, there are established steps to follow that will ensure a clear, concise scope, support for the team involved, and a solid opportunity for successful deployment.
    [Read This Article]  [Top]
    Jul 15, 2002 - Securing SQL Server for Web Applications
    If your SQL Server is exposed to the Internet, then hackers are probing it. This article shows how to secure a SQL Server database that's being used with a Web application
    [Read This Article]  [Top]
    Jul 1, 2002 - Protecting Your Web Application Against Dangerous Requests
    Enrico Di Cesare provides a solution for hiding and securing querystring values that pass through a url.
    [Read This Article]  [Top]
    Apr 2, 2002 - Object-Oriented Programming for VBScripters
    Feel intimidated by .NET? This article by Rob Chartier is designed to ease any level VBScripter (ASP) into .NET by clarifying some OOP concepts.
    [Read This Article]  [Top]
    Mar 27, 2002 - A Best Practice for Using ADO Objects
    A few members of the 15 Seconds discussion list talk about the proper way to use methods in order to prevent ADO object errors.
    [Read This Article]  [Top]
    Jan 2, 2002 - The ASP.NET Page Life Cycle
    Solomon Shaffer explores the life cycle of an ASP.NET page from initialization to unloading. He also explains the various methods to override ASP.NET server-side events.
    [Read This Article]  [Top]
    Dec 19, 2001 - Application Architecture: An N-Tier Approach - Part 2
    Rob Chartier creates a simple portable and reusable address book in .NET to demonstrate the power of N-tier application architecture. Complete source code included!
    [Read This Article]  [Top]
    Oct 23, 2001 - Application Architecture: An N-Tier Approach - Part 1
    Learn about N-tier application architecture and realize that developing with multiple layers produces a flexible and reusable application for distribution to any number of client interfaces.
    [Read This Article]  [Top]
    Oct 23, 2001 - Application Architecture: An N-Tier Approach - Part 1
    Learn about N-tier application architecture and realize that developing with multiple layers produces a flexible and reusable application for distribution to any number of client interfaces.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs