ASP.NET Microsoft .NET Pet Shop 3.x(二)

正在学习PetShop3.x,现将一些自己的一些总结写出来.
PetShop3.x分层分得很清楚,分别为UI,Business Layer,Data  Access Layer,典型的N层体系结构.表现的
非常棒.
但是仔细一看源代码,发现并不是那么简单分清楚,原因就是在表现Data Access layer时,做了一些易于扩展的
架构,那就时工厂模式.所以为了把它搞清出,我专门选了一个功能来研究,其他的功能基本上都是一样,大同小异了.我挑选的是Product这个.
先来一张图:

可以看到,PetShop.Web的Catagory调用BLL层里的Product,
代码如下:可以在catagory.aspx.cs中找到
 // Check to see if the contents are in the Data Cache
   if(Cache[categoryKey] != null){
    // If the data is already cached, then used the cached copy
    products.DataSource = (IList)Cache[categoryKey];
   }else{
    // If the data is not cached, then create a new products object and request the data
    Product product = new Product();
    IList productsByCategory = product.GetProductsByCategory(categoryKey);

    // Store the results of the call in the Cache and set the time out to 12 hours
    Cache.Add(categoryKey, productsByCategory, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
    products.DataSource = productsByCategory;
   }

而PetShop.BLL.Product的GetProductsByCategory是如何处理的呢?
看看如下代码
 public IList GetProductsByCategory(string category) {

   // Return null if the string is empty
   if (category.Trim() == string.Empty)
    return null;

   // Get an instance of the Product DAL using the DALFactory
   IProduct dal = PetShop.DALFactory.Product.Create();

   // Run a search against the data store
   return dal.GetProductsByCategory(category);

  }

上面就是其实现代码了.我们会发现用到了了PetShop.DALFactory,这个就是工厂模式了.Create了一个IProduct的实例.其实
这个时候它创建的是来自PetShop.SQLServerDAL的Product类,所以调用的是PetShop.SQLServerDAL.Product.GetProductByCatagory方法,而不是PetShop.OracleDAL.Product.GetProductByCatagory.关于这个以后再来讨论.
说到这里,我想我对他的如何调用都熟悉了.

原文地址:https://www.cnblogs.com/confach/p/112355.html