关于PetShop4.0登录的一个问题
这是PetShop 4.0 PetShopProfileProvider.cs里面的一段代码
public override void Initialize(string name, NameValueCollection config) {
if(config == null)
throw new ArgumentNullException( "config ");
if(string.IsNullOrEmpty(config[ "description "])) {
config.Remove( "description ");
config.Add( "description ", "Pet Shop Custom Profile Provider ");
}
if(string.IsNullOrEmpty(name))
name = "PetShopProfileProvider ";
if(config[ "applicationName "] != null && !string.IsNullOrEmpty(config[ "applicationName "].Trim()))
applicationName = config[ "applicationName "];
base.Initialize(name, config);
}
我想问我在登录时: public override void Initialize(string name, NameValueCollection config) 这句当中的:name和config是那里传过来的呀。为什么name的值是ShoppingCartProvider还有NameValueCollection这个类干嘛用的,谢谢了
[解决办法]
自定义的ProfileProvider都是按照微软的ProfileProvider规定的‘格式’来写,具体的调用是asp.net来自动完成
可以参考一下微软的源码:
http://weblogs.asp.net/scottgu/archive/2006/04/13/442772.aspx
[解决办法]
msdn上关于provider的说明
http://msdn2.microsoft.com/en-us/library/aa478948.aspx
[解决办法]
ASP.NET 2.0 配置文件
在 ASP.NET 2.0 中,可跨越多个 Web 应用程序,将用户信息存储到一个名为 Profile 的新服务中。.NET Pet Shop 4 的配置文件实现存储并检索用户的购物车、购物清单和帐户信息。这里的关键的一点是,很多用户会发现,假如为用户会话信息提供一个事务处理、集群安全的存储,这几乎可以完全替换他们对会话对象的使用。默认情况下,配置文件服务将数据序列化为一个 BLOB,存储在数据库中。但是,通过实现您自己的配置文件服务序列化服务可以获得更高的性能。对于 Pet Shop 4,创建了一个配置文件服务的自定义实现来降低序列化开销。
运行情况
• 配置配置文件提供程序。
清单 6. 配置文件提供程序配置
<profile automaticSaveEnabled= "false "
defaultProvider= "ShoppingCartProvider ">
<providers>
<add name= "ShoppingCartProvider "
connectionStringName= "SQLProfileConnString "
type= "PetShop.Profile.PetShopProfileProvider "
applicationName= ".NET Pet Shop 4.0 "/>
<add name= "WishListProvider "
connectionStringName= "SQLProfileConnString "
type= "PetShop.Profile.PetShopProfileProvider "
applicationName= ".NET Pet Shop 4.0 "/>
<add name= "AccountInfoProvider "
connectionStringName= "SQLProfileConnString "
type= "PetShop.Profile.PetShopProfileProvider "
applicationName= ".NET Pet Shop 4.0 "/>
</providers>
<properties>
<add name= "ShoppingCart " type= "PetShop.BLL.Cart "
allowAnonymous= "true " provider= "ShoppingCartProvider "/>
<add name= "WishList " type= "PetShop.BLL.Cart "
allowAnonymous= "true "
provider= "WishListProvider "/>
<add name= "AccountInfo " type= "PetShop.Model.AddressInfo "
allowAnonymous= "false " provider= "AccountInfoProvider "/>
</properties>
</profile>
• 迁移匿名配置文件。
清单 7. 迁移匿名配置文件
// Carry over profile property values from an anonymous to an
// authenticated state
void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs e) {
ProfileCommon anonProfile = Profile.GetProfile(e.AnonymousID);
// Merge anonymous shopping cart items to the authenticated
// shopping cart items
foreach (CartItemInfo cartItem in
anonProfile.ShoppingCart.CartItems)
Profile.ShoppingCart.Add(cartItem);
// Merge anonymous wishlist items to the authenticated wishlist
// items
foreach (CartItemInfo cartItem in anonProfile.WishList.CartItems)
Profile.WishList.Add(cartItem);
// Clean up anonymous profile
ProfileManager.DeleteProfile(e.AnonymousID);
AnonymousIdentificationModule.ClearAnonymousIdentifier();
// Save profile
Profile.Save();
}
清单 8. 购物车
using System;
using System.Collections.Generic;
using PetShop.Model;
namespace PetShop.BLL {
/// <summary>
/// An object to represent a customer 's shopping cart.
/// This class is also used to keep track of customer 's wish list.
/// </summary>
[Serializable]
public class Cart {
// Internal storage for a cart
private Dictionary cartItems =
new Dictionary();
/// <summary>
/// Calculate the total for all the cartItems in the Cart
/// </summary>
public decimal Total {
get {
decimal total = 0;
foreach (CartItemInfo item in cartItems.Values)
total += item.Price * item.Quantity;
return total;
}
}
/// <summary>
/// Update the quantity for item that exists in the cart
/// </summary>
/// Item Id
/// Quantity
public void SetQuantity(string itemId, int qty) {
cartItems[itemId].Quantity = qty;
}
/// <summary>
/// Return the number of unique items in cart
/// </summary>
public int Count {
get { return cartItems.Count; }
}
/// <summary>
/// Add an item to the cart.
/// When ItemId to be added has already existed, this method
/// will update the quantity instead.
/// </summary>
/// Item Id of item to add
public void Add(string itemId) {
CartItemInfo cartItem;
if (!cartItems.TryGetValue(itemId, out cartItem)) {
Item item = new Item();
ItemInfo data = item.GetItem(itemId);
if (data != null) {
CartItemInfo newItem = new CartItemInfo(itemId,
data.ProductName, 1, (decimal)data.Price,
data.Name, data.CategoryId, data.ProductId);
cartItems.Add(itemId, newItem);
}
}
else
cartItem.Quantity++;
}
/// <summary>
/// Add an item to the cart.
/// When ItemId to be added has already existed, this method
/// will update the quantity instead.
/// </summary>
/// Item to add
public void Add(CartItemInfo item) {
CartItemInfo cartItem;
if (!cartItems.TryGetValue(item.ItemId, out cartItem))
cartItems.Add(item.ItemId, item);
else
cartItem.Quantity += item.Quantity;
}
/// <summary>
/// Remove item from the cart based on itemId
/// </summary>
/// ItemId of item to remove
public void Remove(string itemId) {
cartItems.Remove(itemId);
}
/// <summary>
/// Returns all items in the cart. Useful for looping through
/// the cart.
/// </summary>
/// Collection of CartItemInfo
public ICollection CartItems {
get { return cartItems.Values; }
}
/// <summary>
/// Method to convert all cart items to order line items
/// </summary>
/// A new array of order line items
public LineItemInfo[] GetOrderLineItems() {
LineItemInfo[] orderLineItems =
new LineItemInfo[cartItems.Count];
int lineNum = 0;
foreach (CartItemInfo item in cartItems.Values)
orderLineItems[lineNum] = new LineItemInfo(item.ItemId,
item.Name, ++lineNum, item.Quantity, item.Price);
return orderLineItems;
}
/// <summary>
/// Clear the cart
/// </summary>
public void Clear() {
cartItems.Clear();
}
}
}