Пример #1
0
 public void Any_Specifications_ShouldBeFalse()
 {
     Specification <Entity> .Any(_value1ShouldBe1, _value2ShouldBe2)
     .IsSatisfiedBy(new Entity {
         Value1 = 2, Value2 = 1
     });
 }
        public void InvokeAny_ReturnAnySpecification()
        {
            var specification = MockComplexSpecification <int> .True();

            var expected = new AnySpecification <FakeType, int>(specification, true);

            var sut = Specification.Any <FakeType, int>(specification);

            Assert.Equal(expected, sut, new SpecificationComparer());
        }
        public void InvokeAnyProperty_ReturnPropertySpecification()
        {
            var specification = MockComplexSpecification <int> .True();

            var expected = new PropertySpecification <FakeType, IEnumerable <int> >(
                ft => ft.Fourth, new AnySpecification <IEnumerable <int>, int>(specification, true));

            var sut = Specification.Any <FakeType, int>(
                ft => ft.Fourth, specification);

            Assert.Equal(expected, sut, new SpecificationComparer());
        }
Пример #4
0
        public override IQueryable <TEntity> Search(Specification <TEntity> filter, SortSpecification <TEntity> sorter, params string[] includes)
        {
            if (filter == null)
            {
                filter = Specification <TEntity> .Any();
            }

            if (sorter == null)
            {
                sorter = SortSpecification <TEntity> .None;
            }

            var query = Context.Set <TEntity>().Where(filter);

            if (includes != null)
            {
                foreach (var include in includes)
                {
                    query = query.Include(include);
                }
            }

            if (sorter.Count > 0)
            {
                var sorts        = sorter.Specifications.ToList();
                var orderedQuery = sorts[0].Item2 == SortDirection.Asc
                                   ? query.OrderBy(sorts[0].Item1)
                                   : query.OrderByDescending(sorts[0].Item1);
                for (var i = 1; i < sorts.Count; i++)
                {
                    orderedQuery = sorts[i].Item2 == SortDirection.Asc
                 ? orderedQuery.OrderBy(sorts[i].Item1)
                 : orderedQuery.OrderByDescending(sorts[i].Item1);
                }

                query = orderedQuery;
            }

            return(query);
        }
Пример #5
0
        public Specification <TAggregateRoot> Parse()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(filter))
                {
                    return(Specification <TAggregateRoot> .Any());
                }

                var parser = new ExpressionParser(filter);
                var expr   = parser.Parse();

                return(Specification <TAggregateRoot> .Eval(Expression.Lambda <Func <TAggregateRoot, bool> >(expr, param)));
            }
            catch (Exception ex)
            {
                if (ex is FilterParseException)
                {
                    throw;
                }
                throw new FilterParseException(ex);
            }
        }
Пример #6
0
        public SqlToEntitiesIntegrationData()
        {
            Specification.LinqToEntities = true;

            var spec1 = Specification
                        .InclusiveBetween <Customer, int>(c => c.CustomerId, 100, 200)
                        .And()
                        .Equal(c => c.LastName, "Kowalski")
                        .And()
                        .True(c => c.IsActive)
                        .And(Specification.ForProperty <Customer, string>(c => c.Email, Specification
                                                                          .MinLength <string>(8)))
                        .And()
                        .NotLengthBetween(c => c.Comments, 1, 10)
                        .And()
                        .ForProperty(c => c.Items, Specification
                                     .NotEmpty <ICollection <Item> >()
                                     .And()
                                     .NotMinLength(10)
                                     .And(Specification
                                          .Any <ICollection <Item>, Item>(Specification.Or(Specification
                                                                                           .NotContains <Item>(i => i.Name, "Super")
                                                                                           .And()
                                                                                           .ExclusiveBetween(i => i.Price, 50, 51),
                                                                                           Specification
                                                                                           .NotMaxLength <Item, string>(i => i.Name, 3)
                                                                                           .And()
                                                                                           .Cast(i => i.Price, Specification.NotLessThan(1))))))
                        .And()
                        .NotNull(c => c.CreditCard)
                        .And(Specification
                             .GreaterThan <Customer, DateTime>(c => c.CreditCard.ValidityDate, new DateTime(2019, 6, 1))
                             .Or()
                             .LessThanOrEqual(c => c.CreditCard.ValidityDate, new DateTime(2019, 3, 12)))
                        .And()
                        .Null(c => c.Caretaker);

            var spec2 = Specification
                        .NotLessThanOrEqual <Customer, int>(c => c.CustomerId, 100)
                        .And()
                        .NotEqual(c => c.LastName, "Kowalski")
                        .And()
                        .False(c => c.IsActive)
                        .And()
                        .Empty(c => c.Items)
                        .And()
                        .ForProperty(c => c.CreditCard, Specification
                                     .NotLength <CreditCard, string>(cc => cc.CardNumber, 18)
                                     .Not()
                                     .Or()
                                     .NotExclusiveBetween(cc => cc.ValidityDate, new DateTime(2019, 1, 1), new DateTime(2019, 12, 31)));

            var spec3 = Specification
                        .ForProperty <Customer, int>(c => c.CustomerId, Specification
                                                     .GreaterThanOrEqual(1000)
                                                     .And()
                                                     .NotGreaterThanOrEqual(2000))
                        .And()
                        .LengthBetween(c => c.Comments, 1, 10)
                        .And()
                        .ForProperty(c => c.Caretaker, Specification
                                     .NotNull <Customer>()
                                     .And()
                                     .NotGreaterThan(ct => ct.CustomerId, 200))
                        .And()
                        .ForProperty(c => c.Items, Specification
                                     .Length <ICollection <Item> >(1)
                                     .And()
                                     .Any(Specification
                                          .Expression <Item>(i => i.Paid && i.Price > 0.0)
                                          .And()
                                          .NotInclusiveBetween(i => i.ItemId, 60, 80)
                                          .And(Specification
                                               .And <Item, string>(i => i.Name, Specification
                                                                   .Contains("Thor"),
                                                                   Specification
                                                                   .MaxLength <string>(20)))
                                          .And()
                                          .LessThanOrEqual(i => i.Price, 20.0)));

            AddValid(spec1, 152);
            AddValid(spec2, 174);
            AddValid(spec3, 1000);

            AddInvalid(spec1.Not(), 174, 1000);
            AddInvalid(spec2.Not(), 152, 1000);
            AddInvalid(spec3.Not(), 152, 174);

            Specification.LinqToEntities = false;
        }
Пример #7
0
 public virtual IQueryable <TEntity> Search(SortSpecification <TEntity> sorter, params string[] includes)
 {
     return(this.Search(Specification <TEntity> .Any(), sorter, includes));
 }