Exemplo n.º 1
0
        public async Task <T> GetLocalizedEntity <T>(T entity, string cultureCode) where T : IEntity
        {
            var properties = entity.GetType().GetPropertiesWithAttribute <LocalizableAttribute>();

            foreach (var property in properties)
            {
                var key          = TermsHelper.GenerateTermsKey(entity, property);
                var defaultValue = (string)property.FastGetValue(entity);
                if (defaultValue.IsNullOrEmpty())
                {
                    continue;
                }
                var cachedValue = _cacheManager.Get <string>(key, () =>
                {
                    var terms = _termsRepository.Find(TermsSpecifications.WithKeyAndCulture(key, cultureCode));
                    if (terms != null)
                    {
                        return(terms.Value);
                    }
                    else
                    {
                        return(defaultValue);
                    }
                });
                if (cachedValue != null && cachedValue.Equals(defaultValue, System.StringComparison.OrdinalIgnoreCase) == false)
                {
                    property.FastSetValue(entity, cachedValue);
                }
            }

            return(entity);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Method that extends IQueryable<T> allowing to search query with request properties
        /// </summary>
        /// <typeparam name="TSource">Generic type of the entity</typeparam>
        /// <param name="source">Self IQueryable<T> instance</param>
        /// <param name="request">Self IWrapRequest<T> instance</param>
        /// <returns>Returns IQueryable instance with with the configuration for search</returns>
        public static IQueryable <TSource> Search <TSource>(
            this IQueryable <TSource> source,
            IWrapRequest <TSource> request
            ) where TSource : class
        {
            var search = request.Search();

            var query       = search.Query;
            var queryStrict = search.Strict;
            var queryPhrase = search.Phrase;

            if (string.IsNullOrWhiteSpace(query))
            {
                return(source);
            }

            var queryTokens = TermsHelper.GetSearchTerms(query, queryPhrase);

            query = string.Join("+", queryTokens.ToArray());

            if (queryTokens.Count == 0)
            {
                return(source);
            }

            List <string> searchableProperties = new List <string>();

            searchableProperties = typeof(TSource).GetProperties().Where(x =>
                                                                         !request.IsPropertySuppressed(x.Name) && !TypesHelper.TypeIsComplex(x.PropertyType)
                                                                         ).Select(x => x.Name).ToList();

            var criteriaExp = LambdasHelper.GenerateSearchCriteriaExpression <TSource>(searchableProperties, queryTokens, queryStrict);

            return(source.Where(criteriaExp));
        }
Exemplo n.º 3
0
        private static List <SelectedProperty> GetValidProperties <TModel>(
            IWrapRequest <TModel> source,
            Type type,
            List <string> requestProperties,
            string rootName       = null,
            bool isFromCollection = false)
            where TModel : class
        {
            var properties = new List <SelectedProperty>();

            type.GetProperties()
            .Where(property =>
                   !source.IsPropertySuppressedResponse(string.Join(".", TermsHelper.GetTerms(rootName, ".").Union(new List <string> {
                property.Name
            }).Where(term => !string.IsNullOrWhiteSpace(term)))) &&
                   (
                       (requestProperties.Count == 0 && IsToLoadComplexPropertyWhenNotRequested(TypesHelper.TypeIsComplex(property.PropertyType), ConfigurationService.GetConfiguration().ByDefaultLoadComplexProperties, string.IsNullOrWhiteSpace(rootName))) ||
                       (requestProperties.Any(requested => TermsHelper.GetTerms(requested, ".").FirstOrDefault().ToLower().Equals(property.Name.ToLower())))
                   )
                   )
            .ToList()
            .ForEach(property =>
            {
                if (!TypesHelper.TypeIsComplex(property.PropertyType))
                {
                    properties.Add(new SelectedProperty
                    {
                        RequestedPropertyName = string.Join(".", TermsHelper.GetTerms(rootName, ".").Union(new List <string> {
                            property.Name
                        }).Where(term => !string.IsNullOrWhiteSpace(term))),
                        PropertyName     = property.Name,
                        RootPropertyName = rootName,
                        PropertyType     = property.PropertyType,
                        PropertyInfo     = property
                    });
                }
                else if (ConfigurationService.GetConfiguration().ByDefaultLoadComplexProperties)
                {
                    properties.AddRange(
                        GetValidProperties(
                            source,
                            TypesHelper.TypeIsCollection(property.PropertyType) ? property.PropertyType.GetGenericArguments()[0] : property.PropertyType,
                            requestProperties
                            .Where(requested => TermsHelper.GetTerms(requested, ".").FirstOrDefault().ToLower().Equals(property.Name.ToLower()))
                            .Select(requested => string.Join(".", TermsHelper.GetTerms(requested, ".").Skip(1)))
                            .Where(requested => !string.IsNullOrWhiteSpace(requested))
                            .ToList(),
                            string.Join(".", TermsHelper.GetTerms(rootName, ".").Union(new List <string> {
                        property.Name
                    }).Where(term => !string.IsNullOrWhiteSpace(term))),
                            TypesHelper.TypeIsCollection(property.PropertyType)
                            )
                        );
                }
            });

            return(properties);
        }