Create your first n-tier app in under 30 minutes! : Part I
Creating your app with an N-tier architecture gives you alot of advantages, like easier maintenance, and better code-reuse. However, many times the concepts behind creating such an app, deter people from ever learning. This tutorial teaches you the fundamentals of this architecture, and by the time you're done, you will have your own working n-tier app in under 30 minutes!
AI
Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Zdrojový kód
</font></b><font face="Verdana" size="2">From <a href="http://www.zdjournals.com/ivb/0003/ivb0031.htm">Inside Visual Basic Magazine</a>, </font></font><font size="2" face="Verdana">January </font><font face="verdana,arial"><font face="Verdana"><font size="2">2000</font><br> </font><font face="Verdana" size="2">Reposted with Permission of <a href="http://www.zdjournals.com/">ZD Net Journals</a><br> <br> <table align="right" border="0"> <tbody> <tr> <td></td> </tr> </tbody> </table> <p><font face="Verdana">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 functionality.</font> <p><font face="Verdana">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.</font> <p><font face="Verdana"><b>Figure A:</b> The Object Builder lets you create business objects in no time at all.<br> <img alt="[ Figure A ]" border="0" height="375" src="/vb/tutorial/vb/images/NTier1_1.gif" width="470"></font> <h3><font face="Verdana">The business case scenario</font></h3> <font face="Verdana">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.</font> <p> <h3><font face="Verdana">Identify business objects and collections</font></h3> <font face="Verdana">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!</font> <p><font face="Verdana">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 <i>objects</i>). 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 object class <code>clsAntenna</code>.</font> <p><font face="Verdana">If you remember Acme's business requirements, the company not only wants to add and delete single antennas (which would use our new <code>clsAntenna</code> object), but they also want to view all the antennas in their inventory. This requirement will necessitate a collection object, which we'll call <code>clsAntennas</code> (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.</font> <p> <h3><font face="Verdana">Object properties and methods</font></h3> <font face="Verdana">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—<code>clsAntenna</code>. However in most systems, you'll probably have more objects to contend with.</font> <p><font face="Verdana">To determine an object's properties, you'll need to ask "What adjectives describe this object (<code>clsAntenna</code>)?" 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 <code>clsAntenna</code>'s properties.</font> <p> <h3><font face="Verdana">Identify methods</font></h3> <font face="Verdana">Lastly, to identify the object's methods, we ask "What verbs can we associate with this object (<code>clsAntenna</code>)?" Verbs become the object's methods, and you implement them with Visual Basic Sub and Function keywords. Some verbs concerning antennas might be <i>sell</i> or <i>purchase</i>. 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 <i>Load</i>, <i>Save</i>, and <i>Delete</i>.</font> <p><font face="Verdana">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 <code>clsAntenna</code>. In addition, we added typical collection properties and methods implemented by <code>clsAntennas</code>.</font> <p><font face="Verdana"><b>Figure B:</b> From our analysis, we created class diagram of clsAntenna and clsAntennas.<br> <img alt="[ Figure B ]" border="0" height="200" src="/vb/tutorial/vb/images/NTier1_2.gif" width="391"></font> <p><font face="Verdana">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.</font> <p> <h3><font face="Verdana">Install the utilities</font></h3> <font face="Verdana">Before you continue, you need to install the Flexible Business Object Framework. In this month's download (<a href="ftp://ftp.zdjournals.com/ivb/200001.zip">ftp.zdjournals.com/ivb/200001.zip</a>), 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 article.</font> <p><font face="Verdana">Once you've extracted the files, find the Object Builder install program, called <i>ObjectBuilder.exe</i>. 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.</font> <p> <h3><font face="Verdana">Implement the database tier</font></h3> <font face="Verdana">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.</font> <p><font face="Verdana">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.</font> <p><font face="Verdana">To begin, launch Access and create a new table, called <i>BusinessObjects</i>. 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.</font> <p><font face="Verdana"><b>Figure C:</b> We created a table to hold the Antenna data.<br> <img alt="[ Figure C ]" border="0" height="296" src="/vb/tutorial/vb/images/NTier1_3.gif" width="470"></font> <p><font face="Verdana">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.</font> <p><font face="Verdana">At this point, we've completed the database. Make sure to save it in the subdirectory called /Business Tier/Server Library. Name the file <i>BusinessObjects.mdb</i>.</font> <p> <h3><font face="Verdana">Create the business object tier with our utility</font></h3> <font face="Verdana">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 out a <code>clsAntenna </code>object in less than five minutes!</font> <p><font face="Verdana">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 <code>clsAntenna</code> and <code>clsAntennas</code> business objects into this project.</font> <p><font face="Verdana">Next, in the Project Explorer, right-click on the clsBusObjectLib10 item and select Add | Class Module from the shortcut menu. Name it <i>clsAntenna</i>. Now, add a second Class module and name it <i>clsAntennas</i>. Don't forget to watch the spelling. Set the Instancing property of both to <i>Public Not Creatable</i>. 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 later.</font> <p> <h3><font face="Verdana">Launch the Object Builder</font></h3> <font face="Verdana">Next, from Program section of the Windows Start menu, launch the Object Builder. When you do, the dialog box in Figure A appears.</font> <p><font face="Verdana">In the Class Name text box, type the class' name you want to build. The dialog box provides the <i>cls</i> for you—so just enter <i>Antenna</i> 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.</font> <p><font face="Verdana">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.</font> <p><font face="Verdana">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.</font> <p><font face="Verdana">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 <code>clsAntenna</code> and <code>clsAntennas</code>. And the best part is that you've created them in a fraction of the time it would normally take!</font> <p> <h3><font face="Verdana">Drop the code into the project</font></h3> <font face="Verdana">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 <code>clsAntenna</code>. Next, copy the code under Collection Code and copy –and paste it into <code>clsAntennas</code>.</font> <p><font face="Verdana">The code under Place Code in <code>clsBusObjectServer</code> goes at the bottom of <code>clsBusObjectServer</code>. This file is located in the same project as <code>clsAntenna</code>. Make sure to just append it to the end of the existing code. You don't want to delete anything that's already there!</font> <p><font face="Verdana">Next, the code under <code>clsFactoryServer</code> goes at the end of <code>clsFactoryServer</code>, which you'll find in the <code>clsFactoryLib10</code> Project. Again, make sure to append the code to the end of the class's existing code.</font> <p> <h3><font face="Verdana">Enable enumeration</font></h3> <font face="Verdana">As our last step, we have to set up the collection class to properly handle <code>For Each...Next</code> enumeration. To do so, in the Project Explorer, double-click on <code>clsAntennas</code> 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 <i>NewEnum</i> from the dropdown list. Then, in the Procedure ID field, enter <i>–4</i>, and click OK. Congratulations, you've just completed the business objects!</font> <p><font face="Verdana"><b>Figure D:</b> The NewEnum attribute allows the For Each…Next enumeration in a collection.<br> <img alt="[ Figure D ]" border="0" height="404" src="/vb/tutorial/vb/images/NTier1_4.gif" width="338"></font> <h3><font face="Verdana">Implement the User Interface tier</font></h3> <font face="Verdana">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.</font> <p><font face="Verdana">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.</font> <p><font face="Verdana">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 towers.</font> <p> <h3><font face="Verdana">Conclusion</font></h3> <font face="Verdana">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.</font>
Původní komentáře (3)
Obnoveno z Wayback Machine