Thursday, May 14, 2009

Enterprise Library 4.1 Configuration – Part 1 Cache

Today I had to develop a small snippet on how to configure enterprise library without going through the application xml configuration system. This gave me the idea of creating a new configuration source that works in memory without the need for xml files.

The component described in this article implements convention over configuration for enterprise library.

Convention over Configuration implementation

First I created a static class to hold the convention values. That is the values for the configuration options that are used if nothing else is said:

   1: /// <summary>
   2: /// Holds the default values for assembling enterprise library components.
   3: /// </summary>
   4: public static class EntLibConvention
   5: {
   6:     /// <summary>
   7:     /// Initializes static members of the <see cref="EntLibConvention"/> class.
   8:     /// </summary>
   9:     static EntLibConvention()
  10:     {
  11:         CacheManagerType = typeof(Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager);
  12:         CacheBackingStoreType = typeof(Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore);
  13:         // ... other settings
  14:     }
  15:  
  16:         /// <summary>
  17:         /// Gets or sets the <see cref="System.Type"/> for the class implementing the cache manager.
  18:         /// </summary>
  19:         public static Type CacheManagerType
  20:         {
  21:             get;
  22:             set;
  23:         }
  24:  
  25:         /// <summary>
  26:         /// Gets or sets the <see cref="System.Type"/> for the class implementing the cache backing store.
  27:         /// </summary>
  28:         public static Type CacheBackingStoreType
  29:         {
  30:             get;
  31:             set;
  32:         }
  33: }

In this initial version one must override the values in the convention class before asking objects to the factory. I added improving this to the backlog to make a first quick release.

Using the extensions

To get objects use another static class name Factories. This class will have one property per supported block holding the concrete factory. For now the caching application block is supported.

I think a good way to express how to use a library is showing some short unit tests. So to get the default manager write something like this:

   1: [TestMethod]
   2: public void TestAddItemToCache()
   3: {
   4:     var cacheManager = Factories.CacheFactory.Default;
   5:     Assert.IsNotNull(cacheManager);
   6:  
   7:     cacheManager.Add("obj1", "object one");
   8:  
   9:     cacheManager = Factories.CacheFactory.Default;
  10:  
  11:     var obj = (string)cacheManager.GetData("obj1");
  12:     Assert.AreEqual(obj, "object one");
  13: }

And if one wants a named manager, so that it can get different instances to separate information:

   1: [TestMethod]
   2: public void TestAddItemToNamedCache()
   3: {
   4:     Factories.CacheFactory.AddNamedConfiguration("myCache");
   5:  
   6:     var cacheManager = Factories.CacheFactory.GetNamedManager("myCache");
   7:     Assert.IsNotNull(cacheManager);
   8:  
   9:     cacheManager.Add("obj1", "object one");
  10:  
  11:     cacheManager = Factories.CacheFactory.GetNamedManager("myCache");
  12:  
  13:     var obj = (string)cacheManager.GetData("obj1");
  14:     Assert.AreEqual(obj, "object one");
  15: }

I plan to add support to the remaining blocks in a near future. The library is distributed under LGPL. I am thinking on using a strong signature in the next release but I need to put in place a way to take out my key file out of the source release :).

Ready to try it? Get it:

Source Code

Binaries

1 comment:

Aashika said...

CAB in the Enterprise Library is been implanted to simplify development tasks but due to it’s in process and standalone behavior it reduces the performance of the application which is not in most case tolerate able. This drawback can be overcome by using third party integrations including NCache which is free available and can be tested for the performance and scalability
www.alachisoft.com/ncache/cab_index.html