petshop学习笔记(5)Items.aspx分析

  Items.aspx页,使用了一个用户自定义控件<%@ Register Src="Controls/ItemsControl.ascx" TagName="ItemsControl" TagPrefix="PetShopControl" %>,还有个Items.aspx.cs页,但Items.aspx.cs页面相对简单,就只是一个在加载此页时 Page.Title = WebUtility.GetProductName(Request.QueryString["productId"]);意时就是页面的Title为WebUtitley这个业务逻辑所能读到的产品名称。

我看来看一个WebUtlity.GetProductName这个方法  

public static string GetProductName(string productId) {  

            Product product = new Product();

            if (!enableCaching)

                return product.GetProduct(productId).Name;  

            string cacheKey = string.Format(PRODUCT_NAME_KEY, productId);

 

            // Check if the data exists in the data cache

            string data = (string)HttpRuntime.Cache[cacheKey];

 

            if (data == null) {

                // Caching duration from Web.config

                int cacheDuration = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);

 

                // If the data is not in the cache then fetch the data from the business logic tier

                data = product.GetProduct(productId).Name;

 

                // Create a AggregateCacheDependency object from the factory

                AggregateCacheDependency cd = DependencyFacade.GetProductDependency();

 

                // Store the output in the data cache, and Add the necessary AggregateCacheDependency object

                HttpRuntime.Cache.Add(cacheKey, data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);

 

            }

 

            return data;

        }

这个方法之前的分析中也有分析过,方便加深印像,再写多一次吧!之前分析了三层结构,这里也是沿用了这个思想,当前这里属于表示层,这个层只属要调用业务逻辑层的逻辑能力就行了,其他的数据处理也就丢给数据处理层去处理也就行了。

Product 为业务逻辑层,他只处理给他的东西就行了。跳转到这个方法里再看看。GetProduct方法反回的是什么东西?返回的是个ProductInfo实体类哦。那么只要得到这个实体类的Name属性就能得到data的值了!然后将这个值添加到缓存中,如果缓存中有这个值那么也就直接返回一个缓存值。

public ProductInfo GetProduct(string productId) {

             

              // Return empty product if the string is empty

              if(string.IsNullOrEmpty(productId))

                   return new ProductInfo();

 

              // Get the product from the data store

              return dal.GetProduct(productId);

         }

 

Product 是如何去读取数据库的值,那也就交给了数据处理的那一层做了,  private static readonly IProduct dal = PetShop.DALFactory.DataAccess.CreateProduct();定义了dal的一个IProduct 接口,这个值又是使用工厂DALFactory的DataAccess类进行CreateProduct得到的一个接口,这里应该也是个创建接口值,这个值来自配置文件的一个反射值。那么使用这个接口的GetProduct方法,由于在配置文件读取到了这个dal为PetShop.SQLServerDAL,那么返回的就是一个PetShop.SQLServerDAL.GetProduct(productId)类型为ProductInfo的一个值了那PetShop.SQLServerDAL.GetProduct到底得到什么值,只要去到这个项目里的类就可以看到了。

 

那么这个Items.aspx这个页面文件分析完后再来分析ItemsControl.ascx,这个才是读取最终数据的东西。页面文件

<%@ Register TagPrefix="PetShopControl" Namespace="PetShop.Web" %> 创建标记前缀和自定义控件之间的关联,这为开发人员提供了一种在 ASP.NET 应用程序文件(包括网页、用户控件和母版页)中引用自定义控件的简明方法。namespace 正在注册的自定义控件的命名空间。 这里的自定义控件是PetShop.Web

  看看下面代码<PetShopControl:CustomGrid ID="itemsGrid" runat="server" EmptyText="No items found." OnPageIndexChanged="PageChanged" PageSize="2">CustomGird这是个自定义的一个控件,打开App_Code文件夹下的CustomGrid.cs可以看到这个是个继承了Repeater的一个控件,所以也就有了Repeater的全部功能了。还有来到2.0数据的绑定用成了<%# Eval("Name") %>在1.1中是要全部写下去的,感觉很长。这里简化了很多啊。

   先看看ItemsControl.ascx.cs页: 

  protected void PageChanged(object sender, DataGridPageChangedEventArgs e) {

            //reset index

            itemsGrid.CurrentPageIndex = e.NewPageIndex;

 

            //get category id

            string productKey = Request.QueryString["productId"];

 

            //bind data           

            Item item = new Item();

            itemsGrid.DataSource = item.GetItemsByProduct(productKey);

            itemsGrid.DataBind();

 

        }

 

   看到这个页面时,大家应该都会想,怎么这个页没有Page_Load页去初始化读数据绑定?  PageChanged的这个方法主要是用在于分页上的,itemsGrid.CurrentPageIndex为设置当前页的索引为传进来新业的索引,定义字条串productKey为参数productId的值,然后实例化一个item,然后itemsGrid这一控件的数据源为item方法GetItemsByProduct的值。最终绑定数据.

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值