Ejemplo n.º 1
0
        protected virtual IEnumerable <TViewModel> GetByCriteria <TAggregateRoot, TViewModel>(
            IRepository <TAggregateRoot> repository,
            string filterExpression = null, string sortingField = null, SortOrder?sortingOrder = null)
            where TAggregateRoot : class, IAggregateRoot
            where TViewModel : ViewModel
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }

            Specification <TAggregateRoot> querySpecification = new AnySpecification <TAggregateRoot>();

            if (!string.IsNullOrEmpty(filterExpression))
            {
                querySpecification =
                    Specification <TAggregateRoot> .Eval(
                        DynamicExpression.ParseLambda <TAggregateRoot, bool>(filterExpression));
            }
            Expression <Func <TAggregateRoot, dynamic> > sortPredicate = null;
            var sortOrder = sortingOrder ?? SortOrder.Unspecified;

            if (!string.IsNullOrEmpty(sortingField))
            {
                sortPredicate = DynamicExpression.ParseLambda <TAggregateRoot, dynamic>(sortingField);
            }
            return(repository.FindAll(querySpecification, sortPredicate, sortOrder).Project().To <TViewModel>());
        }
		public void Test_Any_Normal()
		{
			AnySpecification s = new AnySpecification(new PredicateSpecification<int>(delegate(int i) { return i > 0; }));
			Assert.IsFalse(s.Test(new int[] { 0, 0, 0 }).Success);
			Assert.IsTrue(s.Test(new int[] { 0, 0, 1 }).Success);
			Assert.IsTrue(s.Test(new int[] { 1, 1, 1 }).Success);
		}
        public void Test_Any_InvalidType()
        {
            // cannot test a non-enumerable object
            AnySpecification s = new AnySpecification(AlwaysTrue);

            s.Test(new object());
        }
		public void Test_Any_Empty()
		{
			AnySpecification s1 = new AnySpecification(AlwaysFalse);
			Assert.IsFalse(s1.Test(new object[0]).Success);

			AnySpecification s2 = new AnySpecification(AlwaysTrue);
			Assert.IsFalse(s2.Test(new object[0]).Success);
		}
        public void Test_Any_Normal()
        {
            AnySpecification s = new AnySpecification(new PredicateSpecification <int>(delegate(int i) { return(i > 0); }));

            Assert.IsFalse(s.Test(new int[] { 0, 0, 0 }).Success);
            Assert.IsTrue(s.Test(new int[] { 0, 0, 1 }).Success);
            Assert.IsTrue(s.Test(new int[] { 1, 1, 1 }).Success);
        }
Ejemplo n.º 6
0
        public void Match_should_always_return_true()
        {
            var specification = new AnySpecification <AnySpecificationTest>();

            var matches = specification.Matches(null);

            Assert.IsTrue(matches);
        }
Ejemplo n.º 7
0
        public void Match_should_always_return_true()
        {
            var specification = new AnySpecification<AnySpecificationTest>();

            var matches = specification.Matches(null);

            Assert.IsTrue(matches);
        }
            public void NullCollectionLinqToEntities_NoException()
            {
                var specification = MockComplexSpecification <int> .True();

                var sut       = new AnySpecification <int[], int>(specification, true);
                var exception = Record.Exception(() => sut.IsSatisfiedBy(null));

                Assert.Null(exception);
            }
