New propositions are built using the Spec.Build() method.
This method is overloaded and takes one of the following types of arguments:
| Name | Type | Description |
|---|---|---|
| Boolean Predicate | Func<TModel, bool> |
a predicate that returns a boolean value. |
| Boolean Result Predicate | Func<TModel, BooleanResultBase<TMetadata>> |
a predicate that returns a BooleanResultBase<TMetadata> instance. |
| Proposition | SpecBase<TModel,TMetadata> |
a proposition that has already been built. |
| Proposition Factory | Func<SpecBase<TModel, TMetadata>> |
a factory function that returns a proposition. |
All of these overloads can be used to create a new proposition with varying levels of expressiveness.
From a predicate function
Build<TModel>(Func<TModel, bool> predicate)
Building using a predicate function is the canonical way of creating a proposition. Propositions built from these generally serve as the foundations to more complex propositions.
Spec.Build((int n) => n % 2 == 0)
.Create("is even");
From the result of a proposition
Build<TModel, TMetadata>(Func<TModel, BooleanResultBase<TMetadata>> predicate)
This is semantically the same as the previous example, but uses a BooleanResultBase<TMetadata> to encapsulate the
result of the proposition, instead of a raw bool.
This would be typically generated by an evaluation of another predicate, but instantiating your own result object is
also possible.
Spec.Build((int n) => new IsEvenProposition().Evaluate(n))
.Create("is even (better)");
From an existing proposition
Build<TModel, TMetadata>(SpecBase<TModel,TMetadata> proposition)
This is used to derive a proposition directly from another proposition. It is commonly used to change the assertions or metadata to a different value or type, as well as to compose new propositions as a one-line expression.
Spec.Build(new IsEvenProposition())
.Create("is even (better)");
From a proposition factory
Build<TModel, TMetadata>(Func<SpecBase<TModel, TMetadata>> factory)
This is used to create a proposition from a factory function. The function is immediately invoked and the result is used to create the proposition. This doesn't add any new capabilities, but is instead to assist with encapsulation and readability.
Spec.Build(() => new IsEvenProposition())
.Create("is even (better)");