Freigeben über


Adding Application Code

The Caching Application Block is designed to support the most common situations for storing data in a cache. When adding your application code, refer to the scenarios in the Key Scenarios sections and select the ones that best suit your situation. Use the code that accompanies the scenario either as it is, or revise it as required.

To prepare your application

  1. Add a reference to the Caching Application Block assembly. In Microsoft Visual Studio®, right-click your project node in Solution Explorer, and then click Add Reference. Click the Browse tab and find the location of the Microsoft.Practices.EnterpriseLibrary.Caching.dll assembly. Select the assembly, and then click OK to add the reference.

  2. Follow the same procedure to set a reference to the following assemblies:

    • Microsoft.Practices.EnterpriseLibrary.Common.dll
    • Microsoft.Practices.ServiceLocation.dll
    • Microsoft.Practices.Unity.dll
    • Microsoft.Practices.Unity.Interception.dll
  3. If you are using the database backing store, add a reference to Microsoft.Practices.EnterpriseLibrary.Caching.Database.dll and Microsoft.Practices.EnterpriseLibrary.Data.dll.

  4. If you are using the Cryptography Application Block to encrypt data in the cache, add references to Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll and Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.dll.

  5. (Optional) To use elements from the Caching Application Block without fully qualifying the element reference, add the following using statements (C#) or Imports statements (Microsoft Visual Basic®) to the top of your source code file.

    using Microsoft.Practices.EnterpriseLibrary.Caching;
    using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
    
    'Usage
    Imports Microsoft.Practices.EnterpriseLibrary.Caching
    Imports Microsoft.Practices.EnterpriseLibrary.Caching.Expirations
    

Note

For Visual Basic projects, you can also use the References page of the Project Designer to manage references and imported namespaces. To access the References page, select a project node in Solution Explorer, and then click [project name] Properties on the Project menu. When the Project Designer appears, click the References tab.

Next, add the application code. Generally, there are two steps to create code that uses the Caching Application Block:

  1. Resolve a CacheManager instance.
  2. Call the appropriate methods.

Each key scenario demonstrates how to incorporate these steps into an application.