AggregateCacheDependency(聚合缓存依赖)

代码
  1  /// <summary>
  2     /// This is the interface that the DependencyFactory (Factory Pattern) returns.
  3     /// Developers could implement this interface to add different types of Cache Dependency to Pet Shop.
  4     /// </summary>
  5     public interface ITPSCacheDependency
  6     {
  7 
  8         /// <summary>
  9         /// Method to create the appropriate implementation of Cache Dependency
 10         /// </summary>
 11         /// <returns>CacheDependency object(s) embedded in AggregateCacheDependency</returns>
 12         AggregateCacheDependency GetDependency();
 13     }
 14     /// <summary>
 15     /// This is the base class for SQL2KCacheDependency implementation that encapsulates common
 16     /// algorithm to retrieve database and table names from configuration file and create
 17     /// the necessary AggregateCacheDependency object
 18     /// </summary>
 19     public abstract class TableDependency : TPS.ICacheDependency.ITPSCacheDependency
 20     {
 21 
 22         // This is the separator that's used in web.config
 23         protected char[] configurationSeparator = new char[] { ',' };
 24 
 25         protected AggregateCacheDependency dependency = new AggregateCacheDependency();
 26 
 27         /// <summary>
 28         /// The constructor retrieves all related configuration and add CacheDependency object accordingly
 29         /// </summary>
 30         /// <param name="configKey">Configuration key for specific derived class implementation</param>
 31         protected TableDependency(string configKey)
 32         {
 33 
 34             string dbName = ConfigurationManager.AppSettings["CacheDatabaseName"];
 35             string tableConfig = ConfigurationManager.AppSettings[configKey];
 36             string[] tables = tableConfig.Split(configurationSeparator);
 37 
 38             foreach (string tableName in tables)
 39                 dependency.Add(new SqlCacheDependency(dbName, tableName));
 40         }
 41 
 42         public AggregateCacheDependency GetDependency()
 43         {
 44             return dependency;
 45         }
 46     }
 47 
 48     /// <summary>
 49     /// Implementation of Product Cache Dependency for SQL Server 2000
 50     /// </summary>
 51     public class Product : TableDependency
 52     {
 53 
 54         /// <summary>
 55         /// Call its base constructor by passing its specific configuration key
 56         /// </summary>
 57         public Product() : base("ProductTableDependency") { }
 58     }
 59 
 60     /// <summary>
 61     /// This class is provided to ease the usage of DependencyFactory from the client.
 62     /// It's main usage is to determine whether to invoke the DependencyFactory.  
 63     /// When no assembly is specified under CacheDependencyAssembly section in configuraion file, 
 64     /// then this class will return null
 65     /// Notice that this assembly reference System.Web
 66     /// </summary>
 67     public static class DependencyFacade
 68     {
 69         private static readonly string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];
 70 
 71         public static AggregateCacheDependency GetProductDependency()
 72         {
 73             if (!string.IsNullOrEmpty(path))
 74                 return DependencyAccess.CreateProductDependency().GetDependency();
 75             else
 76                 return null;
 77         }
 78     }
 79 
 80  public static class DependencyAccess
 81     {
 82 
 83         /// <summary>
 84         /// Method to create an instance of Product dependency implementation
 85         /// </summary>
 86         /// <returns>Product Dependency Implementation</returns>
 87         public static ITPSCacheDependency CreateProductDependency()
 88         {
 89             return LoadInstance("Product");
 90         }
 91 
 92 
 93         /// <summary>
 94         /// Common method to load dependency class from information provided from configuration file 
 95         /// </summary>
 96         /// <param name="className">Type of dependency</param>
 97         /// <returns>Concrete Dependency Implementation instance</returns>
 98         private static ITPSCacheDependency LoadInstance(string className)
 99         {
100 
101             string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];
102             string fullyQualifiedClass = path + "." + className;
103 
104             // Using the evidence given in the config file load the appropriate assembly and class
105             return (ITPSCacheDependency)Assembly.Load(path).CreateInstance(fullyQualifiedClass);
106         }
107     }
108 
109 
110         /// <summary>
111         /// Add cache dependency
112         /// </summary>
113         protected void Page_Load(object sender, EventArgs e)
114         {
115             this.CachePolicy.Dependency = DependencyFacade.GetProductDependency();
116         }
117 
118 


posted on 2010-11-10 11:03  HardChen  阅读(442)  评论(0编辑  收藏  举报

导航