Table of Contents

Motiv provides extension methods to work efficiently with collections of specifications and results. These methods improve code readability and reduce the need for repetitive logic when dealing with multiple specifications or results.

Collection Categories

The Collections API is divided into three main categories:

  1. Generic Collections - Methods for working with collections of any type using specifications
  2. Proposition Collections - Methods for combining multiple specifications into a single specification
  3. Result Collections - Methods for working with collections of evaluation results

Generic Collection Methods

These methods help you work with generic collections using specifications:

Method Description
Where<T>() Filters a collection using a specification instead of a predicate function

Proposition Collection Methods

These methods help you combine multiple specifications:

Method Description
AndTogether() Combines multiple specifications with AND logic
AndAlsoTogether() Combines multiple specifications with short-circuiting AND logic
OrTogether() Combines multiple specifications with OR logic
OrElseTogether() Combines multiple specifications with short-circuiting OR logic

Result Collection Methods

These methods help you work with collections of evaluation results:

Method Description
WhereTrue() Filters results to include only satisfied ones
WhereFalse() Filters results to include only unsatisfied ones
CountTrue() Counts the number of satisfied results
CountFalse() Counts the number of unsatisfied results
AllTrue() Determines if all results are satisfied
AllFalse() Determines if all results are unsatisfied
AnyTrue() Determines if any results are satisfied
AnyFalse() Determines if any results are unsatisfied
GetAssertions() Aggregates assertions from all results
GetTrueAssertions() Aggregates assertions from satisfied results
GetFalseAssertions() Aggregates assertions from unsatisfied results
GetRootAssertions() Aggregates assertions from root cause results
GetAllRootAssertions() Aggregates assertions from all contributing results

Examples

Working with Generic Collections

// Create a specification for adults
var isAdult = Spec
    .Build<Person>(p => p.Age >= 18)
    .Create();

// Use it to filter a collection (instead of people.Where(p => p.Age >= 18))
var adults = people.Where(isAdult);

Combining Specifications

// Create multiple specifications
var specs = new List<Spec<Product>>
{
    isInStock,
    isPriceValid,
    hasValidCategory
};

// Combine them all with AND logic
var allChecks = specs.AndTogether();

// Evaluate a product against all checks at once
var result = allChecks.Evaluate(product);

Working with Result Collections

// Evaluate multiple entities
var results = products.Select(p => productSpec.Evaluate(p)).ToList();

// Get only the valid ones
var validResults = results.WhereTrue();

// Check if any are invalid
bool hasInvalid = results.AnyFalse();

// Get all failure messages
string allFailures = string.Join(", ", results.GetFalseAssertions());

Next Steps