Esempio n. 1
0
        /// <summary>
        /// Transforms the specification to a target type, using a selector expression.
        /// </summary>
        /// <returns>The transformed specification expression.</returns>
        /// <param name="selector">A selector to specify how an instance of the transformed/target type may be used to get an instance of the originally-specified type.</param>
        /// <typeparam name="TTarget">The target type.</typeparam>
        public ISpecificationExpression <TTarget> To <TTarget>(Expression <Func <TTarget, TOrigin> > selector)
        {
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            var newExpression = expressionTransformer.Transform(spec.GetExpression(), selector);

            return(Spec.Expr(newExpression));
        }
Esempio n. 2
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()));
        }
Esempio n. 4
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);
        }