Raffinert.Spec 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Raffinert.Spec --version 1.0.0                
NuGet\Install-Package Raffinert.Spec -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Raffinert.Spec" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Raffinert.Spec --version 1.0.0                
#r "nuget: Raffinert.Spec, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Raffinert.Spec as a Cake Addin
#addin nuget:?package=Raffinert.Spec&version=1.0.0

// Install Raffinert.Spec as a Cake Tool
#tool nuget:?package=Raffinert.Spec&version=1.0.0                

Stand With Ukraine

Raffinert.Spec

NuGet version (Raffinert.Spec)

Raffinert.Spec is a lightweight, composable specification library designed for building reusable query logic, with a focus on Entity Framework.

Features

  • Composable Specifications: Easily combine specifications using AND, OR, and NOT operators.
  • Dynamic Expression Creation: Build complex, reusable query logic with minimal boilerplate.
  • Debugger Support: Enhanced debugging with DebuggerDisplay and DebuggerTypeProxy.
  • Predicate Testing: Use IsSatisfiedBy to test if a specification is satisfied by a candidate.
  • Operator Overloading: Use natural operators (&&, ||, !) to compose specifications.

Raffinert.Spec is inspired by libraries such as:"

It reuses much of the logic from NSpecifications.

Usage

Defining a Specification

You can define specifications either inline, use predefined True and False specifications or create custom specification class.

using Raffinert.Spec;

// Inline specification for checking if a number is positive
Spec<int> isPositive = Spec<int>.Create(x => x > 0);

// True specification
Spec<int> alwaysTrue = Spec<int>.True();

// False specification
Spec<int> alwaysFalse = Spec<int>.False();

// Custom specification class
class IsPositiveSpec : Spec<int>
{
    public override Expression<Func<int, bool>> ToExpression()
    {
        return x => x > 0;
    }
}

Composing Specifications

You can combine specifications using logical operators (AND, OR, NOT) with method chaining or operator overloads.

AND Specification
Spec<int> isPositive = Spec<int>.Create(x => x > 0);
Spec<int> isEven = Spec<int>.Create(x => x % 2 == 0);

// Combine with AND (Both positive and even)
Spec<int> isPositiveAndEven = isPositive.And(isEven);

// Or using the && operator
Spec<int> isPositiveAndEvenOperator = isPositive && isEven;
OR Specification
Spec<int> isPositive = Spec<int>.Create(x => x > 0);
Spec<int> isEven = Spec<int>.Create(x => x % 2 == 0);

// Combine with OR (Either positive or even)
Spec<int> isPositiveOrEven = isPositive.Or(isEven);

// Or using the || operator
Spec<int> isPositiveOrEvenOperator = isPositive || isEven;
NOT Specification
Spec<int> isPositive = Spec<int>.Create(x => x > 0);

// Negate the positive specification
Spec<int> isNotPositive = isPositive.Not();

// Or using the ! operator
Spec<int> isNotPositiveOperator = !isPositive;

Evaluating Specifications

You can evaluate if an object satisfies a specification using the IsSatisfiedBy method:

Spec<int> isPositive = Spec<int>.Create(x => x > 0);

// Check if the candidate satisfies the specification
bool result = isPositive.IsSatisfiedBy(5); // true

Full Example

Spec<int> isPositive = Spec<int>.Create(x => x > 0);
Spec<int> isEven = Spec<int>.Create(x => x % 2 == 0);

// Complex specification: Positive and even, or not positive
Spec<int> complexSpec = (isPositive.And(isEven)).Or(!isPositive);

// Evaluate the specification
bool result = complexSpec.IsSatisfiedBy(4);  // true (4 is positive and even)

Converting to Expressions or Delegates

You can convert a Spec<T> into an Expression<Func<T, bool>> or a compiled Func<T, bool> for direct execution:

Spec<int> isPositive = Spec<int>.Create(x => x > 0);

// Convert to an expression
Expression<Func<int, bool>> expression = isPositive;

// Compile to a function
Func<int, bool> func = isPositive;

Debugging

The Spec<T> class includes built-in debugging support with a custom debugger display, giving developers an immediate view of the underlying expression while debugging.

License

This library is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.0 92 12/7/2024
1.5.5 89 11/21/2024
1.5.3 87 11/4/2024
1.5.2 87 11/4/2024
1.5.1 87 11/4/2024
1.5.0 86 11/3/2024
1.0.0 99 10/19/2024