示例#1
0
        /// <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)
        {
            Category category = new Category();
            if (!enableCaching)
                return category.GetCategory(categoryId).Name;

            string cacheKey = string.Format(CATEGORY_NAME_KEY, categoryId);

            // 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["CategoryCacheDuration"]);

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

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

                // 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;
        }
示例#2
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());
    }
    public Category GetCategoryById(string categoryId)
    {
        var svc          = new bll.Category();
        var categoryInfo = svc.GetCategory(categoryId);

        var category = new Category
        {
            CategoryId  = categoryInfo.Id,
            Description = categoryInfo.Description,
            Name        = categoryInfo.Name
        };

        return(category);
    }
    public Category[] GetCategories()
    {
        var svc        = new bll.Category();
        var categories = svc.GetCategories().Select(x =>
        {
            return(new Category
            {
                CategoryId = x.Id,
                Description = x.Description,
                Name = x.Name
            });
        });

        return(categories.ToArray());
    }
        /// <summary>
        /// This method acts as a proxy between the web and business components to check whether the 
        /// underlying data has already been cached.
        /// </summary>
        /// <returns>List of CategoryInfo from Cache or Business component</returns>
        public static IList<CategoryInfo> GetCategories()
        {
            Category cat = new Category();
            if (!enableCaching)
                return cat.GetCategories();

            string key = "category_all";
            IList<CategoryInfo> data = (IList<CategoryInfo>)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 = cat.GetCategories();

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

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

            return data;
        }
 // Bind categories
 private void BindCategories()
 {
     Category category = new Category();
     repCategories.DataSource = category.GetCategories();
     repCategories.DataBind();
 }