• 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();

  • 相关阅读:
    Selenium(Python)等待元素出现
    java文件的I/O
    Map的四种遍历方式
    模板类实现链表
    字符串相关库函数使用
    动态规划之背包问题
    最长递增子序列
    深度优先搜索(DFS),逃离迷宫
    素数环问题(递归回溯)
    枚举(百鸡问题)
  • 原文地址:https://www.cnblogs.com/huaizuo/p/2113597.html
Copyright © 2020-2023  润新知