Exemple #1
0
        /// <summary>
        /// Gets a new specification expression which corresponds to the logical
        /// combination of the specified objects: <c>AND</c>.
        /// </summary>
        /// <returns>A specification expression.</returns>
        /// <param name="spec">A specification expression.</param>
        /// <param name="composeWith">Another specification expression.</param>
        /// <typeparam name="T">The generic type of the specifications.</typeparam>
        public static ISpecificationExpression <T> And <T>(this ISpecificationExpression <T> spec, ISpecificationExpression <T> composeWith)
        {
            var func1 = spec.GetExpressionOrThrow();
            var func2 = composeWith.GetExpressionOrThrow();

            return(Spec.Expr(func1.And(func2)));
        }
Exemple #2
0
        public void GetTickets_gets_query_provider_from_factory(IGetsTicketSpecification specProvider,
                                                                ISpecificationExpression <Ticket> spec,
                                                                IGetsQueryForTickets queryProvider,
                                                                TicketListRequest request)
        {
            // Arrange
            var queryProviderFactoryUsed = false;
            var specFactory          = GetSpecFactory(s => specProvider);
            var queryProviderFactory = GetQueryProviderFactory(s => {
                if (s != spec)
                {
                    return(null);
                }
                queryProviderFactoryUsed = true;
                return(queryProvider);
            });

            Mock.Get(specProvider).Setup(x => x.GetSpecification()).Returns(spec);
            Mock.Get(queryProvider).Setup(x => x.GetQuery()).Returns(() => null);
            var sut = new TicketLister(queryProviderFactory, specFactory);

            // Act
            sut.GetTickets(request);

            // Assert
            Assert.That(queryProviderFactoryUsed, Is.True);
        }
Exemple #3
0
        /// <summary>
        /// Gets a new specification function which corresponds to the logical
        /// combination of the specified objects: <c>AND</c>.
        /// </summary>
        /// <returns>A specification function.</returns>
        /// <param name="spec">A specification expression.</param>
        /// <param name="composeWith">A specification function.</param>
        /// <typeparam name="T">The generic type of the specifications.</typeparam>
        public static ISpecificationFunction <T> And <T>(this ISpecificationExpression <T> spec, ISpecificationFunction <T> composeWith)
        {
            var func1 = spec.GetFunction();
            var func2 = composeWith.GetFunction();

            return(Spec.Func <T>(o => func1(o) && func2(o)));
        }
        public virtual bool CheckExists(ISpecificationExpression <TEntity> specification)
        {
            CheckExists_Before(specification);
            var result = Component.CheckExists(specification);

            CheckExists_After(specification, result);
            return(result);
        }
 /// <summary>
 /// Filters a Linq queryable object using the predicate defined in a specification expression.
 /// </summary>
 /// <returns>A queryable object which is filtered by the specification predicate.</returns>
 /// <param name="collection">A Linq queryable object.</param>
 /// <param name="specification">A specification expression.</param>
 /// <typeparam name="T">The generic type of the collection and the specification.</typeparam>
 public static IEnumerable <T> Where <T>(this IEnumerable <T> collection, ISpecificationExpression <T> specification)
 {
     if (collection == null)
     {
         throw new ArgumentNullException(nameof(collection));
     }
     return(collection.Where(specification.GetFunction()));
 }
        public virtual IList <TEntity> GetList(ISpecificationExpression <TEntity> specification, int offset, int count)
        {
            GetList_Before(specification, offset, count);
            var result = Component.GetList(specification, offset, count);

            GetList_After(specification, offset, count, result);
            return(result);
        }
Exemple #7
0
        public override void Visit(Criterion node)
        {
            var spec = criterionConverter.ConvertToSpecification(node);

            if (spec == null)
            {
                return;
            }

            specBeingBuilt = AddSpecification(spec, (node?.LogicalOperator) ?? default(LogicalOperator));
        }
Exemple #8
0
        public Expression <Func <Ticket, bool> > GetExpression()
        {
            ISpecificationExpression <Ticket> output = Spec.Expr <Ticket>(x => true);

            foreach (var labelName in labelNames)
            {
                var nameExpression = Spec.Expr <Ticket>(x => x.Labels.Any(l => l.Name == labelName));
                output = output.And(nameExpression);
            }

            return(output.GetExpression());
        }
        /// <summary>
        /// Filters a Linq queryable object using the predicate defined in a specification expression.
        /// </summary>
        /// <returns>A queryable object which is filtered by the specification predicate.</returns>
        /// <param name="query">A Linq queryable object.</param>
        /// <param name="specification">A specification expression.</param>
        /// <typeparam name="T">The generic type of the query and the specification.</typeparam>
        public static IQueryable <T> Where <T>(this IQueryable <T> query, ISpecificationExpression <T> specification)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (specification == null)
            {
                throw new ArgumentNullException(nameof(specification));
            }

            return(query.Where(specification.GetExpression()));
        }
