/// <summary> /// Method to retrieve and cache category name by its ID /// </summary> /// <param name="categoryId">Category id</param> /// <returns>Category name</returns> public static string GetCategoryName(string categoryId) { if (!enableCaching) { string categoryName = String.Empty; using (var context = new PetShopDataContext()) { categoryName = context.Category.GetByKey(categoryId).Name; } return categoryName; } string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId); // Check if the data exists in the data cache var data = (string) HttpRuntime.Cache[cacheKey]; if (data == null) { // Caching duration from Web.config int cacheDuration = int.Parse(ConfigurationManager.AppSettings["CategoryCacheDuration"]); // If the data is not in the cache then fetch the data from the business logic tier using (var context = new PetShopDataContext()) { data = context.Category.GetByKey(categoryId).Name; } // Store the output in the data cache, and Add the necessary AggregateCacheDependency object HttpRuntime.Cache.Add(cacheKey, data, null, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null); } return data; }
/// <summary> /// Update profile /// </summary> protected void btnSubmit_Click(object sender, EventArgs e) { using (var context = new PetShopDataContext()) { var options = new DataLoadOptions(); options.LoadWith<Profile>(p => p.AccountList); context.LoadOptions = options; var profile = context.Profile.GetProfile(User.Identity.Name); if (!string.IsNullOrEmpty(profile.Username) && AddressForm.IsValid) { if (profile.AccountList.Count > 0) { Account account = profile.AccountList[0]; UpdateAccount(ref account, AddressForm.Address); } else { var account = new Account(); profile.AccountList.Add(account); account.UniqueID = profile.UniqueID; UpdateAccount(ref account, AddressForm.Address); } context.SubmitChanges(); } } lblMessage.Text = "Your profile information has been successfully updated.<br> "; }
/// <summary> /// Calculate total /// </summary> protected void BtnTotal_Click(object sender, ImageClickEventArgs e) { using (var context = new PetShopDataContext()) { var profile = context.Profile.GetProfile(Page.User.Identity.Name); if (!string.IsNullOrEmpty(profile.Username)) { TextBox txtQuantity; ImageButton btnDelete; int qty = 0; foreach (RepeaterItem row in repShoppingCart.Items) { txtQuantity = (TextBox)row.FindControl("txtQuantity"); btnDelete = (ImageButton)row.FindControl("btnDelete"); if (int.TryParse(WebUtility.InputText(txtQuantity.Text, 10), out qty)) { if (qty > 0) CartHelper.SetQuantity(profile.ShoppingCart, btnDelete.CommandArgument, qty); else if (qty == 0) CartHelper.Remove(profile.ShoppingCart, btnDelete.CommandArgument); } } } } BindCart(); }
protected void Page_PreInit(object sender, EventArgs e) { if (!IsPostBack) { string itemId = Request.QueryString["addItem"]; if (!string.IsNullOrEmpty(itemId)) { Profile profile = null; using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(Page.User.Identity.Name); if (profile != null) profile.Detach(); } if (profile != null && !string.IsNullOrEmpty(profile.Username)) { CartHelper.Add(profile.WishList, itemId, profile.UniqueID, false); } // Redirect to prevent duplictations in the wish list if user hits "Refresh" Response.Redirect("~/WishList.aspx", true); } } }
public void CreateProfile() { Stopwatch watch = Stopwatch.StartNew(); var profile = new Profile(); profile.Username = NAME; profile.ApplicationName = PetShopConstants.APPLICATION_NAME; profile.IsAnonymous = false; profile.LastActivityDate = DateTime.Now; profile.LastUpdatedDate = DateTime.Now; try { using (var context = new PetShopDataContext()) { context.Profile.InsertOnSubmit(profile); context.SubmitChanges(); } } catch (Exception ex) { Assert.Fail(ex.Message); } Assert.IsTrue(true); Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
/// <summary> /// Bind repeater to Cart object in Profile /// </summary> private void BindCart() { using (var context = new PetShopDataContext()) { var profile = context.Profile.GetProfile(Page.User.Identity.Name); if (!string.IsNullOrEmpty(profile.Username)) { List<Cart> items = profile.ShoppingCart; items.ForEach(i => i.Detach()); if (items.Count > 0) { repShoppingCart.DataSource = items; repShoppingCart.DataBind(); PrintTotal(); plhTotal.Visible = true; } else { repShoppingCart.Visible = false; plhTotal.Visible = false; lblMsg.Text = "Your cart is empty."; } } } }
public static void ClearCart(List<Cart> items) { using (var context = new PetShopDataContext()) { context.Cart.AttachAll(items); context.Cart.DeleteAllOnSubmit(items); context.SubmitChanges(); } }
// Bind categories private void BindCategories() { using (var context = new PetShopDataContext()) { rePCategories.DataSource = context.Category.OrderBy(c => c.Name).FromCache().ToList(); } rePCategories.DataBind(); }
public static void SetQuantity(List<Cart> items, string itemId, int quantity) { var item = items.FirstOrDefault(i => i.ItemId == itemId); using (var context = new PetShopDataContext()) { context.Cart.Attach(item); item.Quantity = quantity; context.SubmitChanges(); } }
protected Inventory GetInventory(string itemId) { var inventory = new Inventory(); using( var context = new PetShopDataContext()) { inventory = context.Inventory.GetByKey(itemId); inventory.Detach(); } return inventory; }
protected void Page_Load(object sender, EventArgs e) { if (billingForm.Address == null) { using (var context = new PetShopDataContext()) { var profile = context.Profile.GetProfile(User.Identity.Name); billingForm.Address = new Address(profile); } } }
/// <summary> /// Recalculate the total /// </summary> private void PrintTotal() { using (var context = new PetShopDataContext()) { var profile = context.Profile.GetProfile(Page.User.Identity.Name); if (!string.IsNullOrEmpty(profile.Username)) { if (profile.ShoppingCart.Count > 0) ltlTotal.Text = CartHelper.GetTotal(profile.ShoppingCart).ToString("c"); } } }
public static void MoveToCart(Profile profile, string itemId) { var item = profile.WishList.FirstOrDefault(i => i.ItemId == itemId); using (var context = new PetShopDataContext()) { context.Cart.Attach(item); item.IsShoppingCart = true; context.SubmitChanges(); } profile.WishList.Remove(item); profile.ShoppingCart.Add(item); }
public void FetchProfile() { Stopwatch watch = Stopwatch.StartNew(); Profile profile = null; using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(NAME); profile.Detach(); } Assert.IsTrue(profile.Username == NAME); Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
/// <summary> /// Rebind control /// </summary> protected void PageChanged(object sender, DataGridPageChangedEventArgs e) { //reset index productsList.CurrentPageIndex = e.NewPageIndex; //get category id string categoryId = Request.QueryString["categoryId"]; //bind data( using (var context = new PetShopDataContext()) { productsList.DataSource = context.Product.ByCategoryId(categoryId).ToList(); } productsList.DataBind(); }
/// <summary> /// Rebind control /// </summary> protected void PageChanged(object sender, DataGridPageChangedEventArgs e) { //reset index searchList.CurrentPageIndex = e.NewPageIndex; //get category id string keywordKey = Request.QueryString["keywords"]; var list = new List<Product>(); using (var context = new PetShopDataContext()) { list = context.Product.Search(keywordKey).ToList(); } //bind data searchList.DataSource = list; searchList.DataBind(); }
/// <summary> /// Rebind control /// </summary> protected void PageChanged(object sender, DataGridPageChangedEventArgs e) { //reset index itemsGrid.CurrentPageIndex = e.NewPageIndex; //get category id string productId = Request.QueryString["productId"]; using (var context = new PetShopDataContext()) { var options = new DataLoadOptions(); options.LoadWith<Item>(i => i.Product); context.LoadOptions = options; itemsGrid.DataSource = context.Item.ByProductId(productId).ToList(); } itemsGrid.DataBind(); }
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) { using (var context = new PetShopDataContext()) { string userName = ((System.Web.UI.WebControls.CreateUserWizard)sender).UserName; var profile = context.Profile.GetProfile(userName); if (null == profile) { profile = new Profile(); profile.Username = userName; profile.ApplicationName = ".NET Pet Shop 4.0"; profile.IsAnonymous = false; profile.LastActivityDate = DateTime.Now; profile.LastUpdatedDate = DateTime.Now; context.Profile.InsertOnSubmit(profile); context.SubmitChanges(); } } }
/// <summary> /// Bind controls to profile /// </summary> private void BindUser() { using (var context = new PetShopDataContext()) { var profile = context.Profile.GetProfile(User.Identity.Name); if (string.IsNullOrEmpty(profile.Username)) { profile = new Profile(); profile.Username = User.Identity.Name; profile.ApplicationName = ".NET Pet Shop 4.0"; profile.IsAnonymous = !User.Identity.IsAuthenticated; profile.LastActivityDate = DateTime.Now; profile.LastUpdatedDate = DateTime.Now; context.Profile.InsertOnSubmit(profile); context.SubmitChanges(); } AddressForm.Address = new Address(profile); } }
public static void Add(List<Cart> items, string itemId, int uniqueId, bool isShoppingCart) { int index = 0; bool found = false; using (var context = new PetShopDataContext()) { var options = new DataLoadOptions(); options.LoadWith<Item>(i => i.Product); var item = items.FirstOrDefault(i => i.ItemId == itemId); if (item != null) { context.Cart.Attach(item); item.Quantity++; } else { var cartItem = context.Item.GetByKey(itemId); var cart = new Cart(); cart.UniqueID = uniqueId; cart.ItemId = itemId; cart.Name = cartItem.Name; cart.ProductId = cartItem.ProductId; cart.IsShoppingCart = isShoppingCart; cart.Price = cartItem.ListPrice ?? cartItem.UnitCost ?? 0; cart.Type = cartItem.Product.Name; cart.CategoryId = cartItem.Product.CategoryId; cart.Quantity = 1; items.Add(cart); context.Cart.InsertOnSubmit(cart); } context.SubmitChanges(); } }
/// <summary> /// Bind repeater to Cart object in Profile /// </summary> private void BindCart() { var profile = new Data.Profile(); using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(Page.User.Identity.Name); } if (!string.IsNullOrEmpty(profile.Username)) { List<Cart> wishList = profile.WishList; if (wishList.Count > 0) { repWishList.DataSource = wishList; repWishList.DataBind(); } else { repWishList.Visible = false; lblMsg.Text = "Your wish list is empty."; } } }
/// <summary> /// Handler for Delete/Move buttons /// </summary> protected void CartItem_Command(object sender, CommandEventArgs e) { var profile = new Data.Profile(); using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(Page.User.Identity.Name); profile.Detach(); } if (!string.IsNullOrEmpty(profile.Username)) { switch (e.CommandName) { case "Del": CartHelper.Remove(profile.WishList, e.CommandArgument.ToString()); break; case "Move": CartHelper.MoveToCart(profile, e.CommandArgument.ToString()); break; } } BindCart(); }
public void UpdateProfile() { Stopwatch watch = Stopwatch.StartNew(); Profile profile = null; using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(NAME); profile.Detach(); } using (var context = new PetShopDataContext()) { context.Profile.Attach(profile); profile.IsAnonymous = true; context.SubmitChanges(); } using (var context = new PetShopDataContext()) { Assert.IsTrue(context.Profile.GetProfile(NAME).IsAnonymous.Value); } Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
public void UpdateItemQuantityShoppingCart() { Stopwatch watch = Stopwatch.StartNew(); //Add new Item to the cart. Profile profile = null; using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(NAME); profile.Detach(); } CartHelper.Add(profile.ShoppingCart, ID, profile.UniqueID, true); CartHelper.Add(profile.ShoppingCart, ID, profile.UniqueID, true); using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(NAME); profile.Detach(); } Assert.IsTrue(profile.ShoppingCart.Count == 1 && profile.ShoppingCart[0].Quantity == 2); Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
public void ClearShoppingCart() { Stopwatch watch = Stopwatch.StartNew(); //Add new Item to the cart. Profile profile = null; using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(NAME); profile.Detach(); } CartHelper.Add(profile.ShoppingCart, ID, profile.UniqueID, true); //Clear the cart. using (var context = new PetShopDataContext()) { profile = context.Profile.GetProfile(NAME); profile.Detach(); } CartHelper.ClearCart(profile.ShoppingCart); using (var context = new PetShopDataContext()) { Assert.IsTrue(context.Profile.GetProfile(NAME).ShoppingCart.Count == 0); } Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
public static void SaveOrderLineItems(List<Cart> cart, int orderId) { int lineNum = 0; using (var context = new PetShopDataContext()) { foreach (var item in cart) { var lineItem = new LineItem(); lineItem.OrderId = orderId; lineItem.ItemId = item.ItemId; lineItem.LineNum = ++lineNum; lineItem.Quantity = item.Quantity; lineItem.UnitPrice = item.Price; context.LineItem.InsertOnSubmit(lineItem); } context.SubmitChanges(); } }
public void DeleteSupplier() { Stopwatch watch = Stopwatch.StartNew(); using (var context = new PetShopDataContext()) { context.Supplier.Delete(_supplierId); } Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
public static void Remove(List<Cart> items, string itemId) { var item = items.FirstOrDefault(i => i.ItemId == itemId); using (var context = new PetShopDataContext()) { context.Cart.Delete(item.CartId); } items.Remove(item); }
public void DeleteCategory() { Stopwatch watch = Stopwatch.StartNew(); using (var context = new PetShopDataContext()) { context.Category.Delete(ID); } Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }
public void DeleteProfile() { Stopwatch watch = Stopwatch.StartNew(); using (var context = new PetShopDataContext()) { var profile = context.Profile.GetProfile(NAME); if(profile != null) context.Profile.Delete(profile.UniqueID); } Console.WriteLine("Time: {0} ms", watch.ElapsedMilliseconds); }