/// <summary>
        /// Proccess the task asynchronously.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <typeparam name="TContext">The type of the context.</typeparam>
        /// <param name="repository">The repository.</param>
        /// <param name="list">The list.</param>
        /// <param name="chunkSize">Size of the chunk.</param>
        /// <param name="task">The task.</param>
        /// <param name="className">Name of the class.</param>
        /// <param name="token">The token.</param>
        internal static async Task ProccessTaskAsync <TValue, TContext>(
            IBaseRepositoryAsync <TValue, TContext> repository,
            IEnumerable <TValue> list,
            int chunkSize,
            Func <IEnumerable <TValue>, CancellationToken, Task> task,
            string className,
            CancellationToken token)
            where TValue : class
            where TContext : DbContext
        {
            ThrowErrorIf.IsNullValue(repository, nameof(repository), className);

            ThrowErrorIf.IsNullOrEmptyList(list, nameof(list), className);

            ThrowErrorIf.IsLessThanOrEqualsZero(chunkSize);

            await repository.MultiTransactionsAsync(
                async ctx =>
            {
                var listSplit = list.SplitList(chunkSize);

                foreach (var split in listSplit)
                {
                    await task(split, token).
                    ConfigureAwait(false);
                }
            }, token).ConfigureAwait(false);
        }
        public bool Equals(PageConfig other)
        {
            ThrowErrorIf.
            IsNullValue(other, nameof(other), nameof(Equals));

            return(other == this);
        }
        public async Task MultiTransactionsAsync(
            Func <DbSet <TValue>, Task> @action,
            CancellationToken token)
        {
            ThrowErrorIf.
            IsNullValue(@action, nameof(@action), nameof(MultiTransactionsAsync));

            var strategy = Context.Database.CreateExecutionStrategy();

            await strategy.ExecuteAsync(async() =>
            {
                using (var transaction = await Context.Database.BeginTransactionAsync().ConfigureAwait(false))
                {
                    try
                    {
                        await @action(Context.Set <TValue>()).
                        ConfigureAwait(false);

                        await transaction.
                        CommitAsync().
                        ConfigureAwait(false);
                    }
                    catch
                    {
                        await transaction.
                        RollbackAsync().
                        ConfigureAwait(false);

                        throw;
                    }
                }
            });
        }
Esempio n. 4
0
        public virtual async Task <TValue> FindAsync(
            params object[] parameters)
        {
            ThrowErrorIf.
            IsNullValue(parameters, nameof(parameters), nameof(FindAsync));

            return(await Context.FindAsync <TValue>(parameters).
                   ConfigureAwait(false));
        }
 public void StartCtor(
     IPageConfig config,
     IQueryable <TValue> listEntities,
     ICacheRepository cacheRepository,
     Func <IEnumerable <object>, IEnumerable <TResult> > mapping)
 {
     ThrowErrorIf.IsNullValue(mapping, nameof(mapping), typeof(PageAbstract <,>).Name);
     Mapping = mapping;
 }
        /// <summary>Generates the predicate.</summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <typeparam name="TFilter">The type of the filter.</typeparam>
        /// <param name="filter">The filter.</param>
        /// <param name="cacheRepository">The cache repository.</param>
        /// <returns></returns>
        public static async Task <Expression <Func <TValue, bool> > > CreateGenericFilter <TValue, TFilter>(
            this TFilter filter,
            ICacheRepository cacheRepository,
            CancellationToken token)
            where TValue : class
            where TFilter : class, IFilter
        {
            var predicate   = (Expression <Func <TValue, bool> >)null;
            var parameter   = Expression.Parameter(typeof(TValue));
            var filterName  = typeof(TFilter).Name;
            var mergeOption = LambdaMerge.And;

            var dictionaryMethodGet = await cacheRepository.GetDictionaryMethodGet(filterName, token).ConfigureAwait(false);

            foreach (var getFilter in dictionaryMethodGet)
            {
                var key   = getFilter.Key;
                var value = getFilter.Value(filter);

                if (!IsValidValue(value))
                {
                    continue;
                }

                var attributeCached = await cacheRepository.
                                      GetDictionaryAttribute(filterName, token).
                                      ConfigureAwait(false);

                if (!attributeCached.TryGetValue(key, out var attributes))
                {
                    continue;
                }

                attributes.TryGetValue(MethodOption, out var attributeMethod);

                ThrowErrorIf.IsNullValue(attributeMethod, nameof(attributeMethod), nameof(CreateGenericFilter));

                var methodOption = (LambdaMethod)attributeMethod.Value;

                attributes.TryGetValue(NameProperty, out var attributeName);

                var property = await cacheRepository.
                               GetProperty(typeof(TValue).Name, (string)attributeName.Value ?? key, token).
                               ConfigureAwait(false);

                var expression = methodOption.CreateExpressionPerType(parameter, property, value);

                ThrowErrorIf.IsNullValue(expression, nameof(expression), nameof(CreateGenericFilter));

                predicate = ExpressionMergeFacade.CreateExpression(predicate, expression, parameter, mergeOption);

                attributes.TryGetValue(MergeOption, out var attributeMerge);

                mergeOption = !attributeMerge.Value.IsNull() ? (LambdaMerge)attributeMerge.Value : LambdaMerge.And;
            }
            return(predicate);
        }
        public virtual async Task DeleteAsync(TValue entity, CancellationToken token)
        {
            ThrowErrorIf.
            IsNullValue(entity, nameof(entity), nameof(DeleteAsync));

            Context.Remove(entity);

            await SaveChangesAsync(token).
            ConfigureAwait(false);
        }
        public virtual async Task UpdateAsync(TValue entity, CancellationToken token)
        {
            ThrowErrorIf.
            IsNullValue(entity, nameof(entity), nameof(UpdateAsync));

            Context.Attach(entity).State = EntityState.Modified;

            await SaveChangesAsync(token).
            ConfigureAwait(false);
        }
        public virtual async Task UpdateAsync(IEnumerable <TValue> entityList, CancellationToken token)
        {
            ThrowErrorIf.
            IsNullOrEmptyList(entityList, nameof(entityList), nameof(UpdateAsync));

            Context.UpdateRange(entityList);

            await SaveChangesAsync(token).
            ConfigureAwait(false);
        }
