/// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainModule.SalesManagement.ISalesManagementService"/>
        /// </summary>
        /// <param name="pageIndex"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.SalesManagement.ISalesManagementService"/></param>
        /// <param name="pageCount"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.SalesManagement.ISalesManagementService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.SalesManagement.ISalesManagementService"/></returns>
        public List<Order> FindPagedOrders(int pageIndex, int pageCount)
        {
            if (pageIndex < 0)
                throw new ArgumentException(Resources.Messages.exception_InvalidPageIndex, "pageIndex");

            if (pageCount <= 0)
                throw new ArgumentException(Resources.Messages.exception_InvalidPageCount, "pageCount");

            //implement cache-aside pattern 

            List<Order> orderResult = null;
            CacheKey key = new CacheKey("FindPagedOrders", new { PageIndex = pageIndex, PageCount = pageCount });
            CacheItemConfig cacheItemConfig = new CacheItemConfig(key, new TimeSpan(0, 0, 10));

            if (_cacheManager.TryGet<List<Order>>(cacheItemConfig, out orderResult))
                return orderResult;
            else
            {
                //query repository
                orderResult =  _orderRepository.GetPagedElements(pageIndex, pageCount, o => o.OrderId, true)
                                               .ToList();

                _cacheManager.Add(cacheItemConfig, orderResult);

                return orderResult;
            }
        }
Exemplo n.º 2
0
        public void CacheKey_ConstructorWithWhitespaceKeyName_ThrowArgumentException_Test()
        {
            //arrange
            string keyName = "";

            //act
            CacheKey key = new CacheKey(keyName);
        }