Ejemplo n.º 9
0
        /// <summary>
        ///     Gets the by criteria.
        /// </summary>
        /// <typeparam name="TAggregateRoot">The type of the aggregate root.</typeparam>
        /// <typeparam name="TViewModel">The type of the view model.</typeparam>
        /// <param name="repository">The repository.</param>
        /// <param name="criteria">The criteria.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">repository</exception>
        /// <exception cref="System.InvalidOperationException">
        ///     Cannot perform a paging query: The paging query requires the sorting
        ///     but neither SortPredicate nor SortOrder was specified.
        /// </exception>
        protected virtual IEnumerable <TViewModel> GetByCriteria <TAggregateRoot, TViewModel>(
            IRepository <TAggregateRoot> repository,
            QueryCriteria criteria)
            where TAggregateRoot : class, IAggregateRoot
            where TViewModel : ViewModel
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }

            if (criteria == null)
            {
                return(repository.FindAll()
                       .Project().To <TViewModel>());
            }

            Specification <TAggregateRoot> querySpecification = new AnySpecification <TAggregateRoot>();

            if (!string.IsNullOrEmpty(criteria.FilterExpression))
            {
                querySpecification =
                    Specification <TAggregateRoot> .Eval(
                        DynamicExpression.ParseLambda <TAggregateRoot, bool>(criteria.FilterExpression));
            }

            Expression <Func <TAggregateRoot, dynamic> > sortPredicate = null;

            if (!string.IsNullOrEmpty(criteria.SortingField))
            {
                sortPredicate = DynamicExpression.ParseLambda <TAggregateRoot, dynamic>(criteria.SortingField);
            }

            var sortOrder = SortOrder.Unspecified;

            if (criteria.SortingOrder.HasValue)
            {
                sortOrder = criteria.SortingOrder.Value;
            }

            if (criteria.PageSize.HasValue && criteria.PageNumber.HasValue)
            {
                if (sortPredicate == null || sortOrder == SortOrder.Unspecified)
                {
                    throw new InvalidOperationException(
                              "Cannot perform a paging query: The paging query requires the sorting but neither SortPredicate nor SortOrder was specified.");
                }
                return(repository.FindAll(querySpecification, sortPredicate, sortOrder,
                                          criteria.PageNumber.Value, criteria.PageSize.Value)
                       .CastPagedResult <TAggregateRoot, TViewModel>(Mapper.Map <TAggregateRoot, TViewModel>));
            }
            return(repository
                   .FindAll(querySpecification, sortPredicate, sortOrder)
                   .Project()
                   .To <TViewModel>());
        }
            public void AnyElementsValid_ReturnTrue <T, TType>(T candidate, Expression <Func <TType, bool> > expression)
                where T : IEnumerable <TType>
            {
                var specification = new MockComplexSpecification <TType>(expression);
                var sut           = new AnySpecification <T, TType>(specification);

                var result = sut.IsSatisfiedBy(candidate);

                Assert.True(result);
            }
        public void InvokeCompositeAny_ReturnAnySpecification()
        {
            var specification = MockComplexSpecification <int> .True();

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

            var sut = new MockCompositeSpecification <FakeType>().Any(specification);

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

                var sut       = new AnySpecification <int[], int>(specification, true);
                var exception = Record.Exception(() => sut.GetExpression().Compile().Invoke(null));

                Assert.NotNull(exception);
                Assert.IsType <ArgumentNullException>(exception);
            }
        public void Test_Any_Empty()
        {
            AnySpecification s1 = new AnySpecification(AlwaysFalse);

            Assert.IsFalse(s1.Test(new object[0]).Success);

            AnySpecification s2 = new AnySpecification(AlwaysTrue);

            Assert.IsFalse(s2.Test(new object[0]).Success);
        }
            public void NotAnyElementsValid_ReturnFalse <T, TType>(T candidate, Expression <Func <TType, bool> > expression)
                where T : IEnumerable <TType>
            {
                candidate = candidate?.ToString() != "null" ? candidate : default;
                var specification = new MockComplexSpecification <TType>(expression);
                var sut           = new AnySpecification <T, TType>(specification);

                var result = sut.IsSatisfiedBy(candidate);

                Assert.False(result);
            }
        public void Test_Any_Default()
        {
            ISpecification s = _factory.GetSpecification("any_default");

            Assert.IsInstanceOfType(typeof(AnySpecification), s);

            AnySpecification s1 = (AnySpecification)s;

            Assert.IsNotNull(s1.ElementSpec);
            Assert.IsInstanceOfType(typeof(TrueSpecification), s1.ElementSpec);
        }
            public void CorrectComplexSpecification_ReturnExpressionForAny()
            {
                ISpecification <string> specification = MockComplexSpecification <string> .True();

                var sut = new AnySpecification <string[], string>(specification);

                var sutExpression = sut.GetExpression();
                var result        = sutExpression.ToString();

                Assert.Equal(@"candidate => ((candidate != null) AndAlso candidate.Any(candidate => True))", result);
            }
            public void RelatedTypes_NoException()
            {
                var specification = MockComplexSpecification <IEnumerable <int> > .True();

                var exception = Record.Exception(() =>
                {
                    var sut = new AnySpecification <IEnumerable <EquatableFakeType>, EquatableFakeType>(specification);
                    sut.IsSatisfiedBy(new EquatableFakeType[0]);
                });

                Assert.Null(exception);
            }
            public void AnyElementsValid_ReturnExpectedResultObject <T, TType>(T candidate,
                                                                               Expression <Func <TType, bool> > expression, SpecificationResult expected)
                where T : IEnumerable <TType>
            {
                var specification = new MockComplexSpecification <TType>(expression);
                var sut           = new AnySpecification <T, TType>(specification);

                var overall = sut.IsSatisfiedBy(candidate, out var result);

                Assert.True(overall);
                Assert.Equal(expected, result, new SpecificationResultComparer());
            }
Ejemplo n.º 19
0
            public void CastCorrectSpecification_ReturnIsSatisfiedByFunction()
            {
                ISpecification <string> specification = MockComplexSpecification <string> .True();

                var sut = new AnySpecification <string[], string>(specification);
                Func <string[], bool> expected = sut.IsSatisfiedBy;

                var result = (Func <string[], bool>)sut;

                Assert.NotNull(result);
                Assert.Equal(expected, result);
            }
