/// <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; } }
public void CacheItem_NullCacheKeyThrowNewArgumentException_Test() { //Arrange CacheKey key = null; //Act CacheItemConfig item = new CacheItemConfig(key,new TimeSpan()); }
public void CacheItem_DefaultConstructorSet10SecondsToExpirationTime_Test() { //Arrrange CacheKey key = new CacheKey("fakeName"); //Act CacheItemConfig item = new CacheItemConfig(key); //Assert Assert.IsTrue(item.ExpirationTime.TotalSeconds == 10); }
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); }
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); }
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); }
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); }
/// <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; } }