/// <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 function.</param> /// <param name="composeWith">A specification expression.</param> /// <typeparam name="T">The generic type of the specifications.</typeparam> public static ISpecificationFunction <T> And <T>(this ISpecificationFunction <T> spec, ISpecificationExpression <T> composeWith) { var func1 = GetFunction(spec); var func2 = composeWith.GetFunction(); return(Spec.Func <T>(o => func1(o) && func2(o))); }
/// <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())); }
/// <summary> /// Gets a <c>System.Predicate<T></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)); }
/// <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)); }
/// <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())); }