Ejemplo n.º 20
0
            public void CastCorrectSpecification_ReturnExpressionWithParameterFromSelector()
            {
                ISpecification <string> specification = MockComplexSpecification <string> .True();

                var sut = new AnySpecification <string[], string>(specification);

                var expected      = sut.GetExpression().ToString();
                var sutExpression = (Expression)sut;
                var result        = sutExpression.ToString();

                Assert.Equal(expected, result);
            }
            public void NonGenericILinqSpecification_ReturnBaseExpressionAsAbstractExpression()
            {
                ISpecification <string> specification = MockComplexSpecification <string> .True();

                var sut = new AnySpecification <string[], string>(specification);

                var expected      = sut.GetExpression().ToString();
                var sutExpression = ((ILinqSpecification)sut).GetExpression();
                var result        = sutExpression.ToString();

                Assert.Equal(expected, result);
            }
            public void InvokeRelatedTypes_NoException()
            {
                var specification = MockComplexSpecification <IEnumerable <int> > .True();

                var exception = Record.Exception(() =>
                {
                    var sut = new AnySpecification <IEnumerable <EquatableFakeType>, EquatableFakeType>(specification);
                    sut.GetExpression().Compile().Invoke(new EquatableFakeType[0]);
                });

                Assert.Null(exception);
            }
Ejemplo n.º 23
0
        public void IsSatisfiedBy_StringCollection_AllItems()
        {
            var spec = new AnySpecification <string>();

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

            foreach (var item in StringCollection.Items)
            {
                if (spec.IsSatisfiedBy(item))
                {
                    actual.Add(item);
                }
            }

            Assert.Equal(StringCollection.Items, actual);
        }
            public void NotAnyElementsValid_ReturnExpectedResultObject <T, TType>(T candidate,
                                                                                  Expression <Func <TType, bool> > expression, SpecificationResult expected)
                where T : IEnumerable <TType>
            {
                candidate = candidate?.ToString() != "null" ? candidate : default;
                var specification = new MockComplexSpecification <TType>(expression);
                var sut           = new AnySpecification <T, TType>(specification);

                var overall = sut.IsSatisfiedBy(candidate, out var result);

                Assert.False(overall);
                Assert.Equal(expected, result, new SpecificationResultComparer(candidate, new Dictionary <string, object>
                {
                    { "SpecificationForAny", specification },
                    { "Expression", expression }
                }));
            }
Ejemplo n.º 25
0
        /// <summary>
        ///     Gets the paged result by criteria.
        /// </summary>
        /// <typeparam name="TAggregateRoot">The type of the aggregate root.</typeparam>
        /// <typeparam name="TViewModel">The type of the view model.</typeparam>
        /// <param name="repository">The repository.</param>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="sortingField">The sorting field.</param>
        /// <param name="sortingOrder">The sorting order.</param>
        /// <param name="filterExpression">The filter expression.</param>
        /// <param name="expressionArgs">The expression arguments.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        ///     repository
        ///     or
        ///     sortingField
        /// </exception>
        protected virtual PagedResult <TViewModel> GetPagedResultByCriteria <TAggregateRoot, TViewModel>(
            IRepository <TAggregateRoot> repository,
            int pageNumber, int pageSize, string sortingField, SortOrder sortingOrder, string filterExpression = null,
            params object[] expressionArgs)
            where TAggregateRoot : class, IAggregateRoot
            where TViewModel : ViewModel
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }

            if (string.IsNullOrEmpty(sortingField))
            {
                throw new ArgumentNullException("sortingField");
            }

            if (sortingOrder == SortOrder.Unspecified)
            {
                sortingOrder = SortOrder.Ascending;
            }
            Specification <TAggregateRoot> querySpecification = new AnySpecification <TAggregateRoot>();

            if (!string.IsNullOrEmpty(filterExpression))
            {
                if (expressionArgs != null && expressionArgs.Length > 0)
                {
                    querySpecification =
                        Specification <TAggregateRoot> .Eval(
                            DynamicExpression.ParseLambda <TAggregateRoot, bool>(filterExpression, expressionArgs));
                }
                else
                {
                    querySpecification =
                        Specification <TAggregateRoot> .Eval(
                            DynamicExpression.ParseLambda <TAggregateRoot, bool>(filterExpression));
                }
            }
            var sortPredicate = DynamicExpression.ParseLambda <TAggregateRoot, dynamic>(sortingField);
            var result        = repository.FindAll(querySpecification, sortPredicate, sortingOrder, pageNumber, pageSize);

            return(result != null
                ? result.CastPagedResult <TAggregateRoot, TViewModel>(Mapper.Map <TAggregateRoot, TViewModel>)
                : null);
        }
		public void Test_Any_InvalidType()
		{
			// cannot test a non-enumerable object
			AnySpecification s = new AnySpecification(AlwaysTrue);
			s.Test(new object());
		}