Table of Contents

Logical Operators

Motiv provides a set of logical operators to combine propositions and their results, enabling the construction of more complex expressions. These operators are applicable to both propositions themselves and the outcomes of their evaluations.

Available Operators

Operation Method Usage Operator Usage Description
And() left.And(right) left & right Creates a new specification that is satisfied if both operand specifications are satisfied. Corresponds to the logical AND operator (&).
AndAlso() left.AndAlso(right) left && right
(results and expression trees only)
Creates a new specification that is satisfied if both operand specifications are satisfied. Corresponds to the logical AND ALSO operator (&&).
Or() left.Or(right) left \| right Creates a new specification that is satisfied if either of the operand specifications are satisfied. Corresponds to the logical OR operator (|).
OrElse() left.OrElse(right) left \|\| right
(results and expression trees only)
Creates a new specification that is satisfied if either of the operand specifications are satisfied. Corresponds to the logical OR ELSE operator (||).
XOr() left.XOr(right) left ^ right Creates a new specification that is satisfied if exactly one of the operand specifications is satisfied. Corresponds to the logical XOR operator (^).
Not() proposition.Not() !proposition Creates a new specification that is satisfied if the operand specification is not satisfied. Corresponds to the logical NOT operator (!).

Working with Propositions

Combining propositions with these operators generates new, composite propositions:

// Create individual propositions
var isAdult = Spec
    .Build<Person>(p => p.Age >= 18)
    .WhenTrue("Is an adult")
    .WhenFalse("Is not an adult")
    .Create();

var hasValidId = Spec
    .Build<Person>(p => !string.IsNullOrEmpty(p.IdNumber))
    .WhenTrue("Has valid ID")
    .WhenFalse("Missing ID")
    .Create();

// Combine using method syntax to create a new proposition
var canVote = isAdult.And(hasValidId);

// Alternatively, use operator syntax
// var canVote = isAdult & hasValidId;

Working with Results

The results of evaluated propositions can also be combined using these operators:

var adultResult = isAdult.Evaluate(person);
var idResult = hasValidId.Evaluate(person);

// Combine the results
var canVoteResult = adultResult & idResult;

// Access the combined explanation
if (!canVoteResult.Satisfied)
{
    // The explanation will include reasons from all failed propositions
    Console.WriteLine(canVoteResult.Explanation.Assertions.First()); // Example: "Is not an adult"
}

Key Operator Differences

  • And vs. AndAlso: AndAlso implements short-circuiting. The right-hand operand is evaluated only if the left-hand operand is true. And always evaluates both operands.
  • Or vs. OrElse: OrElse implements short-circuiting. The right-hand operand is evaluated only if the left-hand operand is false. Or always evaluates both operands.

Next Steps