WPF 4: Using Enterprise Library 5.0 Caching Block in WPF application
Posted by: Mahesh Sabnis
in Category WPF
Abstract: In this article, I will demonstrate how to use the Enterprise Library 5.0 Caching Application block in a WPF application.
Enterprise Library 5 provides a collection of reusable application blocks which helps developers to deal with challenges in .NET enterprise application development. Enterprise library provides support for design patterns implementation, giving a framework to developers to design and develop code which collaborates classes, objects, services etc. in an application. In this article, we are using Enterprise Library 5.0, which can be downloaded from here. There are a lots of changes introduced in this v5 release. For details on this release, you can download the documentation and go through it.
In this article, I have used the Caching Application block provided in Enterprise Library 5.0. This caching framework can be manipulated through simple programming classes to store and retrieve data, to and from the cache. For the application, more than one cache can be configured. In this article I have demonstrated a simple implementation of cache for the WPF application. The application makes a call to the database table and fetches the data. The scenario is that if this data is frequently required by the application, then it will be stored in a Cache or future use. Further this can be stored in an Isolated Stored by caching it in Cache, using a Cache Manager. The architecture can be explained with the following diagram.
As you can see, the cache data is stored in the local memory. Caching Manager is responsible to manage the cache for storing and retrieving data from Cache. Let us see an example. Follow these steps:
Step 1: Open VS2010 and create a WPF application. Name it as ‘WCF4_CachingApplicationBlock_EntLib5’.
Step 2: In this project, add a reference to the following libraries:
Note: You will get these libraries from your Enterprise Library installation location.
Step 3: In this project add a new class file and name it as ‘DataAccessLayer.cs’. Write the following code in it:
The above code connects with the database and retrieve the data from it.
Step 4: In the same project, add a new class file and name it as ‘CachingInfrastructure.cs’. This file contains logic for storing the data in cache using ‘ICacheManager’ interface. The code also specifies the expiry time for the data in cache. The code is as below:
Step 5: In the project, add a new App.Config file. This file will contain the Caching configuration. Thankfully when Enterprise Library is installed, we get a tool for writing configuration for various caching blocks for the application. Right-Click on the App.Config file and select ‘Edit Enterprise Library V5 Configuration’. You will get the configuration options. Select the ‘Blocks’ menu and select ‘Add Caching Settings’. You should see the following configuration.
Save the configuration. Now if you open the App.Config file in the VS editor, you will find the ‘cachingConfiguration’ tag is added. This defines the settings for the Cache Manager with BackingStore value as ‘NullBackingStore’. This means that the Cache is managed by the In-Memory data store.
Step 6: Open MainWindow.Xaml, and design the Window as shown below:
Step 7: In the MainPage.Xaml.cs write the following code:
The ‘Load Data’ button click event calls the GetEmployee() method from the CachingInfrastructure class. Then it shows the status of cache in the TextBlock named ‘txtCacheStatus’. The Selectionchanged event retrieves the data from the cache.
Step 8 : Run the application. The following result will be displayed:
Result after the ‘Load Data’ button is clicked
Shown above is the result after selecting EmpName from the Combobox.
Conclusion: With the support of caching application blocks in Enterprise Library, developers are provided an excellent feature of storing frequently required data in-memory of the application and hence helps in improving the performance of the application.
The entire source code of this article can be downloaded over here