Displaying Custom Classes in a DataGridBy 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:
- The Web browser, which renders HTML
- ASP or ASP.NET Web pages, that produce HTML
- Business objects that provide business logic to the ASP/ASP.NET Web pages
- 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
method, which would return a
UserInfo class instance corresponding to the particular user.
UserInfo class would have properties like
and might have methods like,
Assume that you want to create a Web site using n-tiered architecture where the business object layer
UserCollection class. You want the
UserInfo class to abstractly represents a particular user, meaning it will need to have
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
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:
- How to create the
- How to easily and attractively display a
UserCollectioninstance 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 |
The first task we are faced with is creating the
UserInfoclass. As aforementioned, a specific instance of this class defined a specific user, hence it must have properties like
PhoneNumberand 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
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.