Table of Contents

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

TModel

The 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

spec SpecBase<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

spec SpecBase<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

model TModel

The 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

model TModel

The 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

model TModel

The model to evaluate the specification against.

Returns

bool

true if the model satisfies the proposition; otherwise, false.

Not()

Negates this specification.

public SpecBase<TModel, string> Not()

Returns

SpecBase<TModel, string>

A new specification that represents the logical NOT of this specification.

Or(SpecBase<TModel>)

Combines this specification with another specification using the logical OR operator.

public SpecBase<TModel, string> Or(SpecBase<TModel> spec)

Parameters

spec SpecBase<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

spec SpecBase<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

SpecBase<TModel, string>

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

spec SpecBase<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

left SpecBase<TModel>

The left operand of the AND operation.

right SpecBase<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

left SpecBase<TModel>

The left operand of the OR operation.

right SpecBase<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

left SpecBase<TModel>

The left operand of the XOR operation.

right SpecBase<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

spec SpecBase<TModel>

The specification to be converted into predicate

Returns

Func<TModel, bool>

A predicate function

operator !(SpecBase<TModel>)

Negates a specification.

public static SpecBase<TModel> operator !(SpecBase<TModel> spec)

Parameters

spec SpecBase<TModel>

The specification to negate.

Returns

SpecBase<TModel>

A new specification that represents the logical NOT of the specification.