选择显示字体大小

开发btoc电子商务系统(asp.net) 


  原文还有两个图
在对asp.net web表单的编程模型有了基本的认识后,通过应用于现实的开发案例来提高对asp.net web表单内在运作机制的了解,以及由此带来的对系统架构的掌控是很有必要的。 我们没有为编程而编程的高贵姿态,我们深深懂得能够开发出高效,健壮,强大的应用程序始终是编程的终极。我们下面通过一个完整的btoc电子商务系统的开发流程来展示asp.net web表单是怎样具体搭建面向下一代网络平台的。
这是一个典型的基于b/s(浏览器/服务器) 三层架构的食品,饮料电子商务零售系统——“玉米地零食店”。前端为产品浏览器,为消费者提供浏览/选购商品,下订单购物等各个环节的功能;中间层为销售商的税率,优惠等商务逻辑;后端为与整个零售系统相关的产品,顾客,订单等数据库。我们采用asp.net+iis 5来构建前端和中间层,sql server 2000来管理后端数据库,整个系统运行于windows xp。相关硬件配置只要满足上述软件的基本配置,系统性能便可保证。下面为该网上零售系统的前端界面图示:

在编制web 表单商业前端和中间层之前,我们有必要对后端数据库做一个简单的介绍。后端数据库 cornfieldgrocer 由4个表组成:产品类别表categories ,产品细节表 details ,产品表 products ,客户信息表customers。考虑到演示系统的的简洁性,我们没有添加相关的存储过程视图,规则等,这些在实际的系统的开发中对提高系统的性能是很有必要,尤其是在大数据量的情况下。下面为4个表的字段的图示介绍:

各个表的字段的表义已经相当清楚,我们不在这里赘述。我们下面向大家展示一下整个电子商务零售系统——“玉米地零食店”的物理文件组成及其结构,下图为示意图:

所有的文件位于asp.net站点目录cornfieldgrocer下,其中还有web表单页面用到的图片子目录images下的文件就不再在这里列出了。
下面我们开始编写前端和中间层代码,为了更清楚地展示web form asp.net的底层代码构造,我们采用记事本来完成整个代码的编写过程。需要说明的是在真正的工程项目开发实践中,如能借助visual studio.net等可视集成开发工具,开发效率会大大提高。但在asp.net代码的底层机制没有谙熟的情况下,笔者强烈建议初期的开发学习不妨放在windows系统自带的“记事本”这一简单却能够把代码暴露得相当清晰的工具里。
由于篇幅有限,我们不可能将所有的代码都在这里展示给大家。如前所述,web.config为每个站点级的基于xml的配置文件,负责一些asp.net安全认证,编码选择,诊断测试asp.net的配置工作,为浏览器请求asp.net web表单时通过 iis处理后的第一站。下面为其内容:

<configuration>
<system.web>
<globalization requestencoding=&quot;utf-8&quot; responseencoding=&quot;utf-8&quot; />
</system.web>
</configuration>


容易看到这里的配置内容相当简单,仅指定请求/发送的编码为“utf-8”。我们对此不再赘述。
global.asax文件及其由后端代码文件global.asax.cs编译成的bin\cornfieldgrocer.dll共同组成该网上零售系统的asp.net应用程序定义。我们先来看文件global.asax:

<%@ application inherits=&quot;cornfieldgrocer.global&quot; %>

该文件只有一行指示符,它表示asp.net应用程序的定义继承自global类,而global类正是在global.asax.cs文件中定义:

using system;
using system.collections;
using system.componentmodel;
using system.web;
using system.web.sessionstate;

namespace cornfieldgrocer
{
public class global : system.web.httpapplication
{
protected void session_start(object sender, eventargs e)
{
if (session[&quot;shoppingcart&quot;] == null)
{
session[&quot;shoppingcart&quot;] = new cornfieldgrocer.orderlist();
}
}
}
}

