Пример #1
0
        public IEnumerable <Employee> Filter(FilterModel model, string[] includes = null)
        {
            Expression <Func <Employee, bool> > FilterExpression(FilterModel _model)
            {
                Expression <Func <Employee, bool> > expression = null;

                if (_model == null)
                {
                    return(null);
                }

                if (!String.IsNullOrEmpty(_model.Name))
                {
                    expression = x => x.Name.Contains(_model.Name.ToString());
                }

                if (!String.IsNullOrEmpty(_model.Position))
                {
                    expression = ExpressionCombiner.And <Employee>(expression, x => x.Position.Contains(_model.Position.ToString()));
                }

                return(expression);
            }

            var result = base.Filter(FilterExpression(model), includes);

            return(result);
        }
        public void WillThrowOnIncompatibleExpressions()
        {
            var combiner = new ExpressionCombiner <Foo, Bar>();

            Expression <Func <Foo, Bar> > source = (x) => new Bar {
                A = x.A
            };
            Expression <Func <Foo, Bar> > target = (x) => null;

            Assert.Throws <ArgumentException>(() =>
            {
                combiner.CombineIntoMapperWithConstructor(source, target);
            });

            var existing = new Bar();
            Expression <Func <Foo, Bar> > source2 = (x) => existing;
            Expression <Func <Foo, Bar> > target2 = (x) => new Bar {
                B = x.B
            };

            Assert.Throws <ArgumentException>(() =>
            {
                combiner.CombineIntoMapperWithConstructor(source, target);
            });
        }
        public void CanMergeTwoBasciMemberInitLambdas()
        {
            var combiner = new ExpressionCombiner <Foo, Bar>();

            Expression <Func <Foo, Bar> > source = (x) => new Bar {
                A = x.A
            };
            Expression <Func <Foo, Bar> > target = (x) => new Bar {
                B = x.B
            };

            var combined = combiner.CombineIntoMapperWithConstructor(source, target);

            Assert.AreEqual(combined.ToString(), "x => new Bar() {A = x.A, B = x.B}");

            Expression <Func <Foo, Bar> > source2 = (x) => new Bar()
            {
                A = x.A
            };
            Expression <Func <Foo, Bar> > target2 = (x) => new Bar()
            {
                B = x.B
            };

            var combined2 = combiner.CombineIntoMapperWithConstructor(source2, target2);

            Assert.AreEqual(combined.ToString(), "x => new Bar() {A = x.A, B = x.B}");
        }
        public void CanProduceMapperExpressionForExistingClasses()
        {
            var combiner = new ExpressionCombiner <Foo, Bar>();

            Expression <Func <Foo, Bar> > a = (x) => new Bar
            {
                A = x.A,
                E = new Bar {
                    A = 1
                }
            };
            Expression <Func <Foo, Bar> > b = (x) => new Bar {
                B = x.B
            };

            var combined = combiner.CombineIntoMapperForExisting(a, b);

            var foo = new Foo()
            {
                A = 5,
                B = 10
            };
            var bar = new Bar();

            combined.Compile().Invoke(foo, bar);

            Assert.AreEqual(bar.A, 5);
            Assert.AreEqual(bar.B, 10);
            Assert.NotNull(bar.E);
            Assert.AreEqual(bar.E.A, 1);
        }
Пример #5
0
        private Expression <Func <Gig, bool> > _getMyAttendingGigFilterExpression(GigFilterParams filter)
        {
            Expression <Func <Gig, bool> > filterExpression = g =>
                                                              g.DateTime > DateTime.Now;

            return(string.IsNullOrWhiteSpace(filter.SearchTerm) ? filterExpression :
                   ExpressionCombiner.And(filterExpression, getSeachFilter(filter)));
        }
Пример #6
0
        private Expression <Func <Gig, bool> > _getGigFilterExpression(GigFilterParams filter)
        {
            Expression <Func <Gig, bool> > filterExpression = g =>
                                                              g.DateTime > DateTime.Now &&
                                                              g.Artist.Activated &&
                                                              !g.IsCancelled;

            return(string.IsNullOrWhiteSpace(filter.SearchTerm) ? filterExpression :
                   ExpressionCombiner.And(filterExpression, getSeachFilter(filter)));
        }
        public void DoesNotThrowOnEmptyInitializers()
        {
            var combiner = new ExpressionCombiner <Foo, Bar>();

            Expression <Func <Foo, Bar> > source = (x) => new Bar {
            };
            Expression <Func <Foo, Bar> > target = (x) => new Bar {
            };

            var combined = combiner.CombineIntoMapperWithConstructor(source, target);

            Assert.AreEqual(combined.ToString(), "x => new Bar() {}");
        }
