Class SpecBase<TModel>
- Namespace
- Motiv
- Assembly
- Motiv.dll
The base class for all specifications. At its most basic, a 'Spec' is an encapsulated predicate function that can be evaluated against a model. When the predicate is evaluated, it returns a result that contains the Boolean result of the predicate as well as metadata that captures the meaning behind the predicate. By encapsulating the predicate we can supply methods to assist with combining specifications together to form more complex specifications, which together ultimately model the desired logical proposition.
public abstract class SpecBase<TModel> : SpecBase
Type Parameters
TModelThe model type that the specification will evaluate against
- Inheritance
-
SpecBase<TModel>
- Derived
-
SpecBase<TModel, TMetadata>
- Inherited Members
- Extension Methods
Methods
And(SpecBase<TModel>)
Combines this specification with another specification using the logical AND operator.
public SpecBase<TModel, string> And(SpecBase<TModel> spec)
Parameters
specSpecBase<TModel>The specification to combine with this specification.
Returns
- SpecBase<TModel, string>
A new specification that represents the logical AND of this specification and the other specification.
AndAlso(SpecBase<TModel>)
Combines this specification with another specification using the conditional AND operator. The right operand
is only evaluated if the left operand evaluates to true. This is commonly referred to as
"short-circuiting".
public SpecBase<TModel, string> AndAlso(SpecBase<TModel> spec)
Parameters
specSpecBase<TModel>The specification to combine with this specification.
Returns
- SpecBase<TModel, string>
A new specification that represents the conditional AND of this specification and the other specification.
Evaluate(TModel)
Evaluates the proposition against the model and returns a result that contains the Boolean result of the predicate and an explanation of the result.
public BooleanResultBase<string> Evaluate(TModel model)
Parameters
modelTModelThe model to evaluate the specification against.
Returns
- BooleanResultBase<string>
A result that contains the Boolean result of the predicate and an explanation of the result.
IsSatisfiedBy(TModel)
Evaluates the proposition against the model and returns a result that contains the Boolean result of the predicate and an explanation of the result.
[Obsolete("Use Evaluate instead.")]
public BooleanResultBase<string> IsSatisfiedBy(TModel model)
Parameters
modelTModelThe model to evaluate the specification against.
Returns
- BooleanResultBase<string>
A result that contains the Boolean result of the predicate and an explanation of the result.
Matches(TModel)
Evaluates the proposition against the model and returns a boolean indicating whether it is satisfied, without allocating result objects.
public virtual bool Matches(TModel model)
Parameters
modelTModelThe model to evaluate the specification against.
Returns
- bool
trueif the model satisfies the proposition; otherwise,false.
Not()
Negates this specification.
public SpecBase<TModel, string> Not()
Returns
Or(SpecBase<TModel>)
Combines this specification with another specification using the logical OR operator.
public SpecBase<TModel, string> Or(SpecBase<TModel> spec)
Parameters
specSpecBase<TModel>The specification to combine with this specification.
Returns
- SpecBase<TModel, string>
A new specification that represents the logical OR of this specification and the other specification.
OrElse(SpecBase<TModel>)
Combines this specification with another specification using the conditional OR operator. The right operand
is only evaluated if the left operand evaluates to false. This is commonly referred to as
"short-circuiting".
public SpecBase<TModel, string> OrElse(SpecBase<TModel> spec)
Parameters
specSpecBase<TModel>The specification to combine with this specification.
Returns
- SpecBase<TModel, string>
A new specification that represents the conditional OR of this specification and the other specification.
ToExplanationSpec()
Converts this specification to an explanation specification (i.e., Spec<TModel, string>). This is necessary when establishing a "lowest-common-denominator" between very different specification. Therefore, specifications with different metadata types will be wrapped in a spec that uses string as the metadata type.
public abstract SpecBase<TModel, string> ToExplanationSpec()
Returns
ToString()
Serializes the logical hierarchy of the specification to a string.
public override string ToString()
Returns
- string
A string that represents the logical hierarchy of the specification.
XOr(SpecBase<TModel>)
Combines this specification with another specification using the logical XOR operator.
public SpecBase<TModel, string> XOr(SpecBase<TModel> spec)
Parameters
specSpecBase<TModel>The specification to combine with this specification.
Returns
- SpecBase<TModel, string>
A new specification that represents the logical XOR of this specification and the other specification.
Operators
operator &(SpecBase<TModel>, SpecBase<TModel>)
Combines two specifications using the logical AND operator.
public static SpecBase<TModel, string> operator &(SpecBase<TModel> left, SpecBase<TModel> right)
Parameters
leftSpecBase<TModel>The left operand of the AND operation.
rightSpecBase<TModel>The right operand of the AND operation.
Returns
- SpecBase<TModel, string>
A new specification that represents the logical AND of the two specifications.
operator |(SpecBase<TModel>, SpecBase<TModel>)
Combines two specifications using the logical OR operator.
public static SpecBase<TModel, string> operator |(SpecBase<TModel> left, SpecBase<TModel> right)
Parameters
leftSpecBase<TModel>The left operand of the OR operation.
rightSpecBase<TModel>The right operand of the OR operation.
Returns
- SpecBase<TModel, string>
A new specification that represents the logical OR of the two specifications.
operator ^(SpecBase<TModel>, SpecBase<TModel>)
Combines two specifications using the logical XOR operator.
public static SpecBase<TModel, string> operator ^(SpecBase<TModel> left, SpecBase<TModel> right)
Parameters
leftSpecBase<TModel>The left operand of the XOR operation.
rightSpecBase<TModel>The right operand of the XOR operation.
Returns
- SpecBase<TModel, string>
A new specification that represents the logical XOR of the two specifications.
implicit operator Func<TModel, bool>(SpecBase<TModel>)
Converts the expression into a predicate function that can be used to evaluate the proposition against a model.
public static implicit operator Func<TModel, bool>(SpecBase<TModel> spec)
Parameters
specSpecBase<TModel>The specification to be converted into predicate
Returns
operator !(SpecBase<TModel>)
Negates a specification.
public static SpecBase<TModel> operator !(SpecBase<TModel> spec)
Parameters
specSpecBase<TModel>The specification to negate.
Returns
- SpecBase<TModel>
A new specification that represents the logical NOT of the specification.