zoukankan      html  css  js  c++  java
  • petshop缓存依赖的工厂模式

    接口:ICacheDependency

    实现类:TableCacheDependency下的类

    工厂类:CacheDependencyFactory.DataAccess

    这个工厂类跟之前的工厂类差不多,实现起来没什么太大的区别,但还是记录下来吧,因为毕竟这种模式知道归知道罢了。

    先看下这个接口ICacheDependency吧:

    publicinterface IPetShopCacheDependency {

    ///<summary>
    /// Method to create the appropriate implementation of Cache Dependency
    ///</summary>
    ///<returns>CacheDependency object(s) embedded in AggregateCacheDependency</returns>
    AggregateCacheDependency GetDependency();
    }

    然后就是实现类了,先来看下这个缓存数据表吧:

    由此可见要实现缓存的类事Category、Item和Product这些类。那么还是先来看看这些类的实现吧,先不管AspNet_SqlCacheTablesForChangeNotification表的存储过程和触发器了。

    就看下Category吧:

    publicclass Category : TableDependency {

    ///<summary>
    /// Call its base constructor by passing its specific configuration key
    ///</summary>
    public Category() : base("CategoryTableDependency") { }
    }

    这个Category是继承TableDependency类的,而这TableDependency是继承ICacheDependency这个类,也算是间接继承的。看下这个TableDependency这个类事怎么实现缓存的:

    publicabstractclass TableDependency : PetShop.ICacheDependency.IPetShopCacheDependency {

    // This is the separator that's used in web.config
    protectedchar[] configurationSeparator =newchar[] { ',' };

    protected AggregateCacheDependency dependency =new AggregateCacheDependency();

    ///<summary>
    /// The constructor retrieves all related configuration and add CacheDependency object accordingly
    ///</summary>
    ///<param name="configKey">Configuration key for specific derived class implementation</param>
    protected TableDependency(string configKey) {

    string dbName = ConfigurationManager.AppSettings["CacheDatabaseName"];//MSPetShop4
    string tableConfig = ConfigurationManager.AppSettings[configKey];//Product,Category,Item
    string[] tables = tableConfig.Split(configurationSeparator);//','

    foreach (string tableName in tables)
    dependency.Add(
    new SqlCacheDependency(dbName, tableName));//吧数据库表的查询结果放在SqlCacheDependency中
    }
    //实现父类的方法
    public AggregateCacheDependency GetDependency() {
    return dependency;
    }
    }

    这里用到了SqlCacheDependency类和AggregateCacheDependency类。那么这两个类到底做了写什么呢?待续。

    现在来看下工厂类吧(这个工厂类还是利用放射依赖注入的方法来实例化Category等类的):

    publicstaticclass DependencyAccess {
    ///<summary>
    /// Method to create an instance of Category dependency implementation
    ///</summary>
    ///<returns>Category Dependency Implementation</returns>
    publicstatic IPetShopCacheDependency CreateCategoryDependency() {
    return LoadInstance("Category");
    }

    ///<summary>
    /// Method to create an instance of Product dependency implementation
    ///</summary>
    ///<returns>Product Dependency Implementation</returns>
    publicstatic IPetShopCacheDependency CreateProductDependency() {
    return LoadInstance("Product");
    }

    ///<summary>
    /// Method to create an instance of Item dependency implementation
    ///</summary>
    ///<returns>Item Dependency Implementation</returns>
    publicstatic 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>
    privatestatic IPetShopCacheDependency LoadInstance(string className) {

    string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];//PetShop.TableCacheDependency
    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);
    }
    }
    }

    接下来就看实现类DependencyFacade了:

    publicstaticclass DependencyFacade {
    privatestaticreadonlystring path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];//PetShop.TableCacheDependency

    publicstatic AggregateCacheDependency GetCategoryDependency() {
    if (!string.IsNullOrEmpty(path))//PetShop.TableCacheDependency这个怎么判断是否为空?
    return DependencyAccess.CreateCategoryDependency().GetDependency();//直接调用
    else
    returnnull;
    }

    publicstatic AggregateCacheDependency GetProductDependency() {
    if (!string.IsNullOrEmpty(path))
    return DependencyAccess.CreateProductDependency().GetDependency();
    else
    returnnull;
    }

    publicstatic AggregateCacheDependency GetItemDependency() {
    if (!string.IsNullOrEmpty(path))
    return DependencyAccess.CreateItemDependency().GetDependency();
    else
    returnnull;
    }
    }

    这个工厂类的实现有别于DALFactory吧:

    因为在数据访问层的BLL.Category,因为Category的实现是直接继承接口的,而这个是间接继承的,所以我们要经过两步,如DependencyAccess.CreateItemDependency().GetDependency();

  • 相关阅读:
    线段树练习两题
    DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)
    线段树和树状数组问题补充
    一些常见的优化:读入优化,滚动数组
    单调队列应用--BZOJ 3831 Little Bird
    单调队列练习之广告印刷
    详解--单调队列 经典滑动窗口问题
    数据结构--栈 codevs 1107 等价表达式
    离散化+线段树 POJ 3277 City Horizon
    求次短路 codevs 1269 匈牙利游戏
  • 原文地址:https://www.cnblogs.com/huaizuo/p/2113597.html
Copyright © 2011-2022 走看看