Esempio n. 10
0
        public async Task <IReadOnlyList <TValue> > FilterAllAsync(
            TFilter filter,
            bool notTracking,
            CancellationToken token)
        {
            ThrowErrorIf.IsNullValue(filter, nameof(filter), nameof(FilterAllAsync));

            await CreateQueryFiltered(filter, notTracking, token).ConfigureAwait(false);

            return(await Query.ToListAsync().ConfigureAwait(false));
        }
Esempio n. 11
0
        public async Task <IReadOnlyList <TReturn> > GetAllAsync <TReturn>(
            bool notTracking,
            Func <IEnumerable <object>, IEnumerable <TReturn> > mapper,
            CancellationToken token)
        {
            ThrowErrorIf.IsNullValue(mapper, nameof(mapper), nameof(FilterAllAsync));

            var list = await CreateList(notTracking, token).ConfigureAwait(false);

            return(mapper(list).ToList());
        }
Esempio n. 12
0
        public virtual async Task <int> CountAsync(
            Expression <Func <TValue, bool> > predicate,
            CancellationToken token)
        {
            ThrowErrorIf.IsNullValue(predicate, nameof(predicate), nameof(CountAsync));

            await CreateQuery(true, token).ConfigureAwait(false);

            return(await Query.CountAsync(predicate, token).
                   ConfigureAwait(false));
        }
        public virtual async Task <IPage <TValue> > GetPageAsync(
            IPageConfig config,
            bool notTracking,
            CancellationToken token)
        {
            ThrowErrorIf.IsNullValue(config, nameof(config), nameof(GetPageAsync));

            await CreateQuery(notTracking, token).ConfigureAwait(false);

            return(await Query.ToPage(CacheService, config, token).
                   ConfigureAwait(false));
        }
Esempio n. 14
0
 public void ThrowErrorIf_IsLessThanZero_InvalidValue()
 {
     try
     {
         ThrowErrorIf.IsLessThanZero(0);
         Assert.IsTrue(true);
     }
     catch
     {
         Assert.IsTrue(false);
     }
 }
Esempio n. 15
0
        protected QueryAsync(TContext context, ICacheRepository cacheService)
        {
            ThrowErrorIf.
            HasNoCache(cacheService, typeof(QueryAsync <,>).Name);

            ThrowErrorIf.
            IsNullValue(context, nameof(context), typeof(QueryAsync <,>).Name);

            Context      = context;
            CacheService = cacheService;
            Query        = Context.Set <TValue>().AsQueryable();
        }
