?? bookshopprofileprovider.cs
字號:
?using System;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Profile;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
//自定義配置文件提供程序
public sealed class BookShopProfileProvider : ProfileProvider
{
//// 獲取數據操作配置類
private static readonly SqlBookShopProfileProvider dal = new SqlBookShopProfileProvider();
// 內部變量
private const string ERR_INVALID_PARAMETER = "Invalid Profile parameter:";
private const string PROFILE_SHOPPINGCART = "ShoppingCart";
private const string PROFILE_ACCOUNT = "AccountInfo";
private static string applicationName = "BookShop";
/// <summary>
/// 應用程序名
/// </summary>
public override string ApplicationName
{
get
{
return applicationName;
}
set
{
applicationName = value;
}
}
/// <summary>
/// 初始化提供程序
/// </summary>
/// <param name="name">提供程序的名稱</param>
/// <param name="config">提供程序的配置</param>
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", "BookShop Custom Profile Provider");
}
if (string.IsNullOrEmpty(name))
name = "ShoppingCartProvider";
if (config["applicationName"] != null && !string.IsNullOrEmpty(config["applicationName"].Trim()))
applicationName = config["applicationName"];
base.Initialize(name, config);
}
/// <summary>
/// 返回指定應用程序實例的設置屬性值集合和設置屬性組。
/// </summary>
/// <param name="context">當前應用程序上下文</param>
/// <param name="collection">一個系統的配置集合</param>
/// <returns>返回一個當前的配置集合</returns>
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
{
//獲取登錄用戶信息
string username = (string)context["UserName"];
bool isAuthenticated = (bool)context["IsAuthenticated"];
SettingsPropertyValueCollection svc = new SettingsPropertyValueCollection();
//遍歷集合中的屬性
foreach (SettingsProperty prop in collection)
{
SettingsPropertyValue pv = new SettingsPropertyValue(prop);
//判斷屬性名稱
//本實例涉及到兩個屬性的保存:購物籃和帳戶地址
switch (pv.Property.Name)
{
case PROFILE_SHOPPINGCART:
pv.PropertyValue = GetCartItems(username, true);
break;
case PROFILE_ACCOUNT:
pv.PropertyValue = GetAccountInfo(username);
break;
default:
throw new ApplicationException(ERR_INVALID_PARAMETER + " name.");
}
svc.Add(pv);
}
return svc;
}
/// <summary>
/// 設置指定的屬性設置組的值。
/// </summary>
/// <param name="context">當前應用程序上下文</param>
/// <param name="collection">一個系統的配置集合.</param>
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{
//判斷當前登錄用戶
string username = (string)context["UserName"];
CheckUserName(username);
bool isAuthenticated = (bool)context["IsAuthenticated"];
int uniqueID = dal.GetUniqueID(username, isAuthenticated, false, ApplicationName);
if (uniqueID == 0)
uniqueID = dal.CreateProfileForUser(username, isAuthenticated, ApplicationName);
//遍歷用戶的保存信息
foreach (SettingsPropertyValue pv in collection)
{
if (pv.PropertyValue != null)
{
switch (pv.Property.Name)
{
case PROFILE_SHOPPINGCART:
SetCartItems(uniqueID, (CartItem)pv.PropertyValue, true);
break;
case PROFILE_ACCOUNT:
SetAccountInfo(uniqueID, (AddressInfo)pv.PropertyValue);
break;
default:
throw new ApplicationException(ERR_INVALID_PARAMETER + " name.");
}
}
}
//更新配置時間等信息
UpdateActivityDates(username, false);
}
// 獲取profile屬性
// 獲取帳戶信息
private static AddressInfo GetAccountInfo(string username)
{
return dal.GetAccountInfo(username, applicationName);
}
// 獲取購物籃信息
private static CartItem GetCartItems(string username, bool isShoppingCart)
{
//初始化一個購物籃實體操作類
CartItem cart = new CartItem();
foreach (CartItemInfo cartItem in dal.GetCartItems(username, applicationName, isShoppingCart))
{
//在購物籃中添加商品信息
cart.Add(cartItem);
}
return cart;
}
// 更新帳戶地址
private static void SetAccountInfo(int uniqueID, AddressInfo addressInfo)
{
dal.SetAccountInfo(uniqueID, addressInfo);
}
// 更新購物籃信息
private static void SetCartItems(int uniqueID, CartItem cart, bool isShoppingCart)
{
dal.SetCartItems(uniqueID, cart.CartItems, isShoppingCart);
}
/// <summary>
/// 更新當前用戶的購物籃信息
/// </summary>
/// <param name="uniqueID">用戶ID</param>
/// <param name="cartItems">購物籃中的商品</param>
/// <param name="isShoppingCart">購物籃標志</param>
private void SetCartItemsProfile(int uniqueID, ICollection<CartItemInfo> cartItems, bool isShoppingCart)
{
string sqlDelete = "DELETE FROM Cart WHERE UniqueID = @UniqueID AND IsShoppingCart = @IsShoppingCart;";
SqlParameter[] parms1 = {
new SqlParameter("@UniqueID", SqlDbType.Int),
new SqlParameter("@IsShoppingCart", SqlDbType.Bit)};
parms1[0].Value = uniqueID;
parms1[1].Value = isShoppingCart;
if (cartItems.Count > 0)
{
// 通過SQL事務更新信息
string sqlInsert = "INSERT INTO Cart (UniqueID, ItemId, Name, Type, Price, CategoryId, ProductId, IsShoppingCart, Quantity) VALUES (@UniqueID, @ItemId, @Name, @Type, @Price, @CategoryId, @ProductId, @IsShoppingCart, @Quantity);";
//定義購物籃的參數
SqlParameter[] parms2 = {
new SqlParameter("@UniqueID", SqlDbType.Int),
new SqlParameter("@IsShoppingCart", SqlDbType.Bit),
new SqlParameter("@ItemId", SqlDbType.VarChar, 10),
new SqlParameter("@Name", SqlDbType.VarChar, 80),
new SqlParameter("@Type", SqlDbType.VarChar, 80),
new SqlParameter("@Price", SqlDbType.Decimal, 8),
new SqlParameter("@CategoryId", SqlDbType.VarChar, 10),
new SqlParameter("@ProductId", SqlDbType.VarChar, 10),
new SqlParameter("@Quantity", SqlDbType.Int)};
parms2[0].Value = uniqueID;
parms2[1].Value = isShoppingCart;
SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction);
conn.Open();
//開始事務
SqlTransaction trans = conn.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
//在購物籃中添加信息
SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlDelete, parms1);
foreach (CartItemInfo cartItem in cartItems)
{
parms2[2].Value = cartItem.ItemId;
parms2[3].Value = cartItem.Name;
parms2[4].Value = cartItem.Price;
parms2[5].Value = cartItem.SupplierId;
parms2[6].Value = cartItem.ProductId;
parms2[7].Value = cartItem.Quantity;
SqlHelper.ExecuteNonQuery(trans, CommandType.Text, sqlInsert, parms2);
}
//執行事務
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -