缓存

首先建立一个缓存接口如下:
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;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值