学习PetShop3.0(2)宠物展示

宠物展示,也就是商品展示(汗……)
涉及到这个主题的页有Category.aspx / Items.aspx / ItemDetails.aspx,分别是大类/小类/详细信息这三个。下面来一个一个的分析
要注意的是,像第一篇说的,数据的传递都是采用直接传递业务实体的方法来完成。这样是不是有很强的面向对象的味道?
Category.aspx
该页面的主体是一个用户自定义控件:SimplePager,该空间继承自Repeater,作用当然是存放宠物信息,他的数据源的获得方式使用了.net的缓存api。具体实现代码为:
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;
}
// Bind the data to the control
products.DataBind();
是使用了很普遍的做法来完成的,新数据的通过PetShop.BLL.Product.GetProductsByCategory方法获得,梢后分析这个方法。SimplePager获得数据后,就像普通的Repeater一样根据模板的设定来显示数据。对SimplePager的分析同样放在后面。
用户点击具体的小类后,就进入小类的页面,通过url传递小类的编号。
Items.aspx 
和上面的页面基本一样,因为都是对类别进行的操作嘛。
选择具体的宠物后,就进入详细资料展示页面了。
ItemDetails.aspx
这个页面没有使用缓存,而是直接通过业务逻辑层到数据库去取记录,然后返回到表示层。很简单,一看就懂的那种。
下面来看一下SimplePager控件。
控件的定义中重写了很多Repeater的方法,其用意主要是为了是其具有分页的功能,看一下其中的Render方法。
PetShop.Web.Controls.SimplePager : Repeater 
override protected void Render(HtmlTextWriter writer) {

//Check there is some data attached
if (ItemCount == 0) {
writer.Write(emptyText);
return;
}

//Mask the query
string query = Context.Request.Url.Query.Replace(COMMA, AMP);
query = RX.Replace(query, string.Empty);

// Write out the first part of the control, the table header
writer.Write(HTML1);

// Call the inherited method
base.Render(writer);

//从这里往下开始是重写的关键部分,用来显示上下页的按钮
// Write out a table row closure
writer.Write(HTML2);

//Determin whether next and previous buttons are required
//Previous button?
if (currentPageIndex > 0)
writer.Write(string.Format(LINK_PREV, (currentPageIndex - 1) + query));

//other .....


}

另外该控件还有自定义的事件PageIndexChanged,用来对页面切换进行响应。DataSource接受实现IList接口的数据,而我们看上面大类页面在显示的时候使用的语句
products.DataSource = productsByCategory;
productsByCategory作为业务逻辑组件一个方法的返回值,正是实现了IList接口的数据。
接下来看该数据是怎么得到的。
基本的过程都是从最后的数据操作组件开始层层的传递过来,因此我们直接看最后的数据操作部分。
PetShop.SQLServerDAL.Product : IProduct
public IList GetProductsByCategory(string category) {

IList productsByCategory = new ArrayList();

SqlParameter parm = new SqlParameter(PARM_CATEGORY, SqlDbType.Char, 10);
parm.Value = category;

//Execute a query to read the products
using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, SQL_SELECT_PRODUCTS_BY_CATEGORY, parm)) {
while (rdr.Read()){
//向ArrayList里添加一整个的ProductInfo实体
ProductInfo product = new ProductInfo(rdr.GetString(0), rdr.GetString(1), null);
productsByCategory.Add(product);
}
}

return productsByCategory;
}

很明显,返回的是一个ArrayList。这个里面放着该大类的所有小类的资料。一个小类就是一个ProductInfo,而ProductInfo作为一个瘦数据类存放小类的一些基本资料。
现在我们在回头看一下在Category.aspx中SimplePager的部分标记代码 
<%# DataBinder.Eval(Container.DataItem, "Id") %>
从数据绑定的知识可以得到,这个id其实是ProductInfo的一个属性。
if(itemsByProduct.Count > 0)
在Items.aspx中进行的数据操作和Category.aspx的基本一样。
Items.aspx.cs里最后有这么一句代码:
productName.Text = ((ItemInfo)itemsByProduct[0]).ProductName;
因为itemsByProduct是一个ArrayList,所以itemsByProduct[0]实际上返回的就是一个ItemInfo。

从上面的分析可以看出,业务逻辑和业务实体分开来的好处。
posted @ 2005-11-03 16:03  torome  阅读(344)  评论(0编辑  收藏  举报