When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML
Information:
Advertise
Feedback
Author an Article

ASP ASP.NET ASP FAQs Message Board Feedback
 
Print this Page!
Published: Wednesday, October 23, 2002

Displaying Custom Classes in a DataGrid

By Scott Mitchell


A Brief Examination of N-Tier Architecture


If you are developing a large Web application it is advised that you split your application into a number of "layers", where each layer cares only about the layer above and beneath it. For example, in a traditional Web application you may have four total layers:

- continued -

  1. The Web browser, which renders HTML
  2. ASP or ASP.NET Web pages, that produce HTML
  3. Business objects that provide business logic to the ASP/ASP.NET Web pages
  4. A database server from where the business objects obtain their data

In this architecture, each of the four layers is ignorant about the layers that extend beyond its immediate neighbors. That is, the user's Web browser (the first layer) only cares about the HTML that is returned from the Web page it requests. It does not matter to the Web browser how the business objects function, or the database schema. Likewise, the ASP/ASP.NET Web pages (the second layer) care only about producing HTML for the first layer and obtaining data from the business objects in the third layer.

The third layer is the business object layer. Business objects are compiled components that provide business logic to the ASP/ASP.NET Web pages. For example, with ASP pages, a business object may be a COM object written in VB, C++, or Java, that provides data and business logic that the ASP page then uses to render appropriate HTML for the user's browser; with ASP.NET Web pages, the business objects are usually implemented as .NET components (although ASP.NET pages can access traditional COM objects). Business logic is logic specific to the Web application. For example, an eCommerce site might have a set of business objects that had methods like GetInventory(), which returns a collection of items for sale, or GetCustomerInformation(customerID), which retrieves information about a particular customer (identified by some unique customerID).

Typically this architecture is referred to as three-tier, or more generally, n-tier architecture, and is the de facto architecture for Web applications. (For more on n-tier architecture, be sure to read Robert Chartier's Application Architecture: An N-Tier Approach.)

In an n-tiered architecture, the ASP/ASP.NET Web pages should not make direct calls to the database. (That would violate our earlier axiom, that a given layer only communicates with its neighboring layers.) Rather, ASP/ASP.NET Web pages should reference custom objects defined in the business object layer. These objects provide database information in a class structure.

For example, imagine we wanted to store information about users who have signed up on our site. We may have information such as their first and last name, their phone number, and how many times they've visited the site. If we wanted to retrieve information about a particular user, a non n-tier approach would be to have the ASP.NET Web page issue a SQL statement like: SELECT FirstName, LastName, Phone, Visits FROM UserInfoTable WHERE UserID=UserID. An n-tier approach way would, instead, relegate this task to the business object layer. The ASP.NET Web page would then request information from the business object layer, calling some GetUserInfo(UserID) method, which would return a UserInfo class instance corresponding to the particular user. This UserInfo class would have properties like LastName and PhoneNumber, and might have methods like, IncrementVisits().

Assume that you want to create a Web site using n-tiered architecture where the business object layer includes a UserInfo and UserCollection class. You want the UserInfo class to abstractly represents a particular user, meaning it will need to have properties LastName, FirstName, PhoneNumber, and Visits. The UserCollection class abstractly defines a set of users. For example, you may want a business object method called GetAllUsers() that will return a collection that contains all the users, or you may want a method GetUsersWithLastName(strLastName), which returns all users with a particular last name (it returns a collection of users since there may be zero or many users with a particular last name).

In this article we will look at two things:

  1. How to create the UserInfo and UserCollection classes
  2. How to easily and attractively display a UserCollection instance in an ASP.NET Web page

An Example of an N-Tier ASP.NET Application
The ASP.NET Forums are a great example of an n-tiered ASP.NET application. The ASP.NET Forums contain a set of ASP.NET Web pages that access business objects, such as Post, Forum, User, PostCollection, and so on, which abstractly represent a post, a forum, a user, and a collection of posts. The business object layer also contains methods for the ASP.NET pages to retrieve information, such as: GetAllUsers() and GetPost(PostID). If you have questions about the ASP.NET Forums be sure to visit the ASP.NET Forum Discussion.

Creating the UserInfo Class


The first task we are faced with is creating the UserInfo class. As aforementioned, a specific instance of this class defined a specific user, hence it must have properties like LastName, PhoneNumber and so on. We can create such a class as a .NET component by writing a VB.NET or C# class. The code below is a VB.NET implementation of the class, placed in the namespace MyObjects:

Imports System
Namespace MyObjects
    Public Class UserInfo
        'Default constructor
        Public Sub New()
        End Sub
        
        'Additional Constructor
        Public Sub New(fName, lName, pNumber, currentVisits)
          m_firstName = fName
          m_lastName = lName
          m_phoneNumber = pNumber
          m_visits = currentVisits
        End Sub
        

        'Specify private properties
        Dim m_firstName as String
        Dim m_lastName as String
        Dim m_phoneNumber as String
        Dim m_visits as Integer
        
        
        'Specify public methods
        Public Sub IncrementVisits()
          m_visits += 1
        End Sub
        
    
        'Specify public properties
        Public Property FirstName as String
          Get
            Return m_firstName
          End Get
        
          Set
            m_firstname = value
          End Set
        End Property
        
        Public Property LastName as String
          Get
            Return m_LastName
          End Get
        
          Set
            m_LastName = value
          End Set
        End Property

        Public Property PhoneNumber as String
          Get
            Return m_PhoneNumber
          End Get
        
          Set
            m_PhoneNumber = value
          End Set
        End Property

        Public Property Visits as Integer
          Get
            Return m_Visits
          End Get
        
          Set
            If value < 0 then
              m_visits = 0
            Else
              m_Visits = value
            End If
          End Set
        End Property
    End Class
End Namespace

While the above code listing contains a bit of code, it's really quite straightforward code. Two constructors are provided, the default constructor and one that takes four arguments - one for each of the class's private member variables. After the constructors are declared, the four private member variables are declared. After this, a IncrementVisits() method is provided, which simply increments the m_visits member variable. Finally, the class contains a public read/write property for each of the four private member variables.

If you are using Visual Studio .NET, you can compile this code by creating a VB.NET Class project, and then simply building the project. If you don't have Visual Studio .NET you will need to drop to the command line and enter: vbc /t:library filename.vb, where filename.vb is the name of the file that you place the above code into. Both approaches will produce a DLL file.

Now that we've created and compiled the UserInfo class it's time to tackle the UserCollection class, which we'll do in Part 2.

  • Read Part 2!



  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article