Completing Task One in the Sample Application


You now know enough to complete task one in the sample application. You may recall from earlier sections that what you're going to be working on is the ability to have your Web application choose between two DLLs at runtime: one that saves information to memory, and one that uses XML files. The names of the DLLs that you're going to load are: ordersMemory.DLL and ordersXML.DLL. Both DLLs are in a subdirectory called shared under the application's directory.

To complete task one in the sample application:

  1. In the OrdersSystem project select View Solution Explorer from the menu bar.

  2. Double-click on the file utilities.cs to open it in the editor.

  3. Locate the functions CreateCustomerMgr, CreateInventoryMgr, and CreateOrderMgr. This is where you will add your work. You'll notice that all of these three functions create objects by calling new on one of the classes in ordersMemory. Let's focus on the CreateCustomerMgr function.

  4. Above the line that reads retVal = new ordersMemory.CustomerMgrMem(); add the code in Figure 12.22 . This code reads the path to the DLL we want from the Web.config file. You'll add the configuration setting to the file shortly.

    Figure 12.22 Web.Config can be used to store settings for your application and it's now the preferred method for storing application settings rather than the registry. In this case we're storing the path to the DLL and the name of the class we want to create.
     string ordersDLL = System.Configuration.                   ConfigurationSettings.                   AppSettings["OrdersDLL"]; string className = System.Configuration.                   ConfigurationSettings.                   AppSettings["CustomerMgrClass"]; 
  5. Replace the line that reads retVal = new ordersMemory.CustomerMgrMem(); with the code in Figure 12.23 .

    Figure 12.23 Once we know the full path to the DLL, we can load it and create an instance of a CustomerMgr class. Then we cast the object returned to the interface that all versions of the class implement.
     public class VisibleColumnAttribute : System.Attribute {    private bool _visible;    public bool IsReadOnly;    public VisibleColumnAttribute(    bool value)    {      _visible=value;    } } 
  6. Now that the code loads the ordersMemory.DLL dynamically, we can take off the reference to the ordersMemory.DLL file in the project references. Expand the references for the project ( Figure 12.24 ).

    Figure 12.24. We no longer need the reference to the ordersMemory DLL. You can highlight it and delete it.

    graphics/12fig24.gif

  7. Highlight ordersMemory from the list and press the Delete key.

  8. Open the file Web.config from the Solution Explorer window. Just after the line that reads <configuration> copy the code in Figure 12.25 .

    Figure 12.25 You can add an appSettings section to Web.config and in there add your application's custom configuration information. This is a lot easier than creating registry keys.
     <appSettings>    <add key="OrdersDLL"         <value="shared/ordersMemory.DLL" />    <add key="CustomerMgrClass"         <value="ordersMemory.CustomerMgrMem" /> </appSettings> 
  9. Repeat steps 4 and 5 for the other two functions: CreateInventoryMgr, and CreateOrderMgr.

graphics/tick.gif Tips

  • To test the application, go to the web.config file and change the path to the DLL to point to the ordersXML.DLL.

  • Notice from step 8 that you can add custom configuration to web.config and read it with the code in Figure 12.23 .




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