コード例 #1
0
        public virtual async Task <PromotionUsage[]> GetByIdsAsync(string[] ids)
        {
            var cacheKey = CacheKey.With(GetType(), "GetByIdsAsync", string.Join("-", ids));

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(PromotionUsageCacheRegion.CreateChangeToken());
                using (var repository = _repositoryFactory())
                {
                    var promotionUsage = await repository.GetMarketingUsagesByIdsAsync(ids);
                    return promotionUsage.Select(x => x.ToModel(AbstractTypeFactory <PromotionUsage> .TryCreateInstance())).ToArray();
                }
            }));
        }
コード例 #2
0
        public virtual Task <PromotionUsage[]> GetByIdsAsync(string[] ids)
        {
            var cacheKey = CacheKey.With(GetType(), nameof(GetByIdsAsync), string.Join("-", ids));

            return(_platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async(cacheEntry) =>
            {
                using (var repository = _repositoryFactory())
                {
                    //It is so important to generate change tokens for all ids even for not existing objects to prevent an issue
                    //with caching of empty results for non - existing objects that have the infinitive lifetime in the cache
                    //and future unavailability to create objects with these ids.
                    cacheEntry.AddExpirationToken(PromotionUsageCacheRegion.CreateChangeToken(ids));

                    var promotionUsages = await repository.GetMarketingUsagesByIdsAsync(ids);
                    var usages = promotionUsages.Select(x => x.ToModel(AbstractTypeFactory <PromotionUsage> .TryCreateInstance())).ToArray();

                    return usages;
                }
            }));
        }
コード例 #3
0
        public virtual async Task <GenericSearchResult <PromotionUsage> > SearchUsagesAsync(PromotionUsageSearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

            var cacheKey = CacheKey.With(GetType(), "SearchUsagesAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(PromotionUsageCacheRegion.CreateChangeToken());

                using (var repository = _repositoryFactory())
                {
                    var query = GetPromotionUsageQuery(repository, criteria);

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = ReflectionUtility.GetPropertyName <PromotionUsage>(x => x.ModifiedDate), SortDirection = SortDirection.Descending
                                            } };
                    }
                    query = query.OrderBySortInfos(sortInfos);

                    var totalCount = await query.CountAsync();
                    var searchResult = new GenericSearchResult <PromotionUsage> {
                        TotalCount = totalCount
                    };

                    if (criteria.Take > 0)
                    {
                        var coupons = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                        searchResult.Results = coupons.Select(x => x.ToModel(AbstractTypeFactory <PromotionUsage> .TryCreateInstance())).ToList();
                    }

                    return searchResult;
                }
            }));
        }