Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Adding Configuration Support for Custom Providers in Enterprise Library in ASP.NET 2.0 : Page 4

By adding configuration design support, you can make your custom providers look and feel just like the built-in providers, letting users select them and provide settings through the Configuration Console.

Editing the Design Resources File
Several values used in the configuration node class discussed in the previous section were text strings extracted from the resource file for the application block, including the status bar and help text strings used in the Configuration Console and the Save File dialog filter. You'll see more code that uses string resources in the following sections. Placing these language-specific strings in the project resources file makes future updates easier, and allows you to support multiple languages if required.

To add these text strings to the project resources file, open the file Resources.resx from within the Properties folder of the Caching.Configuration.Design project. Add the entries in Table 1 to the file using the resource editor that appears when you open the file:

Table 1. String Resources: The table shows the names and values of the string resources added to the project resources file and used in the menus, the status bar, as help text, and as a filter for the Save File dialog.
String Resource Name Value
FileCacheNameDescription Gets or sets the path and name of the cache disk file.
FileCachePathFileDialogFilter All files (*.*)|*.*
FileCacheUICommandLongText Add custom file cache storage
FileCacheUICommandText Custom File Cache Storage

Executing Node and Command Registration
The final stage in enabling design support for your provider is to edit the two classes that register the nodes in the Configuration Console tree view, and add the commands to the main and shortcut menus. The Enterprise Library configuration system allows application blocks to override the Register method in the two classes that register tree view nodes (the NodeMapRegistrar class) and menu commands (the CommandRegistrar class).

The Design project for the Caching Application Block contains two classes that extend the NodeMapRegistrar and CommandRegistrar classes—CachingNodeMapRegistrar and CachingCommandRegistrar. You add your own registration code to the Register methods of these classes.

For the CachingNodeMapRegistrar class, you simply insert a call to the AddSingleNodeMap method, specifying the text to display in the configuration console for the item, and the types to use for the configuration node and the configuration data:

... AddSingleNodeMap(Resources.FileCacheUICommandText, typeof(CustomFileCacheStorageNode), typeof(CustomFileCacheStorageData)); ...

For the CachingCommandRegistrar class, you create a routine that calls the AddSingleChildNodeCommand method, specifying the command text for the menus, the text to display in the status bar, the type of the provider configuration node class, the type of the base configuration node class, and the type of the base node class:

private void AddFileCacheStorageCommand() { AddSingleChildNodeCommand(Resources.FileCacheUICommandText, Resources.FileCacheUICommandLongText, typeof(CustomFileCacheStorageNode), typeof(CacheStorageNode), typeof(CacheManagerNode)); }

Then you modify the Register method to insert a call to your new routine, followed by a call to the AddDefaultCommands method that specifies the type of the provider configuration node class:

... AddFileCacheStorageCommand(); AddDefaultCommands(typeof(CustomFileCacheStorageNode)); ...

Compiling and Deploying the Provider
You have now created all the classes you need, and completed the modifications to existing classes. The next step is to compile the projects and copy the assemblies into the correct folders. The easiest way to achieve this is to use the two utilities available from the Enterprise Library section of your Start menu—"Build Enterprise Library" and "Copy Assemblies to bin Folder."

Configuring the Custom File Cache Provider
Open the Configuration Console from the Enterprise Library section of your Start menu and either create a new application, or open an existing Web.config or App.config file. If you start a new application, or the existing configuration does not already contain the Caching Application Block, right-click on the Application Configuration entry, select "New," and click "Caching Application Block."

Within the Cache Managers section, select the Cache Manager node (or the existing child node that is a renamed cache manager node), right-click, and select "New." The shortcut menu shows the new provider type "Custom File Cache Storage," which uses the custom provider with the design support described earlier in this article. As you hover the mouse over this item, the status bar shows the text "Add custom file cache storage" (see Figure 3). Alternatively, you can select "New" and then "Custom File Cache Storage" from the Action menu when the Cache Manager node is selected in the tree view.

Figure 3. Custom Configuration Support: The figure shows the Configuration Console when selecting the "Custom Cache File Storage" option that uses the example provider.
Figure 4. Using the Configuration Console: The figure shows the process of changing the name and setting the CacheFilePath property for the example provider.
When you select the new Custom File Cache Storage item in the tree view, the right-hand pane displays the two properties you can edit (see Figure 4). You can change the name of this Cache Manager, and you can set the value of the CacheFilePath property.

In Figure 4, notice that the editor for the FileCachePath property, although it is defined as a String value, displays a "browse" button with an ellipsis (...) when you select it. Clicking this opens a File Save dialog so you can navigate to the folder where you want to store the cache files. However, as discussed earlier, because of the nature of the dialog class, you must either enter a name for a file or select an existing file. If you select an existing file, clicking the "Save" button prompts you to over-write it. However, once the dialog closes, the property contains just the path because the property accessor code (as you saw in the earlier section) removes the filename. Obviously, this is not an ideal situation, but to implement a better path selection mechanism, you must write a custom UITypeEditor class.

Using the Custom File Cache Provider in ASP.NET
In use, the configurable custom cache backing store provider discussed in this article behaves exactly the same at runtime as the non-design-configured version you saw in the previous article, because the only change made to the provider in this article was the addition of design support for the Configuration Console. The code that performs the caching operations is identical.

Figure 5 shows the Web.config file for the ASP.NET example application open in the Enterprise Library Configuration Console. You can see both the Cache Manager that uses the Isolated Storage Backing Store provider, and the second Cache Manager that uses the Custom File Backing Store provider described in this article. The CacheFilePath property is set to a temporary folder on the local machine.

Figure 5. Configuration in ASP.NET: The figure shows the Caching Application Block configuration for the ASP.NET example application.
Figure 6. Adding a DataSet: When you add a DataSet to the cache using the custom caching provider, the application displays the number of items in the cache.
Figure 7. Retrieving the DataSet: When you retriev the DataSet from the cache using the custom caching provider, the application displays the items in the dataset.
When you select the Custom Disk File Cache option in the ASP.NET example application (see Figure 6), the code uses the CustomFileBackingStore provider to write a DataSet to the cache, and then displays the number of items in the cache.

When you click the button to retrieve the DataSet, you see it displayed in the page—just as in the examples in the previous articles in this series (see Figure 7).

This article is the last of a series on using Enterprise Library in your ASP.NET applications, showing how you can add design support to custom providers so that they integrate fully with Enterprise Library, becoming indistinguishable from the built-in providers. For more information, an excellent Enterprise Library reference is the Addison-Wesley book Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications and Services, which will help you understand the workings of each block, the Enterprise Library core and configuration systems, and the best approaches for using and extending the application blocks.

Alex Homer is a director of Stonebroom, Ltd., a software development, consulting, and training organization. He was formerly lead technical author and reviewer for Wrox, specializing in Microsoft Web and database technologies. You can reach him through his Web site.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.