Working with Reflection and Attributes


In this chapter, you're going to work on two separate tasks. The first will teach you how to load a DLL dynamically based on a setting in your configuration file. The second will teach you how to use attributes in an application. Both tasks use the same sample application.

The sample application features an order-tracking system. In this system you'll be able to manage a list of customers, view a list of items, and enter an order for a particular customer. There's too much code in the sample application to write it all from scratch. Therefore, you'll need to download the sample code from Peachpit's Web site (see Tips later in this section) if you haven't already done so.

Let's focus on task one first. The order-tracking system gives you two options for saving records. You can save records to memory or to XML files. To accomplish this, the application has two separate DLLs for storing records: one that uses Hashtables in memory and one that uses MySQL. You can guess that the memory one isn't going to be as popular as the XML one: Any orders you save to memory will be lost five minutes after you shut the application down. Nevertheless, using memory does make it easy to work with data and to learn the concepts.

Through a configuration setting, you'll be able to tell the application which mechanism you wish to use, and the application will load the appropriate DLL. For the application's code to work with either DLL you will use a concept you learned in Chapter 8, "Interfaces," known as polymorphism.

Polymorphism occurs when two classes implement the same interface but have slightly different implementations . You can write generic functions that use classes through the interfaces; the results of the functions will be different depending on what object you pass to them. The application has already been written to use interfaces. However, you may recall that interfaces are not creatable types; you have to create an object that implements the interface. Right now the application creates the objects in the ordersMemory.DLL. So if we want to make it work with the objects in ordersXML.DLL, we have to rewrite some of the code. Ideally, we want to change it only once and make it flexible enough to account for all the future DLLs we may writeand that's what the first task is about.

The first step in doing task one is to get familiar with the sample application. After you've learned a few tricks about loading DLLs dynamically, we'll work on changing the code.

To start working on the sample application:

  1. Download the sample code from Peachpit's Web site (see Tips on next page) if you haven't already done so. You'll notice that for Chapter 12 there are four projects. OrdersSystem is the Web application that enables you to enter customers and orders. The ordersInterfaces project has the definitions for the interfaces that all the other projects use ordersMemory and ordersXML take care of managing the data for the project.

  2. In the OrdersSystem directory, double-click on the file OrderSystem.sln to open the project in Visual Studio.

  3. Press F5 to run the application.

  4. You'll see the dialog in Figure 12.1 . Enter a customer's name in the Name field and click the Add button. This will add a customer to the list of customers.

    Figure 12.1. Probably Order Systems need to have more information than the person's name, but I'll leave adding that functionality "as an exercise for the reader."

    graphics/12fig01.gif

  5. After you add a few names , your screen will look like Figure 12.2 . You can delete names with the Delete link, or you can click View Orders to view all the orders for the selected customer and to add new orders. Click on View Orders for one of the customers in the list.

    Figure 12.2. The grid has a hidden column that stores an ID number for each customer. The View Orders link column has a property called DataNavigateUrlFormatString which is set to "orders.aspx?ID={0}"; when you click on it, it calls orders.aspx passing the ID number from column one (hidden) to the orders page.

    graphics/12fig02.gif

  6. You should see the dialog in Figure 12.3 . The top grid on this page lists all the items in inventory and their available quantities . To purchase an item, enter the amount next to the item you want and click the Add button.

    Figure 12.3. The first time you use this screen you'll only see the top grid with the products that are available. Enter an amount next to the item you wish to purchase and press Add.

    graphics/12fig03.gif

  7. After adding a few items to the list your page should resemble Figure 12.4 .

    Figure 12.4. After you've added a few items, your orders grid should resemble the grid in the figure. You can also put negative numbers in the purchase amount and click add, to subtract the quantity from the amount purchased.

    graphics/12fig04.gif

  8. Play around with the program until you feel you're familiar with it. Be gentle with the dataI didn't put in a lot of safeguards. For example, right now the program will let you purchase more items than there are in inventory.

graphics/tick.gif Tips

  • As with the other projects in this book, building the project isn't necessary to learn the concepts in this chapter.

  • Skeletons for each project can be downloaded from Peachpit's Web site, http://www.peachpit.com/vqs/csharp.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net