Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating Custom Providers for Enterprise Library : Page 7

When the providers installed with Enterprise Library don't meet your needs, take advantage of the library's pluggable architecture and roll your own.


advertisement
Working with the Custom File Cache Provider in ASP.NET
To demonstrate use of the custom provider, the example application contains an option to use either the Isolated Storage backing store provider or the custom backing store provider discussed in this article. If you select the Custom Disk File Cache option and click the button to cache a DataSet, you see the page reports that one item is cached (see Figure 7).

If you now open Windows Explorer on the folder configured as the path attribute, where the provider creates its cache data files, you will see two files named with the hashed cache storage key, with the file extensions .cachedata and .cacheinfo (see Figure 8). The .cachedata file contains the serialized representation of the DataSet. The .cacheinfo file contains the (String) cache key name, the last accessed date and time, and the sliding expiration value:

SalesDataset 16/10/2006 12:09:39 00:00:30

 
Figure 7. Cache Testing: The sample application lets you test the custom cache backing store provider by caching a DataSet.
 
Figure 8. Cached Files: In Windows Explorer, you can see the two files created to cache the DataSet.
Now click the button to load the DataSet back from the cache, and you see it displayed in the page (see Figure 9). You can close the browser and then reopen it to see that the cache survives application restarts. However, notice that when the cached item expires (after 30 seconds) the Cache Manager calls the methods of the provider to remove the item from the cache, which deletes the files.

 
Figure 9. Retrieving Cached Data: After retrieving the cached DataSet, the sample application displays it on the page.
The changes required to application code to accommodate a custom provider are minimal. If you compiled the provider into a separate assembly, outside of the Caching Application Block, you must add a reference to it to your project and to your code. If you compiled it into the Caching Application Block, existing references to Microsoft.Practices.EnterpriseLibrary.Caching and Microsoft.Practices.EnterpriseLibrary.Caching.Expirations are sufficient.

The code in the example ASP.NET application that creates the DataSet and caches it uses the value of the option buttons to determine which Cache Manager to instantiate (using the static GetCacheManager method of the CacheFactory class)—either the one that uses the custom provider or the one that uses the Isolated Storage provider. Note that the application code is in VB.NET even though the Enterprise Library itself—and the custom provider code you've seen here—are both in C#:

' create DataSet using Data Access Application Block here ... ' ... ' use the Caching Application Block Dim diskCache As CacheManager If optCustomCache.Checked Then ' use custom cache provider diskCache = CacheFactory.GetCacheManager("Custom Cache Manager") Else ' use default (Isolated Storage) cache provider diskCache = CacheFactory.GetCacheManager() End If ...

The code to cache the DataSet is the same whichever provider you choose—indicating how the provider architecture disconnects the Caching Application Block from the physical backing stores, and allows you to extend the block and reconfigure your applications as your requirements change:



... ' store the Dataset in the cache diskCache.Add("SalesDataset", ds, CacheItemPriority.Normal, Nothing, _ New SlidingTime(TimeSpan.FromSeconds(30))) lblCount.Text = String.Format("Cache contains {0} item(s)<br />", diskCache.Count)

Extracting the DataSet from the Cache Manager is also easy, irrespective of the chosen backing store provider. The code instantiates the selected provider type, and calls the Cache Manager's GetData method to get the cached item back as an Object type. It can then convert the Object to a DataSet, and display it in a GridView control on the page:

' use the Caching Application Block Dim diskCache As CacheManager If optCustomCache.Checked Then ' use custom cache provider diskCache = CacheFactory.GetCacheManager("Custom Cache Manager") Else ' use default (Isolated Storage) cache provider diskCache = CacheFactory.GetCacheManager() End If ' retrieve the Dataset from the cache Dim ds As DataSet = DirectCast(diskCache.GetData("SalesDataset"), DataSet) If ds Is Nothing Then lblError.Text = "Dataset not found in Cache" Else ' populate the GridView GridView1.DataSource = ds.Tables(0) GridView1.DataBind() End If

This article demonstrates how easily you can create pluggable custom providers that extend the Enterprise Library application blocks. The example shows a custom cache backing store provider, but the principles are the same for all the application blocks. By inheriting from a suitable base class for the provider, or implementing the appropriate interface, you can connect an application block to the resources or data it processes or uses in such a way that the users can reconfigure their applications as requirements change without having to modify their application code.

You can, of course, implement much more complex providers, though you should bear in mind the suitability and considerations discussed in this article.

The one area where the example provider is also deficient in relation to the built-in providers is that it must be configured as a custom provider, and therefore requires users to specify provider configuration values (such as the path for the cache disk files) using name/value pairs. In a related article (see http://www.daveandal.net/articles/EntLibASPNET/), you will see how you can add configuration design support to a provider so that it appears as a first-class member of Enterprise Library and is indistinguishable from the built-in providers.



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.

 

 

Sitemap