Exemple #10
0
 public SpecificationQueryProviderDecorator(IGetsQueryForTickets decoratedInstance,
                                            ISpecificationExpression <Ticket> specification)
 {
     if (specification == null)
     {
         throw new ArgumentNullException(nameof(specification));
     }
     if (decoratedInstance == null)
     {
         throw new ArgumentNullException(nameof(decoratedInstance));
     }
     this.decoratedInstance = decoratedInstance;
     this.specification     = specification;
 }
Exemple #11
0
        internal static Expression <Func <T, bool> > GetExpressionOrThrow <T>(this ISpecificationExpression <T> spec)
        {
            if (spec == null)
            {
                throw new ArgumentNullException(nameof(spec));
            }
            var expr = spec.GetExpression();

            if (expr == null)
            {
                throw new ArgumentException($"{nameof(ISpecificationExpression<T>)}<T>.{nameof(ISpecificationExpression<T>.GetExpression)}() must not return null.", nameof(spec));
            }
            return(expr);
        }
Exemple #12
0
        // If this method needs to become any more complex, then break it away to a service of its own which gets
        // a spec from a request.
        ISpecificationExpression <Sprint> GetSpecification(ListSprintsRequest request)
        {
            var project = GetProject(request);
            ISpecificationExpression <Sprint> spec = projectSprintFactory(project);

            if (!request.ShowOpenSprints)
            {
                spec = spec.And(closedSprintFactory());
            }

            if (!request.ShowClosedSprints)
            {
                spec = spec.And(openSprintFactory());
            }

            return(spec);
        }
Exemple #13
0
        public override void Visit(CriteriaGroup node)
        {
            if (node?.Criteria == null || !node.Criteria.Any())
            {
                return;
            }

            var recursiveVisitor = recursiveVisitorFactory();

            recursiveVisitor.VisitLogicalCriteriaProvider(node);
            var spec = recursiveVisitor.GetSpecification();

            if (spec == null)
            {
                return;
            }
            specBeingBuilt = AddSpecification(spec, node.LogicalOperator);
        }
        public void ConvertToSpecification_uses_a_viable_strategy(Criterion criterion,
                                                                  [MockedMetadata] IStrategyForConvertingCriterionToSpecification one,
                                                                  [MockedMetadata] IStrategyForConvertingCriterionToSpecification two,
                                                                  [MockedMetadata] IStrategyForConvertingCriterionToSpecification three,
                                                                  ISpecificationExpression <Ticket> spec)
        {
            // Arrange
            var strategies = new [] { one, two, three };
            var sut        = new StrategyBasedCriterionToSpecificationConverter(strategies);

            Mock.Get(two.GetMetadata()).Setup(x => x.CanConvert(criterion)).Returns(true);
            Mock.Get(two).Setup(x => x.ConvertToSpecification(criterion)).Returns(spec);

            // Act
            var result = sut.ConvertToSpecification(criterion);

            // Assert
            Assert.That(result, Is.SameAs(spec));
        }
Exemple #15
0
        ISpecificationExpression <Ticket> AddSpecification(ISpecificationExpression <Ticket> toAdd,
                                                           LogicalOperator logicalOperator)
        {
            if (specBeingBuilt == null)
            {
                return(toAdd);
            }
            logicalOperator.RequireDefinedValue(nameof(logicalOperator));

            switch (logicalOperator)
            {
            case LogicalOperator.Or:
                return(specBeingBuilt.Or(toAdd));

            case LogicalOperator.And:
                return(specBeingBuilt.And(toAdd));

            default:
                throw new NotSupportedException($"The {nameof(LogicalOperator)} must have a supported value.");
            }
        }
        ISpecificationExpression <Ticket> GetSpecFromFunction(PredicateFunction function)
        {
            ISpecificationExpression <Ticket> spec = null;

            if (function.FunctionName == PredicateName.Function.IsEmpty)
            {
                spec = new HasNoStoryPoints();
            }

            if (function.FunctionName == PredicateName.Function.IsAnyOf)
            {
                spec = new StoryPointsIsOneOf(valueResolver.ResolveAll <int>(function.Parameters));
            }

            if (spec != null && function.Inverted)
            {
                spec = spec.Not();
            }

            return(spec);
        }
 public virtual void GetList_Before(ISpecificationExpression <TEntity> specification, int offset, int count)
 {
 }
 /// <summary>
 /// Gets a single object from the query which matches the specification, or a default object if no instance was
 /// matched.
 /// </summary>
 /// <returns>The matched object or a default instance.</returns>
 /// <param name="query">A Linq queryable object.</param>
 /// <param name="specification">A specification expression.</param>
 /// <typeparam name="T">The generic type of the query and the specification.</typeparam>
 public static T SingleOrDefault <T>(this IQueryable <T> query, ISpecificationExpression <T> specification)
 => query.Where(specification).SingleOrDefault();
 public virtual void GetList_After(ISpecificationExpression <TEntity> specification, int offset, int count, IList <TEntity> result)
 {
 }
 public virtual void CheckExists_Before(ISpecificationExpression <TEntity> specification)
 {
 }
 /// <summary>
 /// Gets the first object from the query which matches the specification.
 /// </summary>
 /// <returns>The matched object.</returns>
 /// <param name="query">A Linq queryable object.</param>
 /// <param name="specification">A specification expression.</param>
 /// <typeparam name="T">The generic type of the query and the specification.</typeparam>
 public static T First <T>(this IQueryable <T> query, ISpecificationExpression <T> specification)
 => query.Where(specification).First();
 /// <summary>
 /// Gets a value which indicates whether or not any objects in the query match the given specification.
 /// </summary>
 /// <returns><c>true</c> if there are any matches for the specification; <c>false</c> otherwise.</returns>
 /// <param name="query">A Linq queryable object.</param>
 /// <param name="specification">A specification expression.</param>
 /// <typeparam name="T">The generic type of the query and the specification.</typeparam>
 public static bool Any <T>(this IQueryable <T> query, ISpecificationExpression <T> specification)
 => query.Where(specification).Any();
 /// <summary>
 /// Gets a count of the objects within the query which match the specification.
 /// </summary>
 /// <returns>The count of matching objects.</returns>
 /// <param name="query">A Linq queryable object.</param>
 /// <param name="specification">A specification expression.</param>
 /// <typeparam name="T">The generic type of the query and the specification.</typeparam>
 public static int Count <T>(this IQueryable <T> query, ISpecificationExpression <T> specification)
 => query.Where(specification).Count();
