代码阅读总结之ASP.NET StartKit Commerce

ASP.NET StartKit Commerce简单,容易理解。
我认为是初次学习.NET代码的首选,不怕各位笑话,我曾经完整阅读该项目代码3次。
那么,通过阅读我们能学习到什么知识呢?请看我下面的总结:


1。多层结构的实现
依我见是2层结构:PL层和BLL层(没有明显的DAL层,DAL和BLL共同组成BLL层)。但是我们可以学习到Db过程的调用方法。

2。Web服务的简单使用

3。Web用户控件的使用

4。数据绑定的相关知识
让我们先看该项目中的一段代码:
<asp:HyperLink cssclass="MenuSelected" id="HyperLink2" Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>' NavigateUrl='<%# "productslist.aspx?CategoryID=" + DataBinder.Eval(Container.DataItem, "CategoryID") + "&selection=" +

Container.ItemIndex %>' runat="server" />

说明:
(1)数据绑定用单引号,单引号中的字符用双引号。
(2)特别注意属性:Container.ItemIndex,它生成的是每一项的ID,它是从零开始的。功能类似MS SQL的IDENTITY(0,1).想想我以前的项目为了展现每一行的索引,往往利用在DB中创建临时表生成行索引,再绑定数据的做法真愚。


5。基于窗体的验证
在Web.config文件中,我们可以看到如下2段代码:
<authentication mode="Forms">
            <forms name="CommerceAuth" loginUrl="login.aspx" protection="All" path="/" />
</authentication>

<location path="OrderDetails.aspx">
        <system.web>
            <authorization>
                <deny users="?" />
            </authorization>
        </system.web>
</location>

说明:这样就禁止未通过登陆验证的用户访问页面:OrderDetails.aspx,自动将未登陆用户引导到页面login.aspx进行登陆验证

6.购物车的实现
购物车是电子商务站的典型功能。在此项目中我们可以看到购物车的增加,删除和修改功能的典型操作。同时也学习控件的一些经典使用方法

让我们先看代码:
<asp:DataGrid id=MyList runat="server" BorderColor="black" GridLines="Vertical" cellpadding="4" cellspacing="0"

DataKeyField="Quantity" AutoGenerateColumns="false">
......
</asp:DataGrid>


利用DataKeyField属性绑定数据源的字段Quantity,也就是此项目中每个商品的购买数量。
类System.Web.UI.WebControls.BaseDataList的属性DataKeyField是获取或设置由 DataSource 属性指定的数据源中的键字段。
同时DataGrid和DataList都是BaseDataList的子类。

让我们再看下面代码:此代码是购物车的修改方法实现。

void UpdateShoppingCartDatabase() {

            ASPNET.StarterKit.Commerce.ShoppingCartDB cart 
= new ASPNET.StarterKit.Commerce.ShoppingCartDB();

            
// Obtain current user's shopping cart ID
            String cartId = cart.GetShoppingCartId();

            
// Iterate through all rows within shopping cart list
            for (int i=0; i < MyList.Items.Count; i++{

                
// Obtain references to row's controls
                TextBox quantityTxt = (TextBox) MyList.Items[i].FindControl("Quantity");
                CheckBox remove 
= (CheckBox) MyList.Items[i].FindControl("Remove");

                
// Wrap in try/catch block to catch errors in the event that someone types in
                
// an invalid value for quantity
                int quantity;
                
try {
                    quantity 
= Int32.Parse(quantityTxt.Text);

                    
// If the quantity field is changed or delete is checked
                    if (quantity != (int)MyList.DataKeys[i] || remove.Checked == true{

                        Label lblProductID 
= (Label) MyList.Items[i].FindControl("ProductID");

                        
if (quantity == 0 || remove.Checked == true{
                            cart.RemoveItem(cartId, Int32.Parse(lblProductID.Text));
                        }

                        
else {
                            cart.UpdateItem(cartId, Int32.Parse(lblProductID.Text),quantity);
                        }

                    }

                }

                
catch {
                    MyError.Text 
= "There has been a problem with one or more of your inputs.";
                }

            }

        }


说明:
(1)我们利用(int)MyList.DataKeys[i]取出绑定在每一行的商品购买数量值
(2)利用方法FindControl()在当前的命名容器中搜索指定的服务器控件。


7。ms sql中定时任务的实现
利用MS SQL的JOB功能实现在特定时间时自动运行过程ShoppingCartRemoveAbandoned,以删除购物车表(CMRC_ShoppingCart)中的多余数据。

8。全球化的相关知识
见Global.asax.cs中代码:

protected void Application_BeginRequest(Object sender, EventArgs e) 
        
{
               
            
try
            
{
                
if (Request.UserLanguages != null)
                    Thread.CurrentThread.CurrentCulture 
= 

CultureInfo.CreateSpecificCulture(Request.UserLanguages[
0]);
                
else
                    
// Default to English if there are no user languages
                    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

                Thread.CurrentThread.CurrentUICulture 
= Thread.CurrentThread.CurrentCulture;
            }

            
catch (Exception ex)
            
{
                Thread.CurrentThread.CurrentCulture 
= new CultureInfo("en-us");
            }

            
        }

说明:全球化是为多种文化用户支持地方化用户界面和地方性数据应用程序的设计和开发过程。在.NET 构架中,CultureInfo 类体现了一个特

定文化的信息。这个信息包括书写系统、所使用的历法、日期和时间格式风格、数字和货币风格以及分类原则。地方化是为应用程序支持的每

种文化将应用程序资源转变为本地化版本的过程。当前文化在每次用户向应用程序请求页面时根据浏览器设置修改。


9。其他知识点:
(1)缓存的使用
(2)Web.config文件中自定义节点的设置和读取
等等。


本人第一次写阅读总结,由于水平有效可能有误,希望大家多多指出。谢谢。
下次将分类总结ASP.NET StartKit TimeTracker的阅读心得。

 

posted @ 2004-12-20 16:47  aierong  阅读(2563)  评论(1编辑  收藏  举报