Пример #8
0
        /// <summary>
        /// Deletes all matching entities permanently for given predicate
        /// </summary>
        /// <typeparam name="TEntity">Entity type</typeparam>
        /// <typeparam name="TPrimaryKey">Primary key type</typeparam>
        /// <param name="repository">Repository</param>
        /// <param name="predicate">Predicate to filter entities</param>
        /// <returns></returns>
        public static async Task <int> BatchDeleteAsync <TEntity, TPrimaryKey>([NotNull] this IRepository <TEntity, TPrimaryKey> repository, [NotNull] Expression <Func <TEntity, bool> > predicate)
            where TEntity : Entity <TPrimaryKey>
        {
            Check.NotNull(repository, nameof(repository));
            Check.NotNull(predicate, nameof(predicate));

            var query = repository.GetAll().IgnoreQueryFilters();

            var abpFilterExpression = GetFilterExpressionOrNull <TEntity, TPrimaryKey>(repository.GetIocResolver());
            var filterExpression    = ExpressionCombiner.Combine(predicate, abpFilterExpression);

            query = query.Where(filterExpression);

            return(await query.DeleteAsync());
        }
        public void CanMergeTwoAdvancedMemberInitLambdas()
        {
            var combiner = new ExpressionCombiner <Foo, Bar>();

            Expression <Func <Foo, Bar> > source = (x) => new Bar {
                E = new Bar {
                    A = 1
                }
            };
            Expression <Func <Foo, Bar> > target = (x) => new Bar {
                A = x.D.FirstOrDefault()
            };

            var combined = combiner.CombineIntoMapperWithConstructor(source, target);

            Assert.AreEqual(combined.ToString(), "x => new Bar() {E = new Bar() {A = 1}, A = x.D.FirstOrDefault()}");
        }
        private static Expression <Func <TEntity, bool> > GetFilterExpressionOrNull <TEntity, TPrimaryKey>(IIocResolver iocResolver)
            where TEntity : Entity <TPrimaryKey>
        {
            Expression <Func <TEntity, bool> > expression = null;

            using (var scope = iocResolver.CreateScope())
            {
                var currentUnitOfWorkProvider = scope.Resolve <ICurrentUnitOfWorkProvider>();

                if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
                {
                    var isSoftDeleteFilterEnabled = currentUnitOfWorkProvider.Current?.IsFilterEnabled(AbpDataFilters.SoftDelete) == true;
                    if (isSoftDeleteFilterEnabled)
                    {
                        Expression <Func <TEntity, bool> > softDeleteFilter = e => !((ISoftDelete)e).IsDeleted;
                        expression = softDeleteFilter;
                    }
                }

                if (typeof(IMayHaveTenant).IsAssignableFrom(typeof(TEntity)))
                {
                    var isMayHaveTenantFilterEnabled = currentUnitOfWorkProvider.Current?.IsFilterEnabled(AbpDataFilters.MayHaveTenant) == true;
                    var currentTenantId = GetCurrentTenantIdOrNull(iocResolver);

                    if (isMayHaveTenantFilterEnabled)
                    {
                        Expression <Func <TEntity, bool> > mayHaveTenantFilter = e => ((IMayHaveTenant)e).TenantId == currentTenantId;
                        expression = expression == null ? mayHaveTenantFilter : ExpressionCombiner.Combine(expression, mayHaveTenantFilter);
                    }
                }

                if (typeof(IMustHaveTenant).IsAssignableFrom(typeof(TEntity)))
                {
                    var isMustHaveTenantFilterEnabled = currentUnitOfWorkProvider.Current?.IsFilterEnabled(AbpDataFilters.MustHaveTenant) == true;
                    var currentTenantId = GetCurrentTenantIdOrNull(iocResolver);

                    if (isMustHaveTenantFilterEnabled)
                    {
                        Expression <Func <TEntity, bool> > mustHaveTenantFilter = e => ((IMustHaveTenant)e).TenantId == currentTenantId;
                        expression = expression == null ? mustHaveTenantFilter : ExpressionCombiner.Combine(expression, mustHaveTenantFilter);
                    }
                }
            }

            return(expression);
        }
        public void WillThrowOnConstructorsWithArguments()
        {
            var combiner = new ExpressionCombiner <Foo, Bar>();

            Expression <Func <Foo, Bar> > source = (x) => new Bar(1)
            {
                B = x.B
            };
            Expression <Func <Foo, Bar> > target = (x) => new Bar()
            {
                C = x.C
            };

            Assert.Throws <ArgumentException>(() =>
            {
                combiner.CombineIntoMapperWithConstructor(source, target);
            });
        }
Пример #12
0
        public Expression <Func <T, bool> > ToExpression <T>() where T : class
        {
            if (Filters.Count == 0)
            {
                return(x => true);
            }
            if (Filters.Count == 1)
            {
                return(Filters.First().ToExpression <T>());
            }
            Expression <Func <T, bool> > expression = Filters.First().ToExpression <T>();

            expression = Filters.ToList()
                         .Skip(1)
                         .Aggregate(expression, (current, filter) =>
                                    Logic == FilterLogic.And
                        ? ExpressionCombiner.And(current, filter.ToExpression <T>())
                        : ExpressionCombiner.Or(current, filter.ToExpression <T>()));
            return(expression);
        }
Пример #13
0
        public IEnumerable <ApplicationUser> GetUsersByRoleId(string roleId, string query = null)
        {
            Expression <Func <ApplicationUser, bool> > filterExpression = u =>
                                                                          u.Activated &&
                                                                          u.Roles.Any(r => r.RoleId == roleId);

            if (query == null)
            {
                return(_context.Users
                       .Where(filterExpression)
                       .OrderBy(u => u.Name).ToList());
            }
            {
                Expression <Func <ApplicationUser, bool> > nameFilter = u =>
                                                                        u.Name.Contains(query);
                filterExpression = ExpressionCombiner.And(filterExpression, nameFilter);
            }
            return(_context.Users
                   .Where(filterExpression)
                   .OrderBy(u => u.Name).ToList());
        }
Пример #14
0
 protected virtual Expression <Func <T, bool> > CombineExpressions <T>(Expression <Func <T, bool> > expression1, Expression <Func <T, bool> > expression2)
 {
     return(ExpressionCombiner.Combine(expression1, expression2));
 }
Пример #15
0
 /// <summary>
 /// 合并表达式
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="expression1"></param>
 /// <param name="expression2"></param>
 /// <returns></returns>
 public static Expression <Func <T, bool> > CombineExpressions <T>(this Expression <Func <T, bool> > expression1, Expression <Func <T, bool> > expression2)
 {
     return(ExpressionCombiner.Combine(expression1, expression2));
 }