示例#1
0
        /// <summary>
        /// Method to retrieve and cache product name by its ID
        /// </summary>
        /// <param name="productId">Product id</param>
        /// <returns>Product name</returns>
        public static string GetProductName(string productId)
        {
            Product product = new Product();
            if (!enableCaching)
                return product.GetProduct(productId).Name;

            string cacheKey = string.Format(PRODUCT_NAME_KEY, productId);

            // Check if the data exists in the data cache
            string data = (string)HttpRuntime.Cache[cacheKey];

            if (data == null) {
                // Caching duration from Web.config
                int cacheDuration = int.Parse(ConfigurationManager.AppSettings["ProductCacheDuration"]);

                // If the data is not in the cache then fetch the data from the business logic tier
                data = product.GetProduct(productId).Name;

                // Create a AggregateCacheDependency object from the factory
                AggregateCacheDependency cd = DependencyFacade.GetProductDependency();

                // Store the output in the data cache, and Add the necessary AggregateCacheDependency object
                HttpRuntime.Cache.Add(cacheKey, data, cd, DateTime.Now.AddHours(cacheDuration), Cache.NoSlidingExpiration, CacheItemPriority.High, null);

            }

            return data;
        }
        /// <summary>
        /// This method acts as a proxy between the web and business components to check whether the 
        /// underlying data has already been cached.
        /// </summary>
        /// <param name="category">Category</param>
        /// <returns>List of ProductInfo from Cache or Business component</returns>
        public static IList<ProductInfo> GetProductsByCategory(string category)
        {
            Product product = new Product();

            if (!enableCaching)
                return product.GetProductsByCategory(category);

            string key = "product_by_category_" + category;
            IList<ProductInfo> data = (IList<ProductInfo>)HttpRuntime.Cache[key];

            // Check if the data exists in the data cache
            if (data == null) {

                // If the data is not in the cache then fetch the data from the business logic tier
                data = product.GetProductsByCategory(category);

                // Create a AggregateCacheDependency object from the factory
                AggregateCacheDependency cd = DependencyFacade.GetProductDependency();

                // Store the output in the data cache, and Add the necessary AggregateCacheDependency object
                HttpRuntime.Cache.Add(key, data, cd, DateTime.Now.AddHours(productTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }

            return data;
        }
示例#3
0
    public Product[] GetProducts()
    {
        var categorySvc = new bll.Category();
        var categories  = categorySvc.GetCategories().Select(x => x.Id);
        var products    = new List <Product>();
        var productSvc  = new bll.Product();

        foreach (var category in categories)
        {
            var productInfos = productSvc.GetProductsByCategory(category);
            products.AddRange(productInfos.Select(x =>
            {
                return(new Product
                {
                    ProductId = x.Id,
                    CategoryId = x.CategoryId,
                    Description = x.Description,
                    ImageUrl = x.Image,
                    Name = x.Name
                });
            }));
        }

        return(products.ToArray());
    }
示例#4
0
        protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            products.CurrentPageIndex = e.NewPageIndex;

            // Get the search terms from the query string
            string searchKey = WebComponents.CleanString.InputText(Request["keywords"], 100);

            if (searchKey != ""){

                // Create a data cache key
                string cacheKey = "search" + searchKey;

                // Check if the objects are in the cache
                if(Cache[cacheKey] != null){
                    products.DataSource = (IList)Cache[cacheKey];
                }else{
                    // If that data is not in the cache then use the business logic tier to fetch the data
                    Product product = new Product();
                    IList productsBySearch = product.GetProductsBySearch(searchKey);
                    // Store the results in a cache
                    Cache.Add(cacheKey, productsBySearch, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
                    products.DataSource = productsBySearch;
                }

                // Databind the data to the controls
                products.DataBind();
            }
        }
示例#5
0
        protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            products.CurrentPageIndex = e.NewPageIndex;

            // Get the category from the query string
            // string categoryKey = Request["categoryId"];
            string categoryKey = WebComponents.CleanString.InputText(Request["categoryId"], 50);

            // Check to see if the contents are in the Data Cache
            if(Cache[categoryKey] != null){
                // If the data is already cached, then used the cached copy
                products.DataSource = (IList)Cache[categoryKey];
            }else{
                // If the data is not cached, then create a new products object and request the data
                Product product = new Product();
                IList productsByCategory = product.GetProductsByCategory(categoryKey);
                // Store the results of the call in the Cache and set the time out to 12 hours
                Cache.Add(categoryKey, productsByCategory, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
                products.DataSource = productsByCategory;
            }

            // Bind the data to the control
            products.DataBind();
            // Set the label to be the query parameter
            lblPage.Text = categoryKey;
        }
示例#6
0
        /// <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"];

            //bind data
            Product product = new Product();
            searchList.DataSource = product.GetProductsBySearch(keywordKey);
            searchList.DataBind();
        }
示例#7
0
        /// <summary>
        /// Rebind control 
        /// </summary>
        protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            //reset index
            productsList.CurrentPageIndex = e.NewPageIndex;

            //get category id
            string categoryKey = Request.QueryString["categoryId"];

            //bind data
            Product product = new Product();
            productsList.DataSource = product.GetProductsByCategory(categoryKey);
            productsList.DataBind();
        }
示例#8
0
    public Product GetProductById(string productId)
    {
        var svc         = new bll.Product();
        var productInfo = svc.GetProduct(productId);

        return(new Product
        {
            ProductId = productInfo.Id,
            CategoryId = productInfo.CategoryId,
            Description = productInfo.Description,
            ImageUrl = productInfo.Image,
            Name = productInfo.Name
        });
    }
示例#9
0
        /// <summary>
        /// Function called the user is trying to go 
        /// forward or backwards through the favourites list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FavoritesPageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            //Update the current page count
            favorites.CurrentPageIndex = e.NewPageIndex;

            //Retrieve the list of favourites from Cache using the category name stored in viewstate
            if(Cache[(string)ViewState[KEY_CATEGORY]] != null){
                favorites.DataSource = (IList)Cache[(string)ViewState[KEY_CATEGORY]];
            }else{
                //If there is nothing in viewstate, then fetch the favourites from the middle tier
                Product product = new Product();
                IList productsByCategory = product.GetProductsByCategory((string)ViewState[KEY_CATEGORY]);
                //Store the results in the cache
                Cache.Add((string)ViewState[KEY_CATEGORY], productsByCategory, null, DateTime.Now.AddHours(12), Cache.NoSlidingExpiration , CacheItemPriority.High, null);
                favorites.DataSource = productsByCategory;
            }

            //Rebind the favourites list data to the control
            favorites.DataBind();
        }