Exemple #1
0
        internal OrderByFilterOperation ConstructOrderByExpression <T>(bool nested = false) where T : class
        {
            var type = typeof(T);

            if (string.IsNullOrWhiteSpace(PropertyName))
            {
                PropertyName = EFCoreSugarPropertyCollection.GetDefaultPropertyName(type);
            }

            var        command      = OrderByDirection == OrderByDirection.Descending ? nested ? "ThenByDescending" : "OrderByDescending" : nested ? "ThenBy" : "OrderBy";
            var        parameter    = Expression.Parameter(type, "p");
            Expression memberAccess = parameter;

            foreach (var property in PropertyName.Split('.'))
            {
                if (typeof(IEnumerable).IsAssignableFrom(memberAccess.Type))
                {
                    var subtype = memberAccess.Type.GetGenericArguments()[0];
                    memberAccess = Expression.Call(
                        typeof(Enumerable),
                        "FirstOrDefault",
                        new Type[] { subtype },
                        memberAccess);
                }
                memberAccess = Expression.Property(memberAccess, property);
            }

            return(new OrderByFilterOperation(PropertyName, command, memberAccess.Type, Expression.Lambda(memberAccess, parameter)));
        }
Exemple #2
0
        public static void BuildFilters()
        {
            var types = GetAllTypesInAssemblies(typeof(Filter));

            foreach (var type in types)
            {
                EFCoreSugarPropertyCollection.RegisterFilterProperties(type);
                //TODO: prebuild some of the expressions here also?
            }
        }
Exemple #3
0
        public Filter()
        {
            //save this so we only reflect once
            ThisType = this.GetType();

            if (!EFCoreSugarPropertyCollection.FilterTypeProperties.ContainsKey(ThisType))
            {
                EFCoreSugarPropertyCollection.RegisterFilterProperties(ThisType);
            }
        }
Exemple #4
0
        public BaseDbRepository(T context, IServiceProvider serviceProvider)
        {
            DBContext        = context;
            _serviceProvider = serviceProvider;
            var thisType = this.GetType();

            if (!EFCoreSugarPropertyCollection.BaseDbRepositoryGroupTypeProperties.TryGetValue(thisType, out var props))
            {
                props = EFCoreSugarPropertyCollection.RegisterBaseDbRepositoryGroupProperties(thisType);
            }

            foreach (var prop in props)
            {
                var group = serviceProvider.GetService(prop.PropertyType) as IBaseRepositoryGroup;
                if (group is null)
                {
                    continue;
                }
                group.ParentBaseRepository = this;
                prop.SetValue(this, group);
            }
        }
Exemple #5
0
        public static FilteredQuery <T> Filter <T>(this FilteredQuery <T> baseQuery, Filter filter) where T : class
        {
            //we will preserve the first filter paging settings always
            var newFilteredQuery = filter.ApplyFilter(baseQuery.Query);

            baseQuery.Query = newFilteredQuery.Query;

            if (baseQuery.OrderByProperties.Count == 0)
            {
                baseQuery.OrderByProperties.Add(baseQuery.OrderBys.First().PropertyName ?? EFCoreSugarPropertyCollection.GetDefaultPropertyName(typeof(T)));
            }

            //we want to ignore additional orderbys if they are not set explicitly, this likely means they are doing something else and dont care about the order by
            var newOrderBy = newFilteredQuery.OrderBys.First();

            if (newOrderBy.PropertyName != null && !baseQuery.OrderByProperties.Contains(newOrderBy.PropertyName))
            {
                baseQuery.OrderBys.Add(newOrderBy);
                baseQuery.OrderByProperties.Add(newOrderBy.PropertyName);
            }
            return(baseQuery);
        }