在global类里,我们定义了区段(session)意义下的购物卡(shoppingcart)——这里采用了c#中的索引器。购物卡的类型为cornfieldgrocer命名空间中的orderlist类,在cornfieldgrocer.cs文件中有定义。我们当然也可以在global.asax文件中用脚本语言的形式将上面两个文件的内容合并起来,但那不是asp.net推荐的做法,因为脚本语言的第一次执行还要进行动态编译,这回损失一部分性能,而将cs文件提前编译成dll文件则会降低这种代价——当然这里的编译的意思还是指将cs的源代码文件编译成微软中间语言的过程。其次,页面与后端代码分离的原则易于项目管理,是visual studio.net推荐的工程性的做法。
文件default.aspx为整个网上零售系统的前端页面html代码,default.aspx.cs为其后端控制web表单行为的cs代码。由于篇幅关系我们这里不再赘述其html代码,实际上从前面给出的前端界面图示,我们可以基本了解default.aspx的html代码结构。style.css文件为default.aspx文件的页面样式定义文件,定义一些页面元素的颜色,格式,间距等修饰性的东西,我们也不再多言。下面只向大家展示default.aspx的页面指示符:

<%@ autoeventwireup=&quot;false&quot; inherits=&quot;cornfieldgrocer.mainform&quot; %>

