对于Microsoft .net PetShop程序中的购物车和订单处理模块,文中主要分析两种技术的应用:
1. Profile技术在PetShop程序中用于三处:
1) 购物车ShoppingCart -下面的例子围绕购物车流程进行
2) 收藏WishList
3) 用户信息AccountInfo
注册新用户 NewUser.aspx :使用的是CreateUserWizard 控件,基于MemberShip机制,在数据库MSPetShop4Services的表aspnet_Users中创建用户<o:p></o:p>
修改用户注册信息 UserProfile.aspx: 基于Profile技术,在数据库MSPetShop4Profile的表Profiles和Account中创建用户信息
2. 异步消息处理技术运用于订单处理
4.1 Web.config配置
Profile可以利用数据库存储关于用户的个性化信息,有点象session对象,但session对象是有生存期的,在生存期后,session对象自动失效了。而profile不同,除非显式移除它。要实现profile功能,必须先在web.config中进行定义。<o:p></o:p>
在web.congfig中,将会定义一些属性/值,分别存贮将要保存的变量和值,比如language属性,定义其值是string类型,如此类推。而<group>标签,则是将一些相同或类似功能的变量值放在一起。<o:p></o:p>
程序中使用方法:Profile.language = ddlLanguage.SelectedItem.Value;<o:p></o:p>
<o:p></o:p>
<profile automaticSaveEnabled="false" defaultProvider="ShoppingCartProvider"><o:p></o:p>
<providers><o:p></o:p>
<add name="ShoppingCartProvider" connectionStringName="SQLProfileConnString" type="PetShop.Profile.PetShopProfileProvider" applicationName=".NET Pet Shop 4.0"/><o:p></o:p>
<add name="WishListProvider" connectionStringName="SQLProfileConnString" type="PetShop.Profile.PetShopProfileProvider" applicationName=".NET Pet Shop 4.0"/><o:p></o:p>
<add name="AccountInfoProvider" connectionStringName="SQLProfileConnString" type="PetShop.Profile.PetShopProfileProvider" applicationName=".NET Pet Shop 4.0"/><o:p></o:p>
</providers><o:p></o:p>
<properties><o:p></o:p>
<add name="ShoppingCart" type="PetShop.BLL.Cart" allowAnonymous="true" provider="ShoppingCartProvider"/><o:p></o:p>
<add name="WishList" type="PetShop.BLL.Cart" allowAnonymous="true" provider="WishListProvider"/><o:p></o:p>
<add name="AccountInfo" type="PetShop.Model.AddressInfo" allowAnonymous="false" provider="AccountInfoProvider"/><o:p></o:p>
</properties><o:p></o:p>
</profile><o:p></o:p>
4.2 购物车程序流程-Profile技术<o:p></o:p>
1. 点击“加入购物车”: http://localhost:2327/Web/ShoppingCart.aspx?addItem=EST-34
2. ShoppingCart.aspx文件处理:在init方法之前处理<o:p></o:p>
protected void Page_PreInit(object sender, EventArgs e) {<o:p></o:p>
if (!IsPostBack) {<o:p></o:p>
string itemId = Request.QueryString["addItem"];<o:p></o:p>
if (!string.IsNullOrEmpty(itemId)) {<o:p></o:p>
Profile.ShoppingCart.Add(itemId); //注意ShoppingCart的类型是PetShop.BLL.Cart<o:p></o:p>
//Save 方法将修改后的配置文件属性值写入到数据源,如ShoppingCart属性已经改变<o:p></o:p>
Profile.Save(); <o:p></o:p>
<o:p></o:p>
// Redirect to prevent duplictations in the cart if user hits "Refresh"<o:p></o:p>
//防止刷新造成 多次提交<o:p></o:p>
Response.Redirect("~/ShoppingCart.aspx", true); //将客户端重定向到新的 URL。指定新的 URL 并指定当前页的执行是否应终止。<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
3. PetShop.BLL.Cart类<o:p></o:p>
// Dictionary: key/value <o:p></o:p>
private Dictionary<string, CartItemInfo> cartItems = new Dictionary<string, CartItemInfo>();<o:p></o:p>
<o:p></o:p>
/// <summary><o:p></o:p>
/// Add an item to the cart.<o:p></o:p>
/// When ItemId to be added has already existed, this method will update the quantity instead.<o:p></o:p>
/// </summary><o:p></o:p>
/// <param name="itemId">Item Id of item to add</param><o:p></o:p>
public void Add(string itemId) {<o:p></o:p>
CartItemInfo cartItem;<o:p></o:p>
//获取与指定的键相关联的值TryGetValue(TKey key,out TValue value)<o:p></o:p>
if (!cartItems.TryGetValue(itemId, out cartItem)) {<o:p></o:p>
Item item = new Item();<o:p></o:p>
ItemInfo data = item.GetItem(itemId);<o:p></o:p>
if (data != null) {<o:p></o:p>
CartItemInfo newItem = new CartItemInfo(itemId, data.ProductName, 1, (decimal)data.Price, data.Name, data.CategoryId, data.ProductId);<o:p></o:p>
cartItems.Add(itemId, newItem);<o:p></o:p>
}<o:p></o:p>
}<o:p></o:p>
else<o:p></o:p>
cartItem.Quantity++;<o:p></o:p>
}<o:p></o:p>
<o:p></o:p>
4. 更新Profile<o:p></o:p>
//Save 方法将修改后的配置文件属性值写入到数据源,如ShoppingCart属性已经改变<o:p></o:p>
Profile.Save(); <o:p></o:p>
如何更新:<o:p></o:p>
根据配置中的ShoppingCartProvider类型 PetShop.Profile.PetShopProfileProvider。<o:p></o:p>
<o:p></o:p>
ASP.NET 配置文件提供对用户特定属性的持久性存储和检索。配置文件属性值和信息按照由 ProfileProvider 实现确定的方式存储在数据源中。
每个用户配置文件在数据库的 Profiles 表中进行唯一标识。该表包含配置文件信息,如应用程序名称和上次活动日期。
CREATE TABLE Profiles<o:p></o:p>
(<o:p></o:p>
UniqueID AutoIncrement NOT NULL PRIMARY KEY,<o:p></o:p>
Username Text (255) NOT NULL,<o:p></o:p>
ApplicationName Text (255) NOT NULL,<o:p></o:p>
IsAnonymous YesNo, <o:p></o:p>
LastActivityDate DateTime,<o:p></o:p>
LastUpdatedDate DateTime,<o:p></o:p>
CONSTRAINT PKProfiles UNIQUE (Username, ApplicationName)<o:p></o:p>
)<o:p></o:p>
<o:p></o:p>
5. PetShop.Profile. PetShopProfileProvider类, 继承自ProfileProvider<o:p></o:p>
// 创建 PetShop.SQLProfileDAL.PetShopProfileProvider类-数据库操作<o:p></o:p>
private static readonly IPetShopProfileProvider dal <o:p></o:p>
= DataAccess.CreatePetShopProfileProvider();<o:p></o:p>
<o:p></o:p>
/// <summary><o:p></o:p>
/// 设置指定的属性设置组的值<o:p></o:p>
/// </summary><o:p></o:p>
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) {<o:p></o:p>
string username = (string)context["UserName"];<o:p></o:p>
CheckUserName(username); <o:p></o:p>
bool isAuthenticated = (bool)context["IsAuthenticated"];<o:p></o:p>
<o:p></o:p>
int uniqueID = dal.GetUniqueID(username, isAuthenticated, false, ApplicationName);<o:p></o:p>
if(uniqueID == 0)<o:p></o:p>
uniqueID = dal.CreateProfileForUser(username, isAuthenticated, ApplicationName);<o:p></o:p>
<o:p></o:p>
foreach(SettingsPropertyValue pv in collection) {<o:p></o:p>
if(pv.PropertyValue != null) {<o:p></o:p>
switch(pv.Property.Name) {<o:p></o:p>
case PROFILE_SHOPPINGCART: //ShoppingCart<o:p></o:p>
SetCartItems(uniqueID, (Cart)pv.PropertyValue, true);<o:p></o:p>
break;<o:p></o:p>
case PROFILE_WISHLIST:<o:p></o:p>
SetCartItems(uniqueID, (Cart)pv.PropertyValue, false);<o:p></o:p>
break;<o:p></o:p>
case PROFILE_ACCOUNT:<o:p></o:p>
if(isAuthenticated)<o:p></o:p>