The Builder API provides a fluent interface for creating specifications and propositions in Motiv. This allows you to construct complex expressions in a readable, maintainable way.
Core Builder Methods
Building specifications in Motiv follows a consistent pattern:
- Start with
Spec.Build()to begin the specification - Optionally add higher-order proposition methods like
AsAllSatisfied() - Define outcomes with
WhenTrue()andWhenFalse() - Complete with
Create()to produce the final specification
Available Builder Methods
| Method | Description |
|---|---|
| Build() | Initiates the build process |
| As() | Defines a custom higher order proposition |
| AsAllSatisfied() | Creates proposition satisfied when all models in a collection are satisfied |
| AsAnySatisfied() | Creates proposition satisfied when any model in a collection is satisfied |
| AsNoneSatisfied() | Creates proposition satisfied when no models in a collection are satisfied |
| AsAtLeastNSatisfied() | Creates proposition satisfied when at least N models are satisfied |
| AsAtMostNSatisfied() | Creates proposition satisfied when at most N models are satisfied |
| AsNSatisfied() | Creates proposition satisfied when exactly N models are satisfied |
| WhenTrue() | Defines the explanation when the proposition is satisfied |
| WhenTrueYield() | Defines a collection of assertions when satisfied |
| WhenFalse() | Defines the explanation when the proposition is not satisfied |
| WhenFalseYield() | Defines a collection of assertions when not satisfied |
| Create() | Completes the build process and returns the proposition |
Example
Here's a simple example of the builder in action:
// Create a specification that validates if a person is allowed to drive
var canDrive = Spec
.Build<Person>(person => person.Age >= 16)
.WhenTrue("Person is old enough to drive")
.WhenFalse("Person is too young to drive")
.Create();
// For collection-based specifications:
var allAdults = Spec
.Build<IEnumerable<Person>>()
.AsAllSatisfied(person => person.Age >= 18)
.WhenTrue("All persons are adults")
.WhenFalse("Some persons are minors")
.Create();
Next Steps
After creating specifications, you'll likely want to:
- Combine specifications using logical operators
- Work with collections of specifications
- Evaluate specifications against your domain models