Esempio n. 16
0
        public virtual async Task <IReadOnlyList <TValue> > GetAllByAsync(
            Expression <Func <TValue, bool> > predicate,
            bool notTracking,
            CancellationToken token)
        {
            ThrowErrorIf.IsNullValue(predicate, nameof(predicate), nameof(GetSingleByAsync));

            await CreateQuery(notTracking, token).ConfigureAwait(false);

            return(await Query.Where(predicate).ToListAsync(token).
                   ConfigureAwait(false));
        }
        private static Expression <Func <TValue, object> > CreateExpression <TValue>(
            ParameterExpression parameter,
            PropertyInfo propertyInfo)
        {
            ThrowErrorIf.IsNullValue(parameter, nameof(parameter), nameof(CreateExpression));
            ThrowErrorIf.IsNullValue(propertyInfo, nameof(propertyInfo), nameof(CreateExpression));

            var memberExpression = Expression.PropertyOrField(parameter, propertyInfo.Name);

            var conversion = Expression.Convert(memberExpression, typeof(object));

            return(Expression.Lambda <Func <TValue, object> >(conversion, parameter));
        }
        public Func <object, object> CreateFunction <TValue>(PropertyInfo property)
        {
            ThrowErrorIf.
            IsNullValue(property, nameof(property), nameof(CreateFunction));

            var getter = property.
                         GetGetMethod(true);

            ThrowErrorIf.
            IsNullValue(getter, nameof(property), nameof(CreateFunction));

            return(ExtractMethod <TValue, Func <object, object> >(getter, property, nameof(CreateFunctionGeneric)));
        }
        public async Task <TReturn> GetData <TReturn>(IDictionary <string, TReturn> dictionary, string key, CancellationToken token)
        {
            return(await Task.Run(() =>
            {
                ThrowErrorIf.
                IsEmptyOrNullString(key, nameof(key), nameof(GetData));

                dictionary.TryGetValue(key, out var result);

                return result;
            }, token).
                   ConfigureAwait(false));
        }
Esempio n. 20
0
        public virtual async Task <TValue> GetSingleByAsync(
            Expression <Func <TValue, bool> > predicate,
            bool notTracking,
            CancellationToken token)
        {
            ThrowErrorIf.
            IsNullValue(predicate, nameof(predicate), nameof(GetSingleByAsync));

            await CreateQuery(notTracking, token).ConfigureAwait(false);

            return(await Query.SingleOrDefaultAsync(predicate, token).
                   ConfigureAwait(false));
        }
        /// <summary>
        /// Splits the list.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="list">The list.</param>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        public static IEnumerable <IEnumerable <TValue> > SplitList <TValue>(this IEnumerable <TValue> list, int size)
        {
            ThrowErrorIf.
            IsNullOrEmptyList(list, nameof(list), nameof(SplitList));

            ThrowErrorIf.
            IsLessThanOrEqualsZero(size);

            var listSplited = list.Select((x, i) => new { Index = i, Value = x })
                              .GroupBy(x => x.Index / size)
                              .Select(x => x.Select(v => v.Value));

            return(listSplited);
        }
        public Action <object, object> CreateAction <TValue>(PropertyInfo property)
        {
            ThrowErrorIf.
            IsNullValue(property, nameof(property), nameof(CreateAction));

            var setter = property.
                         GetSetMethod(true);

            ThrowErrorIf.
            IsNullValue(setter, nameof(setter), nameof(CreateAction));

            var result = ExtractMethod <TValue, Action <object, object> >(setter, property, nameof(CreateActionGeneric));

            return(result);
        }
        private TReturn ExtractMethod <TValue, TReturn>(MethodInfo method, PropertyInfo property, string nameMethod)
        {
            ThrowErrorIf.
            IsNullValue(method, nameof(method), nameof(ExtractMethod));

            var type          = typeof(ICacheRepositoryFacade);
            var genericMethod = type.GetMethod(nameMethod);
            var genericHelper = genericMethod?.
                                MakeGenericMethod(typeof(TValue), property.PropertyType);

            var extractedMethod = (TReturn)genericHelper?.
                                  Invoke(this, new object[] { method });

            return(extractedMethod);
        }
