首先建立一个缓存接口如下:
using System.Web.Caching;
namespace PetShop.ICacheDependency {
/// <summary>
/// This is the interface that the DependencyFactory (Factory Pattern) returns.
/// Developers could implement this interface to add different types of Cache Dependency to Pet Shop.
/// </summary>
public interface IPetShopCacheDependency {
/// <summary>
/// Method to create the appropriate implementation of Cache Dependency
/// </summary>
/// <returns>CacheDependency object(s) embedded in AggregateCacheDependency</returns>
AggregateCacheDependency GetDependency();
}
}
缓存工厂CacheDependencyFactory下有两个类
DependencyAccess.cs
DependencyFacade.cs
其中DependencyAccess 代码全部如下:
using System.Reflection;
using System.Configuration;
using PetShop.ICacheDependency; //必要的引用 因为涉及道反射 配只文件 和缓存
namespace PetShop.CacheDependencyFactory {
public static class DependencyAccess {
/// <summary>
/// Method to create an instance of Category dependency implementation
/// </summary>
/// <returns>Category Dependency Implementation</returns>
//注意返回类型是IPetShopCacheDependency
public static IPetShopCacheDependency CreateCategoryDependency() {
return LoadInstance("Category");
}
/// <summary>
/// Method to create an instance of Product dependency implementation
/// </summary>
/// <returns>Product Dependency Implementation</returns>
public static IPetShopCacheDependency CreateProductDependency() {
return LoadInstance("Product");
}
/// <summary>
/// Method to create an instance of Item dependency implementation
/// </summary>
/// <returns>Item Dependency Implementation</returns>
public static IPetShopCacheDependency CreateItemDependency() {
return LoadInstance("Item");
}
/// <summary>
/// Common method to load dependency class from information provided from configuration file
/// </summary>
/// <param name="className">Type of dependency</param>
/// <returns>Concrete Dependency Implementation instance</returns>
private static IPetShopCacheDependency LoadInstance(string className) {
string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];
string fullyQualifiedClass = path + "." + className;
// Using the evidence given in the config file load the appropriate assembly and class
return (IPetShopCacheDependency)Assembly.Load(path).CreateInstance(fullyQualifiedClass);
}
}
}
//注意上述的多个方法中都含有个方法LoadInstance(string) 为什么和前面讲解的DALFactory工厂方法利用到反射得到类信息不一样呢,
这里用到了一个设计模式 即下面类DependencyFacade中用到的门面模式.
using System.Configuration;
using System.Web.Caching;
using System.Collections.Generic;
using PetShop.ICacheDependency;
namespace PetShop.CacheDependencyFactory {
/// <summary>
/// This class is provided to ease the usage of DependencyFactory from the client.
/// Its main usage is to determine whether to invoke the DependencyFactory.
/// When no assembly is specified under CacheDependencyAssembly section in configuraion file,
/// then this class will return null
/// Notice that this assembly reference System.Web
/// </summary>
public static class DependencyFacade {
private static readonly string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];
//结合上述类DependencyAccess进行讲解,根据信息调用不同的方法DependencyAccess.CreateCategoryDependency()
//该方法返回得到的接口在统一调用接口的共有方法GetDependency()即可.
public static AggregateCacheDependency GetCategoryDependency() {
if (!string.IsNullOrEmpty(path))
return DependencyAccess.CreateCategoryDependency().GetDependency();
else
return null;
}
public static AggregateCacheDependency GetProductDependency() {
if (!string.IsNullOrEmpty(path))
return DependencyAccess.CreateProductDependency().GetDependency();
else
return null;
}
public static AggregateCacheDependency GetItemDependency() {
if (!string.IsNullOrEmpty(path))
return DependencyAccess.CreateItemDependency().GetDependency();
else
return null;
}
}
}