我们用“inherits=&quot;cornfieldgrocer.mainform&quot;”来表示我们的页面继承自mainform类,这样我们就实现了对asp.net web 表单行为的控制代码与页面显示的html的分离。其中“autoeventwireup=&quot;false&quot;”表示页面事件非自动使能——页面事件非自动使能的意思是所有页面事件必须经过用户明确的操作才能触发,由于该属性缺省为“true”表示自动使能,但我们的商业逻辑要求非自动使能,故这里的语句很有必要,否则会引起系统处理的混乱。下面我们来看mainform类:

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace cornfieldgrocer
{
public class mainform: system.web.ui.page
{
protected system.web.ui.webcontrols.label currentcategory;
protected system.web.ui.webcontrols.label name;
protected system.web.ui.webcontrols.label subtotal;
protected system.web.ui.webcontrols.imagebutton imagebutton1;
protected system.web.ui.webcontrols.label description;
protected system.web.ui.webcontrols.label company;
protected system.web.ui.webcontrols.repeater detailslisting;
protected system.web.ui.webcontrols.datalist productlisting;
protected system.web.ui.webcontrols.datalist shoppingcartlist;
protected system.web.ui.htmlcontrols.htmlselect categorylist;
protected system.web.ui.webcontrols.button btnselect;
protected system.web.ui.webcontrols.label tax;
protected system.web.ui.webcontrols.label total;
protected system.web.ui.webcontrols.imagebutton imagebutton4;
protected system.web.ui.webcontrols.imagebutton imagebutton5;
protected system.web.ui.webcontrols.imagebutton imagebutton6;
protected system.web.ui.htmlcontrols.htmlinputtext qty;
protected system.web.ui.htmlcontrols.htmlgenericcontrol checkoutpanel;
protected system.web.ui.htmlcontrols.htmlimage selectedprodpicture;

public mainform()
{
page.init += new system.eventhandler(page_init);
}
private void page_load(object sender, system.eventargs e)
{
if (!ispostback)
{
productlisting.selectedindex = 0;

updateproducts();
updateshoppingcart();
}
}
private void page_init(object sender, eventargs e)
{
initializecomponent();
}
private void initializecomponent()
{
this.btnselect.click +=
new system.eventhandler(this.categorylist_select);
this.productlisting.selectedindexchanged+=
new system.eventhandler(this.productlisting_select);
this.imagebutton1.click+=
new imageclickeventhandler(this.addbtn_click);
this.imagebutton4.click+=
new imageclickeventhandler(this.recalculate_click);
this.imagebutton6.click+=
new imageclickeventhandler(this.clearcart_click);
this.load +=
new system.eventhandler(this.page_load);
}
private void categorylist_select(object sender, eventargs e)
{
currentcategory.text =
categorylist.items[categorylist.selectedindex].text;
updateproducts();
}
private void productlisting_select(object sender, eventargs e)
{
updateproducts();
}
private void addbtn_click(object sender, imageclickeventargs e)
{
int productid = int32.parse
(productlisting.datakeys[productlisting.selectedindex].tostring());

inventorydb market = new inventorydb();
datarow product = market.getproduct(productid);

cornfieldgrocer.orderlist shoppingcart =
((cornfieldgrocer.orderlist) session[&quot;shoppingcart&quot;]);

shoppingcart.add(new cornfieldgrocer.orderitem(productid,
(string) product[&quot;productname&quot;],
double.parse(product[&quot;unitprice&quot;].tostring()), 1));

updateshoppingcart();
}
private void recalculate_click(object sender, imageclickeventargs e)
{
cornfieldgrocer.orderlist shoppingcart =
((cornfieldgrocer.orderlist) session[&quot;shoppingcart&quot;]);

for (int i=0; i<shoppingcartlist.items.count; i++) >
{
htmlinputtext qty =
(htmlinputtext) shoppingcartlist.items[i].findcontrol(&quot;qty&quot;);
try
{
shoppingcart[(string) shoppingcartlist.datakeys][i]].quantity
= int32.parse(qty.value);
}
catch (exception)
{
}
}
updateshoppingcart();
}
private void clearcart_click(object sender, imageclickeventargs e)
{
cornfieldgrocer.orderlist shoppingcart =
((cornfieldgrocer.orderlist) session[&quot;shoppingcart&quot;]);

shoppingcart.clearcart();
updateshoppingcart();
}
void updateproducts()
{
inventorydb market = new inventorydb();

int categoryid = int32.parse
(categorylist.items[categorylist.selectedindex].value);
productlisting.datasource =
market.getproducts(categoryid).defaultview;
productlisting.databind();

int productid = int32.parse
(productlisting.datakeys[productlisting.selectedindex].tostring());
datarow product = market.getproduct(productid);

name.text = product[&quot;productname&quot;].tostring();
selectedprodpicture.src = product[&quot;imagepath&quot;].tostring();
description.text = product[&quot;productdescription&quot;].tostring();
company.text = product[&quot;manufacturer&quot;].tostring();

detailslisting.datasource =
market.getproductcalories(productid).defaultview;
detailslisting.databind();
}
void updateshoppingcart()
{
cornfieldgrocer.orderlist shoppingcart =
((cornfieldgrocer.orderlist) session[&quot;shoppingcart&quot;]);

subtotal.text = string.format(&quot;{0:c}&quot;, shoppingcart.subtotal);
tax.text = string.format(&quot;{0:c}&quot;, shoppingcart.tax);
total.text = string.format(&quot;{0:c}&quot;, shoppingcart.total);

shoppingcartlist.datasource=shoppingcart.values;
shoppingcartlist.databind();
}
}
}
mainform类中共有11个方法,19个保护域。其中的19个保护域和前面给出的前端界面图示的页面元素相对应,这里不再赘述。11个方法中mainform()为构建器,其添加了页面初始化事件page_init(),这是asp.net web表单最先处理的事件,一般进行一些基础的初始化操作。我们可以看到在page_init()中进行了初始化组件initializecomponent()的操作。page_load()事件出现在用户发出请求后,页面装载的时候,在这里一般可做一些商业逻辑初始化方面的操作,比如数据库的连接,购物卡的初始化等。我们这里进行了产品展示updateproducts()和购物卡的初始化updateshoppingcart()的操作。
其他四个方法分别为产品类别的选择productlisting_select(),购买产品的添加addbtn_click(),购物卡的重新计算recalculate_click(),购物卡的清除clearcart_click()都是通过对asp.net控件的操作来触发相应的事件完成商业逻辑。上面的代码已经展示的相当清楚,我们不再赘述。
最后我们要向大家说明的是中间层商务逻辑的组件。它由三个类构成:库存数据类inventorydb,订单项目类orderitem和订单列表类orderlist。它们共同在文件cornfieldgrocer.cs文件中定义。自解释的编程方式已经它们的结构展示的相当清除,我们下面只给出该文件的cs源代码:

using system;
using system.data;
using system.data.sqlclient;
using system.collections;