Exemplo n.º 3
0
        public void CacheKey_ConstructorWithNullKeyName_ThrowArgumentException_Test()
        {
            //arrange
            string keyName = null;

            //act
            CacheKey key = new CacheKey(keyName); 
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create a new instance of cache item
        /// </summary>
        /// <param name="cacheKey">The cached key</param>
        /// <param name="expirationTime">Associated expiration time</param>
        public CacheItemConfig(CacheKey cacheKey, TimeSpan expirationTime)
        {
            if (cacheKey == (CacheKey)null)
                throw new ArgumentNullException("cacheKey");

            _cacheKey = cacheKey;
            _expirationTime = expirationTime;

        }
Exemplo n.º 5
0
        public void CacheKey_Constructor_Test()
        {
            //arrange
            string keyName = "fakeName";

            //act
            CacheKey key = new CacheKey(keyName);

            //assert
            Assert.AreEqual(key.KeyName, keyName);
        }
Exemplo n.º 6
0
        public void CacheItem_DefaultConstructorSet10SecondsToExpirationTime_Test()
        {
            //Arrrange
            CacheKey key = new CacheKey("fakeName");

            //Act
            CacheItemConfig item = new CacheItemConfig(key);

            //Assert
            Assert.IsTrue(item.ExpirationTime.TotalSeconds == 10);
        }
Exemplo n.º 7
0
        public void CacheItem_Constructor_Test()
        {
            //Arrange 
            CacheKey key = new CacheKey("fakeName");

            //Act
            CacheItemConfig item = new CacheItemConfig(key, new TimeSpan(0, 0, 10));

            //assert

            Assert.AreEqual(key, item.CacheKey);
            Assert.IsTrue(item.ExpirationTime.TotalSeconds == 10);

        }
Exemplo n.º 8
0
        public void CacheKey_CheckVaryParamsFromNullAnonimousType_Test()
        {
            //arrange
            string keyName = "fakeName";
            object varyParams = null;

            //act
            CacheKey key = new CacheKey(keyName, varyParams);

            string result = key.GetCacheKey();

            //assert
            Assert.AreEqual(result, string.Format("{0}#", keyName));

        }
        public void CacheManager_TryGet_NotInCacheReturnFalse_Test()
        {
            //Arrange
            CacheManager cacheManager = new CacheManager();

            CacheKey key = new CacheKey(Guid.NewGuid().ToString());
            CacheItemConfig item = new CacheItemConfig(key);

            //Act
            object cacheditem = null;
            bool result = cacheManager.TryGet<object>(item, out cacheditem);

            //Assert
            Assert.IsFalse(result);
        }
Exemplo n.º 10
0
        public void CacheKey_CheckVaryParamsFromAnonimousType_Test()
        {
            //arrange
            string keyName = "fakeName";
            object varyParams = new { PropertyA = "ParamA", PropertyB = 2 };

            //act
            CacheKey key = new CacheKey(keyName, varyParams);

            string result = key.GetCacheKey();

            string expected = string.Format("{0}#{1};{2};{3};{4}", keyName, "PropertyA", "ParamA", "PropertyB", "2");

            //assert
            Assert.AreEqual(result, expected);

        }
Exemplo n.º 11
0
        public void CacheManager_TryGet_InCacheReturnTrue_Test()
        {
            //Arrange
            CacheManager cacheManager = new CacheManager();

            CacheKey key = new CacheKey(Guid.NewGuid().ToString());
            CacheItemConfig cacheitem = new CacheItemConfig(key);

            //Act
            object item = new object();
            object expected;
            cacheManager.Add(cacheitem, item);
            bool result = cacheManager.TryGet<object>(cacheitem, out expected);

            //Assert
            Assert.IsTrue(result);
        }
Exemplo n.º 12
0
        public void CacheManager_Add_Test()
        {
            //Arrange 
            CacheManager cacheManager = new CacheManager();

            CacheKey key = new CacheKey("fakeKey");
            CacheItemConfig cacheItem = new CacheItemConfig(key);

            //act
            
            cacheManager.Add(cacheItem,new object());

            //assert
            object result;
            Assert.IsTrue(cacheManager.TryGet(cacheItem, out result));
            Assert.IsNotNull(result);

        }
Exemplo n.º 13
0
 /// <summary>
 /// Create a new instance of cache item
 /// </summary>
 /// <param name="cacheKey">The cached key</param>
 public CacheItemConfig(CacheKey cacheKey)
     :this(cacheKey,new TimeSpan(0,0,10))
 {
 }
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/>
        /// </summary>
        /// <param name="pageIndex"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <param name="pageCount"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></returns>
        public List<Country> FindPagedCountries(int pageIndex, int pageCount)
        {
            //implement cache aside pattern
            List<Country> countryResults = null;
            CacheKey key = new CacheKey("FindPagedCountries", new { PageIndex = pageIndex, PageCount = pageCount });
            CacheItemConfig cacheItemConfig = new CacheItemConfig(key, new TimeSpan(0, 10, 0));


            if (_cacheManager.TryGet<List<Country>>(cacheItemConfig, out countryResults))
                return countryResults;
            else
            {
                countryResults =  _countryRepository.GetPagedElements(pageIndex, pageCount, c => c.CountryName, true)
                                                    .ToList();

                _cacheManager.Add(cacheItemConfig, countryResults);

                return countryResults;
            }
        }
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/>
        /// </summary>
        /// <param name="countryName"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></returns>
        public List<Country> FindCountriesByName(string countryName)
        {
            //implement cache-aside pattern 

            List<Country> countryResults = null;
            CacheKey key = new CacheKey("FindCountriesByName",new {CountryName=countryName});
            CacheItemConfig cacheItemConfig = new CacheItemConfig(key, new TimeSpan(0, 10, 0));

            if (_cacheManager.TryGet<List<Country>>(cacheItemConfig, out countryResults))
                return countryResults;
            else
            {
                CountryNameSpecification spec = new CountryNameSpecification(countryName);

                countryResults =  _countryRepository.GetBySpec(spec as ISpecification<Country>)
                                                    .ToList();

                _cacheManager.Add(cacheItemConfig, countryResults);

                return countryResults;
            }
                                     
        }
        /// <summary>
        /// <see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/>
        /// </summary>
        /// <param name="pageIndex"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <param name="pageCount"><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></param>
        /// <returns><see cref="Microsoft.Samples.NLayerApp.Application.MainModule.CustomersManagement.ICustomerManagementService"/></returns>
        public List<Customer> FindPagedCustomers(int pageIndex, int pageCount)
        {
            if (pageIndex < 0)
                throw new ArgumentException(Resources.Messages.exception_InvalidPageIndex, "pageIndex");

            if (pageCount <= 0)
                throw new ArgumentException(Resources.Messages.exception_InvalidPageCount, "pageCount");

            //implement cache-aside pattern 

            List<Customer> customerResults = null;
            CacheKey key = new CacheKey("FindPagedCustomers", new {PageIndex=pageIndex,PageCount = pageCount });
            CacheItemConfig cacheItemConfig = new CacheItemConfig(key, new TimeSpan(0, 0, 30));

            if (_cacheManager.TryGet<List<Customer>>(cacheItemConfig, out customerResults))
                return customerResults;
            else
            {

                //Create "enabled variable" transform adhoc execution plan in prepared plan
                //for more info: http://geeks.ms/blogs/unai/2010/07/91/ef-4-0-performance-tips-1.aspx
                bool enabled = true;
                Specification<Customer> onlyEnabledSpec = new DirectSpecification<Customer>(c => c.IsEnabled == enabled);

                customerResults = _customerRepository.GetPagedElements(pageIndex, pageCount, c => c.CustomerCode, onlyEnabledSpec, true)
                                                     .ToList();

                _cacheManager.Add(cacheItemConfig, customerResults);

                return customerResults;
            }

            
        }