Visual Basic Magazine, January 2000
Reposted with Permission of ZD
As we mentioned in last month's article, "Develop
three-tier applications using VB 6.0 and MTS," most developers base
enterprise-wide applications on n-tier architecture. This architecture type
makes maintenance easier, quicker, and less likely to break existing
Despite the benefits however, multi-tier applications do
have one drawback—completion time. A typical programming shop can spend weeks
or months just designing and building the core business components. If your
business is like most, you don't have this luxury. You need to reap the benefits
of an n-tier architecture, but you probably can't afford to let clients wait
around while you experiment. Well, with the help of two tools, the Flexible
Business Object Framework and the Object Builder—shown in Figure A, you
actually can! In fact, in this article, we'll generate a multi-tier business
object in just a few minutes. Then, we'll tie an entire n-tier application
together in less than half an hour! Before we begin, however, let's briefly
discuss object-oriented analysis.
Figure A: The Object Builder lets you create
business objects in no time at all.
The business case scenario
Let's say you work for a company called Acme Antenna
Corporation. Acme sells television, satellite, and infrared antennas to
customers through direct mail. The program management department desperately
needs some sort of system to track their inventory better. The department
manager needs to view all of the antennas in inventory, as well as add and
delete antennas to and from the inventory. Armed with this information, we're
then given free reign to create this application.
Identify business objects and collections
Unlike functional programming, object orientation forces us
to design a system before we code it. This is actually a good thing because it
greatly decreases our chances of ever having to gut and completely redo our
program while in the middle of developing it!
The first design question we need to ask ourselves is,
"What nouns are the users concerned about?" In this case, only one
noun interests us—an antenna. Nouns represent classes (which are usually what
people mean when they say the word objects). VB creates them through
Class Modules. So, at this point, we've actually identified our first business
object! Using Microsoft's blend of Hungarian notation, we'll call this business
If you remember Acme's business requirements, the
company not only wants to add and delete single antennas (which would use our
clsAntenna object), but they also want to view all the antennas
in their inventory. This requirement will necessitate a collection object, which
clsAntennas (plural). It's a good bet that whenever you
identify a single object, you'll almost always need a collection object to go
along with it…at least in VB.
Object properties and methods
Now that we've identified the business objects, we'll need
to determine their properties and methods for each non-collection object in the
project. For our simple example, this is pretty straightforward, since we only
have one object—
clsAntenna. However in most systems, you'll
probably have more objects to contend with.
To determine an object's properties, you'll need to ask
"What adjectives describe this object (
These adjectives will become the object's properties, which you implement with
Visual Basic's Property Let and Property Get keywords. In our case, Acme Antenna
wants to track an antenna's ID, name, and manufacturer. So, we'll make these
three adjectives into
Lastly, to identify the object's methods, we ask "What
verbs can we associate with this object (
become the object's methods, and you implement them with Visual Basic Sub and
Function keywords. Some verbs concerning antennas might be sell or purchase.
However, since Acme didn't specify a concern for these business aspects, we
won't add these methods to the object. On the other hand, we do need to add some
methods to store and restore the object to and from the database. We'll call
these methods Load, Save, and Delete.
At this point, we've completed the analysis. It's a good
idea to write down everything that we came up with so far, because we may need
to refer to it in the project's later stages. Figure B shows the class diagram
we created from the analysis for
clsAntenna. In addition, we added
typical collection properties and methods implemented by
Figure B: From our analysis, we created class
diagram of clsAntenna and clsAntennas.
Now that we have a plan to follow, we can develop the
application's data, business, and user interface tiers. When we finish, we'll
have a fully functional n-tier application.
Install the utilities
Before you continue, you need to install the Flexible
Business Object Framework. In this month's download (ftp.zdjournals.com/ivb/200001.zip),
you'll find the Framework.zip file. When you extract the items in this file,
make sure to maintain the existing folder structure (the Use Folder Names option
in WinZip). Also, don't forget to make a note of the directory into which you
unzip these files. We'll refer to them quite often throughout the rest of this
Once you've extracted the files, find the Object Builder
install program, called ObjectBuilder.exe. You'll find it in the Install
subdirectory. Go ahead and run this program to install the builder. After it
finishes running, you'll be ready to continue.
Implement the database tier
For the database portion of the example, we'll use an
Access database. However, you'll design most n-tier applications to accommodate
a large number of users, which calls for heavy-duty databases, such as SQL
Server, DB2, or Oracle. However, chances are you probably don't have a spare
copy of SQL Server sitting around. Just remember that we don't recommend Access
for designing a high-volume application.
If you don't have Access, we've included a sample MDB
file, called BusinessObjects.mdb, in this month's download in the /Business
Tier/Server Library sub directory. This database contains all the sample tables
you'll need to complete the example.
To begin, launch Access and create a new table, called BusinessObjects.
Here's where the class diagram proves handy. Simply copy the analysis and create
one database field in the table for every property we identified. Figure C shows
the completed table.
Figure C: We created a table to hold the Antenna
Like most n-tier systems, the Flexible Business Object
Framework requires a key field that generates a new unique number for each new
record. To do so in Access, you create an AutoNumber field. Also, you mark the
field as the table's primary key. For those of you more familiar with Access,
SQL Server's IDENT data type serves the same purpose.
At this point, we've completed the database. Make sure
to save it in the subdirectory called /Business Tier/Server Library. Name the
Create the business object tier with our utility
We've finished the lowest level tier, so now we're ready to
implement the business objects. Normally this milestone would be the time to
call your spouse and warn him or her that you'll be working late for the next
few weeks. However, with the Flexible Business Object framework, you'll crank
clsAntenna object in less than five minutes!
To do so, first launch Visual Basic and open the
Master.vbg file, located in the directory into which you installed
Framework.zip. When you do, VB opens a project containing four Active-X DLL
files that make up the framework. In the Project Explorer treeview, find the
Business Object project called clsBusObjectLib10. We'll put our new
clsAntennas business objects into this project.
Next, in the Project Explorer, right-click on the
clsBusObjectLib10 item and select Add | Class Module from the shortcut menu.
Name it clsAntenna. Now, add a second Class module and name it clsAntennas.
Don't forget to watch the spelling. Set the Instancing property of both to Public
Not Creatable. If the phrase Option Explicit appears at the top of these
classes, go ahead and delete it. The Object Builder will add this phrase back
Launch the Object Builder
Next, from Program section of the Windows Start menu,
launch the Object Builder. When you do, the dialog box in Figure A appears.
In the Class Name text box, type the class' name you
want to build. The dialog box provides the cls for you—so just enter Antenna
as shown in Figure A. As you type, the builder automatically fills in the Table
Name and Table Key Column text boxes. These entries need to match the fields in
the database. If you followed the previous instructions for creating the
database, or use the database in the Framework.zip file, you won't need to
change the auto-generated values at all.
Next, click the Browse button next to the Database Name
text box and find the BusinessObjects database that we created earlier. You
should find it in the /Business Tier/Server Library subdirectory.
Now click the Load From Database button. After a brief
pause, click OK when the builder tells you that it loaded the properties from
the database. If you want, you can click on the dialog box' Properties tab to
view the newly loaded properties.
Next, click the Generate button. When you do, the
builder creates the code for the class. Again, after a few seconds a message box
appears stating that it has generated the code. Click OK. Select the Code tab to
see the power of the Object Builder with your own eyes. Four rich-text boxes
contain all of the code required to implement
And the best part is that you've created them in a fraction of the time it would
Drop the code into the project
Now that we've got the code, it's just a matter of copying
and pasting it into the VB project. First, copy the code under Class Code and
paste it into
clsAntenna. Next, copy the code under Collection Code
and copy –and paste it into
The code under Place Code in
goes at the bottom of
clsBusObjectServer. This file is located in
the same project as
clsAntenna. Make sure to just append it to the
end of the existing code. You don't want to delete anything that's already
Next, the code under
at the end of
clsFactoryServer, which you'll find in the
Project. Again, make sure to append the code to the end of the class's existing
As our last step, we have to set up the collection class to
For Each...Next enumeration. To do so, in the
Project Explorer, double-click on
clsAntennas to bring up the class
module. Select Tools | Procedure Attributes from the menu bar. Click the
Advanced button. When you do, Visual Basic displays the dialog box shown in
Figure D. In the Name field, select NewEnum from the dropdown list. Then,
in the Procedure ID field, enter –4, and click OK. Congratulations,
you've just completed the business objects!
Figure D: The NewEnum attribute allows the For
Each…Next enumeration in a collection.
Implement the User Interface tier
All we have left to do is add the user-interface tier. In
the next article of this series, we'll show you how to create a custom GUI for
these objects from start to finish. For now, let's just add a simple pre-created
GUI that will exercise the business objects we just created.
To do so, Select File | Add Project from the menu bar.
Click on the Existing tab and add the project located in the subdirectory
/Client Tier/Client directory from the Framework.zip. Add the Client.vbp
project. Next, we'll set this project as the Startup project. To do so,
right-click on it and select Set As Start Up from the shortcut menu. If you
haven't saved the program yet, do so now.
Press [F5] to run the project, and you're off to the
races. You've just created your first n-tier application! Go ahead and put the
app through its paces to make sure it can add, delete, and display all of Acme's
Multi-tier systems offer incredible advantages, but they
normally require a multi-week to multi-month investment in time and effort just
to get started. With the Flexible Business Object Framework and the Object
Builder presented in this article, even a complete n-tier novice can tap the
power of multi-tier applications in just minutes.