namespace cornfieldgrocer
{
public class inventorydb
{
public datatable getproducts(int categoryid)
{
sqlconnection sqlconnect= new sqlconnection
(&quot;server=(local);database=cornfieldgrocer;trusted_connection=yes&quot;);
sqldataadapter sqladapter1 = new sqldataadapter
(&quot;select * from products where categoryid=&quot;+categoryid,sqlconnect);
dataset products = new dataset();
sqladapter1.fill(products, &quot;products&quot;);
return products.tables[0];
}
public datarow getproduct(int productid)
{
sqlconnection sqlconnect= new sqlconnection
(&quot;server=(local);database=cornfieldgrocer;trusted_connection=yes&quot;);
sqldataadapter sqladapter1 = new sqldataadapter
(&quot;select * from products where productid=&quot; + productid, sqlconnect);
dataset product = new dataset();
sqladapter1.fill(product, &quot;product&quot;);
return product.tables[0].rows[0];
}
public datatable getproductcalories(int productid)
{
sqlconnection sqlconnect = new sqlconnection
(&quot;server=(local);database=cornfieldgrocer;trusted_connection=yes&quot;);
sqldataadapter sqladapter1 = new sqldataadapter
(&quot;select * from details where productid=&quot;+productid,sqlconnect);
dataset details = new dataset();
sqladapter1.fill(details, &quot;details&quot;);
return details.tables[0];
}
}

public class orderitem
{
public int productid;
public int quantity;
public string name;
public double price;
public orderitem(int productid, string name, double price, int quantity)
{
this.productid = productid;
this.quantity = quantity;
this.name = name;
this.price = price;
}
public int productid
{
get { return productid; }
}
public int quantity
{
get { return quantity; }
set { quantity=value; }
}
public string name
{
get { return name; }
}
public double price
{
get { return price; }
}
public double total
{
get { return quantity * price; }
}
}

public class orderlist
{
private hashtable orders = new hashtable();
private double taxrate = 0.08;
public double subtotal
{
get
{
if (orders.count == 0)
return 0.0;
double subtotal = 0;
ienumerator items = orders.values.getenumerator();
while(items.movenext())
{
subtotal += ((orderitem) items.current).price *
((orderitem) items.current).quantity;
}
return subtotal;
}
}
public double taxrate
{
get { return taxrate; }
set { taxrate = value; }
}
public double tax
{
get { return subtotal * taxrate; }
}
public double total
{
get { return subtotal * (1 + taxrate); }
}
public icollection values {
get {
return orders.values;
}
}
public orderitem this[string name] {
get {
return (orderitem) orders[name];
}
}
public void add(orderitem value)
{
if (orders[value.name] == null) {
orders.add(value.name, value);
}
else
{
orderitem oi = (orderitem)orders[value.name];
oi.quantity = oi.quantity + 1;
}
}
public void clearcart() {
orders.clear();
}
}
}

需要说明的是我们将三个文件cornfieldgrocer.cs,default.aspx.cs,global.asax.cs用编译命令“csc /t:library /out:cornfieldgrocer.dll cornfieldgrocer.cs default.aspx.cs global.asax.cs”将它们全部封装在cornfieldgrocer命名空间里,虽然这并不是必须的。上面的编译器输出cornfieldgrocer.dll文件,我们配置该网上零售站点时只需将该文件拷贝到站点根目录中的bin目录下即可。
到此为止,我们已经完整的向大家展示了利用asp.net web表单建立一个小型的网上交易系统的编码,配置等工作。当然作为演示案例,它还没有真正系统的完善的性能,安全,界面等各个方面的优化考虑和设计。但它向我们展示的asp.net web表单模型却非常典型且底层,大家不防在此基础上通过不断的修改和扩充来开发适合自己的交易系统。比如对于default.aspx文件中autoeventwireup=&quot;false&quot;如果设置为“true”或去掉这个语句,在运行页面时会出现什么情况?通过这些练习便会不断的加深我们对asp.net底层的认识,最后达到游刃有余的把握。实际上技术的学习,尤其是编程,除了一定的兴趣和悟性外,大量代码实例的锻炼也是很有必要的,这本身就是笔者成长的一个过程,也是本文中笔者竭力要给大家展示的。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons