读书人

profile 兑现购物车 实例(一)(转)

发布时间: 2012-12-26 14:39:29 作者: rapoo

profile 实现购物车 实例(一)(转)

首先要了解什么是Profile,不了解就查下资料跟MSDN吧。

首先建立两个类,商品类跟购物车类。这个购物车类要进行存储。(这里的篇幅可能回很长,希望本着上进的朋友耐心看完,肯定会有收获的。我的代码都是从自己的项目中copy下来的,都是生成成功的代码)。

首先商品类---CartItem.cs。我定义了 商品的特征,id,name,price,imageurl,quntity(ID,名称,价格,图片URL,数量);

?

CartItem.csusing System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// CartItem 是购物车中的购物项
/// </summary>
///
//Serializable含义为CartItem的对象支持序列化,这样可以保存数据库当中
[Serializable]
public class CartItem
{
?public CartItem()
?{
??//
??// TODO: 在此处添加构造函数逻辑
??//
?}

??? public CartItem(Guid id,string name,decimal price,string imageurl)
??? {
??????? this.id = id;
??????? this.name = name;??
??????? this.price = price;
??????? this.imgurl=imageurl;

??? }

??? public CartItem(Guid id,string name,decimal price,int quantity,string imageurl)
??? {
??????? this.id = id;
??????? this.name = name;
??????? this.price = price;
??????? this.quantity = quantity;
??????? this.imgurl = imageurl;
??????? //Url();
??? }

??? Guid id;
??? string name;
??? decimal price;
??? int quantity=1;
??? string imgurl;

??? public string Imgurl
??? {
??????? get { return imgurl; }
??????? set { imgurl = value; }
??? }

?

??? /// <summary>
??? /// 只读的商品唯一编号
??? /// </summary>
??? public Guid Id
??? {
??????? get { return id; }
??????
??? }
??? public string Name
??? {
??????? get { return name; }

??? }
??? public decimal Price
??? {
??????? get { return price; }

??? }


??? public int Quantity
??? {
??????? get { return quantity; }
??????? set { quantity = value; }
??? }


}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// CartItem 是购物车中的购物项
/// </summary>
///
//Serializable含义为CartItem的对象支持序列化,这样可以保存数据库当中
[Serializable]
public class CartItem
{
?public CartItem()
?{
??//
??// TODO: 在此处添加构造函数逻辑
??//
?}

??? public CartItem(Guid id,string name,decimal price,string imageurl)
??? {
??????? this.id = id;
??????? this.name = name;??
??????? this.price = price;
??????? this.imgurl=imageurl;

??? }

??? public CartItem(Guid id,string name,decimal price,int quantity,string imageurl)
??? {
??????? this.id = id;
??????? this.name = name;
??????? this.price = price;
??????? this.quantity = quantity;
??????? this.imgurl = imageurl;
??????? //Url();
??? }

??? Guid id;
??? string name;
??? decimal price;
??? int quantity=1;
??? string imgurl;

??? public string Imgurl
??? {
??????? get { return imgurl; }
??????? set { imgurl = value; }
??? }

?

??? /// <summary>
??? /// 只读的商品唯一编号
??? /// </summary>
??? public Guid Id
??? {
??????? get { return id; }
??????
??? }
??? public string Name
??? {
??????? get { return name; }

??? }
??? public decimal Price
??? {
??????? get { return price; }

??? }


??? public int Quantity
??? {
??????? get { return quantity; }
??????? set { quantity = value; }
??? }

}

?

Cart.csusing System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Collections;
/// <summary>
/// Cart 购物车类
/// </summary>
///

[Serializable]
public class Cart
{
??? Hashtable Items = new Hashtable();

??? /// <summary>
??? /// 返回购物车中商品的集合
??? /// </summary>
??? public ICollection CartItems
??? {
??????? get
??????? {
??????????? return? Items.Values;
??????? }
??? }

??? /// <summary>
??? /// 返回购物车中商品的总价
??? /// </summary>
??? public decimal Total
??? {
??????? get
??????? {
??????????? decimal sum = 0;
??????????? foreach (CartItem item in Items.Values)
??????????? {
??????????????? sum += item.Price * item.Quantity;
??????????? }
??????????? return sum;
??????? }
??? }

?public Cart()
?{
??//
??// TODO: 在此处添加构造函数逻辑
??//
?}

??? /// <summary>
??? /// 添加商品到购物车
??? /// </summary>
??? /// <param name="id">商品编号</param>
??? /// <param name="name">商品名称</param>
??? /// <param name="price">商品价格</param>
??? public void AddItem(Guid id, string name, decimal price,string imageurl)
??? {
??????? //添加商品步骤分析:1检查购物车中是否有该类商品已经存在,如果没有就向购物车增加该商品;2如果有该商品,那么就对已经存在的该商品,数量累加1

??????? CartItem item =(CartItem )Items[id];
??????? if (item == null)
??????? {
??????????? Items.Add(id, new CartItem(id, name, price,imageurl));
??????? }
??????? else
??????? {
??????????? item.Quantity++;
??????????? Items[id] = item;
??????? }

??? }

??? /// <summary>
??? /// 从购物车当中删除商品
??? /// </summary>
??? /// <param name="id">编号</param>
??? public void RemoveItem(Guid id)
??? {
??????? //删除商品:1将存在于购物车当中的商品数量减1;2判断该商品数量是否为0,如果为0,我们将该商品彻底从购物车中删除,否则就更新购物车;
??????? CartItem item = (CartItem)Items[id];
??????? if (item == null)
??????? {
??????????? return;
??????? }

??????? item.Quantity--;
??????? if (item.Quantity == 0)
??????? {
??????????? Items.Remove(id);
??????? }
??????? else
??????? {
??????????? Items[id] = item;
??????? }
??? }

??? /// <summary>
??? /// 将匿名用户的购物车,迁移到实名用户购物车的方法
??? /// </summary>
??? /// <param name="CItem"></param>
??? public void AddCartItem(CartItem CItem)
??? {
??????? CartItem item = (CartItem)Items[CItem.Id];
??????? if (item == null)
??????? {
??????????? Items.Add(CItem.Id, new CartItem(CItem.Id, CItem.Name, CItem.Price, CItem.Quantity, CItem.Imgurl));
??????? }
??????? else
??????? {
??????????? item.Quantity += CItem.Quantity;
??????????? Items[CItem.Id] = item;
??????? }
??? }
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.Collections;
/// <summary>
/// Cart 购物车类
/// </summary>
///

[Serializable]
public class Cart
{
??? Hashtable Items = new Hashtable();

??? /// <summary>
??? /// 返回购物车中商品的集合
??? /// </summary>
??? public ICollection CartItems
??? {
??????? get
??????? {
??????????? return? Items.Values;
??????? }
??? }

??? /// <summary>
??? /// 返回购物车中商品的总价
??? /// </summary>
??? public decimal Total
??? {
??????? get
??????? {
??????????? decimal sum = 0;
??????????? foreach (CartItem item in Items.Values)
??????????? {
??????????????? sum += item.Price * item.Quantity;
??????????? }
??????????? return sum;
??????? }
??? }

?public Cart()
?{
??//
??// TODO: 在此处添加构造函数逻辑
??//
?}

??? /// <summary>
??? /// 添加商品到购物车
??? /// </summary>
??? /// <param name="id">商品编号</param>
??? /// <param name="name">商品名称</param>
??? /// <param name="price">商品价格</param>
??? public void AddItem(Guid id, string name, decimal price,string imageurl)
??? {
??????? //添加商品步骤分析:1检查购物车中是否有该类商品已经存在,如果没有就向购物车增加该商品;2如果有该商品,那么就对已经存在的该商品,数量累加1

??????? CartItem item =(CartItem )Items[id];
??????? if (item == null)
??????? {
??????????? Items.Add(id, new CartItem(id, name, price,imageurl));
??????? }
??????? else
??????? {
??????????? item.Quantity++;
??????????? Items[id] = item;
??????? }

??? }

??? /// <summary>
??? /// 从购物车当中删除商品
??? /// </summary>
??? /// <param name="id">编号</param>
??? public void RemoveItem(Guid id)
??? {
??????? //删除商品:1将存在于购物车当中的商品数量减1;2判断该商品数量是否为0,如果为0,我们将该商品彻底从购物车中删除,否则就更新购物车;
??????? CartItem item = (CartItem)Items[id];
??????? if (item == null)
??????? {
??????????? return;
??????? }

??????? item.Quantity--;
??????? if (item.Quantity == 0)
??????? {
??????????? Items.Remove(id);
??????? }
??????? else
??????? {
??????????? Items[id] = item;
??????? }
??? }

??? /// <summary>
??? /// 将匿名用户的购物车,迁移到实名用户购物车的方法
??? /// </summary>
??? /// <param name="CItem"></param>
??? public void AddCartItem(CartItem CItem)
??? {
??????? CartItem item = (CartItem)Items[CItem.Id];
??????? if (item == null)
??????? {
??????????? Items.Add(CItem.Id, new CartItem(CItem.Id, CItem.Name, CItem.Price, CItem.Quantity, CItem.Imgurl));
??????? }
??????? else
??????? {
??????????? item.Quantity += CItem.Quantity;
??????????? Items[CItem.Id] = item;
??????? }
??? }
}

这两个类简单的不能再简单了。哈希表,就是一个有键值对构成的表。我这里把商品的ID作为了key

接下来配置一下web.config文件,启用文中所说的Profile我们要存储的多项就是 Items 这个对象了。

所以profile节要这样配置

?

web.config

?<system.web>
??<anonymousIdentification enabled="true"/>
??<profile enabled="true" defaultProvider="Myprofile">
???<providers>
????<add name="Myprofile" type="System.Web.Profile.SqlProfileProvider" connectionStringName="AspNetDBConn"/>
???</providers>
???<properties>
????<add name="ShoppingCart" type="Cart" allowAnonymous="true" serializeAs="Binary"/>
???</properties>
??</profile>

</system.web>

注意!!这里的代码为配置文件的一部分---profile的部分

这样我们就能在项目的中的任何地方用

profile 的配置,这里就不在赘述了

profile.ShoppingCart .**** 访问Cart类的属性跟方法了

不能再写了,没了解过profile 的这里先要了解一下。

没问题请看第二篇

读书人网 >编程

热点推荐