public void Get_Dictionary_Attributes()
        {
            InitCache();
            var attr = cache.GetDictionaryAttribute(typeof(T).Name);

            Assert.IsNotNull(attr);
        }
Example #2
0
        /// <summary>
        /// Generate lambda method
        /// </summary>
        /// <param name="filter">Object filter</param>
        /// <typeparam name="TValue">Type Entity</typeparam>
        /// <typeparam name="F">Type Filter</typeparam>
        /// <returns>Predicate generated</returns>
        public static Expression <Func <TValue, bool> > GenerateLambda <TValue, TFilter>(
            this TFilter filter,
            ICacheRepository cacheRepository)
            where TValue : class
            where TFilter : class, IFilter
        {
            var param           = Expression.Parameter(typeof(TValue));
            var typeNameTFilter = typeof(TFilter).Name;
            var typeNameTValue  = typeof(TValue).Name;
            var mergeOption     = LambdaMerge.And;

            Expression <Func <TValue, bool> > predicate = null;
            LambdaMethod methodOption;

            cacheRepository.
            GetDictionaryMethodGet(typeNameTFilter).
            ToList().
            ForEach(propertyTFilter =>
            {
                Expression lambda         = null;
                string namePropertyOnE    = null;
                var namePropertyOnTFilter = propertyTFilter.Key;
                var propertyValueTFilter  = propertyTFilter.Value(filter);

                if (ValidateProperty(propertyValueTFilter))
                {
                    var customAttributes = cacheRepository.GetDictionaryAttribute(typeNameTFilter);
                    if (customAttributes.TryGetValue(namePropertyOnTFilter, out Dictionary <string, CustomAttributeTypedArgument> attributes))
                    {
                        if (attributes.TryGetValue("NameProperty", out CustomAttributeTypedArgument attribute))
                        {
                            namePropertyOnE = attribute.Value.ToString();
                        }
                        if (attributes.TryGetValue("MethodOption", out attribute))
                        {
                            var property = cacheRepository.GetProperty(typeNameTValue, namePropertyOnE ?? propertyTFilter.Key.ToString());
                            methodOption = (LambdaMethod)attribute.Value;
                            lambda       = methodOption.SetExpressionType(param, property, propertyValueTFilter);
                        }
                        if (!lambda.IsNull(nameof(GenerateLambda), nameof(lambda)))
                        {
                            predicate = predicate == null ?
                                        lambda.MergeExpressions <TValue>(param) :
                                        predicate.MergeExpressions(mergeOption, param, lambda.MergeExpressions <TValue>(param));

                            if (attributes.TryGetValue("MergeOption", out attribute))
                            {
                                mergeOption = (LambdaMerge)attribute.Value;
                            }
                            else
                            {
                                mergeOption = LambdaMerge.And;
                            }
                        }
                    }
                }
            });
            return(predicate);
        }
        /// <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 static async Task <Expression <Func <TValue, object> > > CreateGenericOrderBy <TValue, TFilter>(
            this IPageConfig pageConfig,
            ICacheRepository cacheRepository,
            CancellationToken token)
        {
            var key = typeof(TFilter).Name;

            var dictionary = await cacheRepository.
                             GetDictionaryAttribute(key, pageConfig.Order, token).
                             ConfigureAwait(false);

            dictionary.TryGetValue(NameProperty, out var nameField);

            var order = (string)nameField.Value ?? pageConfig.Order;

            return(await GetGenericExpression <TValue>(order, cacheRepository, token).
                   ConfigureAwait(false));
        }