protected override async Task <TResult> InterceptAsync <TResult>(IInvocation invocation,
                                                                         IInvocationProceedInfo proceedInfo, Func <IInvocation, IInvocationProceedInfo, Task <TResult> > proceed)
        {
            MethodInfo           methodInfo           = invocation.MethodInvocationTarget;
            TransactionAttribute transactionAttribute = TransactionUtils.FindTransactionAttribute(methodInfo);
            bool bindingRequired = transactionAttribute != null;

            if (bindingRequired)
            {
                using (SessionWrapper sessionWrapper = sessionService.GetCurrentSession(transactionAttribute.ReadOnly))
                {
                    using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(
                               transactionAttribute.ReadOnly, transactionAttribute.IsolationLevel))
                    {
                        TResult retObject = await proceed(invocation, proceedInfo).ConfigureAwait(false);

                        await transactionWrapper.Commit().ConfigureAwait(false);

                        return(retObject);
                    }
                }
            }

            {
                TResult retObject = await proceed(invocation, proceedInfo).ConfigureAwait(false);

                return(retObject);
            }
        }
Exemple #2
0
        public async Task <QuestionInfo> RandomSelect(QuestionCategory questionCategory, int minDifficult)
        {
            if (minDifficult < QuestionConstants.QuestionMinDifficult ||
                minDifficult > QuestionConstants.QuestionMaxDifficult)
            {
                throw new ArgumentOutOfRangeException(nameof(minDifficult));
            }

            int questionCount = await questionInfoStatisticService.GetQuestionCount(questionCategory, minDifficult);

            if (questionCount <= 0)
            {
                throw new ApplicationException($"Can't find any question with {questionCategory} and {minDifficult}.");
            }

            int skipNum      = random.Next(0, questionCount);
            int maxDifficult = minDifficult + QuestionConstants.QuestionSelectDifficultRange;

            using SessionWrapper sessionWrapper         = questionInfoRepository.GetSessionWrapper(true);
            using TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(true);
            IQueryable <QuestionInfo> questionInfoQuery = questionInfoRepository.GetQueryable(sessionWrapper);

            questionInfoQuery = questionInfoQuery.Where(m => m.QuestionCategory == questionCategory);
            questionInfoQuery =
                questionInfoQuery.Where(m => m.Difficult >= minDifficult && m.Difficult < maxDifficult);
            return(await questionInfoQuery.Skip(skipNum).Take(1).FirstOrDefaultAsync());
        }
Exemple #3
0
        public virtual async Task Delete(long id)
        {
            using (SessionWrapper sessionWrapper = entityRepository.GetSessionWrapper())
            {
                using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction())
                {
                    await entityRepository.Delete(id);

                    await transactionWrapper.Commit();
                }
            }
        }
Exemple #4
0
        public virtual async Task <TEntity> Update(TEntity entity)
        {
            using (SessionWrapper sessionWrapper = entityRepository.GetSessionWrapper())
            {
                using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction())
                {
                    TEntity updatedEntity = await entityRepository.Update(entity);

                    await transactionWrapper.Commit();

                    return(updatedEntity);
                }
            }
        }
Exemple #5
0
        public virtual async Task <TEntity> Get(long id, bool updateLater = false)
        {
            using (SessionWrapper sessionWrapper = entityRepository.GetSessionWrapper(true))
            {
                using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(true))
                {
                    TEntity entity = await entityRepository.Get(id, updateLater);

                    await transactionWrapper.Commit();

                    return(entity);
                }
            }
        }
Exemple #6
0
        public virtual async Task <IList <TEntity> > LoadAll()
        {
            using (SessionWrapper sessionWrapper = entityRepository.GetSessionWrapper(true))
            {
                using (TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(true))
                {
                    IQueryable <TEntity> entityQuery = entityRepository.GetQueryable(sessionWrapper);
                    List <TEntity>       entities    = await entityQuery.ToListAsync();

                    await transactionWrapper.Commit();

                    return(entities);
                }
            }
        }
        public async Task <int> GetQuestionCount(QuestionCategory questionCategory, int minDifficult)
        {
            if (minDifficult < QuestionConstants.QuestionMinDifficult ||
                minDifficult > QuestionConstants.QuestionMaxDifficult)
            {
                throw new ArgumentOutOfRangeException(nameof(minDifficult));
            }

            string cacheName = GetCacheName(questionCategory, minDifficult);

            try
            {
                var cachedData = await cacheService.Get <CachedData>(cacheName);

                if (cachedData != null)
                {
                    return(cachedData.Count);
                }
            }
            catch (Exception e)
            {
                logger.LogError(e, $"Error on get cache by key {cacheName}");
            }

            using SessionWrapper sessionWrapper         = questionInfoStatisticRepository.GetSessionWrapper(true);
            using TransactionWrapper transactionWrapper = sessionWrapper.BuildTransaction(true);
            IQueryable <QuestionInfoStatistic> questionInfoStatisticQuery =
                questionInfoStatisticRepository.GetQueryable(sessionWrapper);

            questionInfoStatisticQuery = questionInfoStatisticQuery.Where(m => m.QuestionCategory == questionCategory);
            questionInfoStatisticQuery =
                questionInfoStatisticQuery.Where(m => m.Difficult == minDifficult);
            int questionCount = await questionInfoStatisticQuery.Select(m => m.QuestionCount).SumAsync();

            try
            {
                await cacheService.Set(cacheName, new CachedData { Count = questionCount });
            }
            catch (Exception e)
            {
                logger.LogError(e, $"Error on set cache by key {cacheName}");
            }

            return(questionCount);
        }