Esempio n. 24
0
        /// <summary>Determines whether this instance contains the object.</summary>
        /// <param name="constant">The constant.</param>
        /// <param name="memberExpression">The member expression.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException">Type of argument is not valid to method. &gt; {nameof(Contains)}</exception>
        /// <exception cref="InvalidOperationException">Error to get method contains. &gt; {nameof(Contains)}</exception>
        public Expression Contains(
            ConstantExpression constant,
            MemberExpression memberExpression,
            object value)
        {
            ThrowErrorIf.IsTypeNotEquals <string>(value);

            var method = typeof(string).
                         GetMethod(LambdaMethod.Contains.ToString(), new[] { typeof(string) });

            var result = Expression.
                         Call(memberExpression,
                              method ?? throw new InvalidOperationException($"Error to get method Contains > {nameof(Contains)}"),
                              constant);

            return(result);
        }
Esempio n. 25
0
        protected PageAttrAbstract(IQueryable <TIn> listEntities, IPageConfig config, ICacheRepository cache)
        {
            ThrowErrorIf.IsNullValue(config, nameof(config), typeof(PageAttrAbstract <,>).Name);

            ThrowErrorIf.IsNullValue(listEntities, nameof(listEntities), typeof(PageAttrAbstract <,>).Name);

            ThrowErrorIf.IsNullValue(cache, nameof(cache), typeof(PageAttrAbstract <,>).Name);

            Cache        = cache;
            PageConfig   = config;
            ListEntities = listEntities;
            Count        = listEntities.Count();

            Sort          = PageConfig.Sort.ToString();
            NumberPage    = PageConfig.Page;
            Order         = PageConfig.Order;
            Size          = PageConfig.Size;
            TotalElements = Count;

            TotalPage = TotalElements / Size;
        }
        /// <summary>Creates the type of the expression per.</summary>
        /// <param name="type">The type.</param>
        /// <param name="parameter">The parameter.</param>
        /// <param name="propertyInfo">The property information.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        private static Expression CreateExpressionPerType(
            this LambdaMethod type,
            ParameterExpression parameter,
            PropertyInfo propertyInfo,
            object value)
        {
            ThrowErrorIf.IsNullValue(parameter, nameof(parameter), nameof(CreateExpressionPerType));
            ThrowErrorIf.IsNullValue(propertyInfo, nameof(propertyInfo), nameof(CreateExpressionPerType));

            var memberExpression = Expression.Property(parameter, propertyInfo);
            var constant         = Expression.Constant(value);

            switch (type)
            {
            case LambdaMethod.Contains:
                var result = FilterFacade.Contains(constant, memberExpression, value);
                return(result);

            case LambdaMethod.GreaterThan:
                result = FilterFacade.GreaterThan(constant, memberExpression, value);
                return(result);

            case LambdaMethod.LessThan:
                result = FilterFacade.LessThan(constant, memberExpression, value);
                return(result);

            case LambdaMethod.GreaterThanOrEqual:
                result = FilterFacade.GreaterThanOrEqual(constant, memberExpression, value);
                return(result);

            case LambdaMethod.LessThanOrEqual:
                result = FilterFacade.LessThanOrEqual(constant, memberExpression, value);
                return(result);

            default:
                result = FilterFacade.Equal(constant, memberExpression, value);
                return(result);
            }
        }
Esempio n. 27
0
 public void ThrowErrorIf_IsEqualsZero() =>
 Assert.Throws <LessThanOrEqualsZeroException>(() =>
 {
     ThrowErrorIf.IsLessThanOrEqualsZero(0);
 }
                                               );
Esempio n. 28
0
 public void ThrowErrorIf_IsLessThanZero() =>
 Assert.Throws <LessThanZeroException>(() =>
 {
     ThrowErrorIf.IsLessThanZero(-1);
 }
                                       );
Esempio n. 29
0
 public void ThrowErrorIf_IsNullOrEmptyList() =>
 Assert.Throws <ListNullOrEmptyException>(() =>
                                          ThrowErrorIf.IsNullOrEmptyList(new List <string>(), string.Empty, string.Empty));
Esempio n. 30
0
 public void ThrowErrorIf_IsEmptyOrNullString() =>
 Assert.Throws <ArgumentNullException>(() => ThrowErrorIf.IsEmptyOrNullString("", string.Empty, string.Empty));