Exemple #24
0
 /// <summary>
 /// Gets a specification function which copies the specified specification expression.
 /// </summary>
 /// <returns>A specification function.</returns>
 /// <param name="spec">A specification expression.</param>
 /// <typeparam name="T">The generic type of the specification.</typeparam>
 public static ISpecificationFunction <T> AsSpecificationFunction <T>(this ISpecificationExpression <T> spec)
 {
     return(Spec.Func(spec.GetFunction()));
 }
 public virtual void CheckExists_After(ISpecificationExpression <TEntity> specification, bool result)
 {
 }
Exemple #26
0
        /// <summary>
        /// Gets a function from a specification expression, as if it were a
        /// <see cref="ISpecificationFunction{T}"/>.
        /// </summary>
        /// <returns>A function, representing the compiled expression contained within the specification expression.</returns>
        /// <param name="spec">A specification expression.</param>
        /// <typeparam name="T">The generic type of the specification.</typeparam>
        public static Func <T, bool> GetFunction <T>(this ISpecificationExpression <T> spec)
        {
            var expr = spec.GetExpressionOrThrow();

            return(expr.Compile());
        }
Exemple #27
0
 /// <summary>
 /// Gets a new specification expression which corresponds to the logical
 /// negation of the specified object: <c>NOT</c>.
 /// </summary>
 /// <returns>A specification expression.</returns>
 /// <param name="spec">A specification expression.</param>
 /// <typeparam name="T">The generic type of the specification.</typeparam>
 public static ISpecificationExpression <T> Not <T>(this ISpecificationExpression <T> spec)
 {
     return(Spec.Expr(spec.GetExpressionOrThrow().Not()));
 }
Exemple #28
0
        /// <summary>
        /// Gets a <c>System.Predicate&lt;T&gt;</c> from a specification expression.
        /// </summary>
        /// <returns>A predicate instance matching the specification.</returns>
        /// <param name="spec">A specification expression.</param>
        /// <typeparam name="T">The generic type of the specification.</typeparam>
        public static Predicate <T> AsPredicate <T>(this ISpecificationExpression <T> spec)
        {
            var func = spec.GetFunction();

            return(o => func(o));
        }
Exemple #29
0
        /// <summary>
        /// Gets a value which indicates whether a specified value matches/satisfies the specification.
        /// </summary>
        /// <returns><c>true</c> if the value matches the specification; <c>false</c> otherwise.</returns>
        /// <param name="spec">A specification expression.</param>
        /// <param name="value">The value to test with the specification.</param>
        /// <typeparam name="T">The generic type of the specification and the specific type of the value object.</typeparam>
        public static bool Matches <T>(this ISpecificationExpression <T> spec, T value)
        {
            var func = spec.GetFunction();

            return(func(value));
        }
Exemple #30
0
        /// <summary>
        /// Transforms a specification object to a new specified-type, by using a selector expression to indicate how
        /// the intended type will provide the originally-specified type.
        /// </summary>
        /// <returns>A transformed specification object.</returns>
        /// <param name="spec">The original specification to transform.</param>
        /// <param name="transformation">A transformation to perform on the specification.</param>
        /// <typeparam name="TOrigin">The original specified type.</typeparam>
        /// <typeparam name="TTarget">The new specified type (after transformation).</typeparam>
        public static ISpecificationExpression <TTarget> Transform <TOrigin, TTarget>(this ISpecificationExpression <TOrigin> spec,
                                                                                      Func <IGetsTransformedSpecificationExpression <TOrigin>, ISpecificationExpression <TTarget> > transformation)
        {
            if (spec == null)
            {
                throw new ArgumentNullException(nameof(spec));
            }
            if (transformation == null)
            {
                throw new ArgumentNullException(nameof(transformation));
            }

            var transformer = new SpecificationExpressionTransformer <TOrigin>(spec);

            return(transformation(transformer));
        }