Raffinert.Spec
1.6.0
dotnet add package Raffinert.Spec --version 1.6.0
NuGet\Install-Package Raffinert.Spec -Version 1.6.0
<PackageReference Include="Raffinert.Spec" Version="1.6.0" />
paket add Raffinert.Spec --version 1.6.0
#r "nuget: Raffinert.Spec, 1.6.0"
// Install Raffinert.Spec as a Cake Addin #addin nuget:?package=Raffinert.Spec&version=1.6.0 // Install Raffinert.Spec as a Cake Tool #tool nuget:?package=Raffinert.Spec&version=1.6.0
Raffinert.Spec
Raffinert.Spec
is a rethinking of libraries and sources such as:
Why Another Specification Library?
Cleaner IDE:
Raffinert.Spec
doesn't add any extension methods to common classes likeobject
orExpression<Func<TEntity, bool>>
. This means you won't see a lot of extra options in your IntelliSense.Simple Design: All the 'magic' is encapsulated inside the Spec<T> and then can be converted to Expression<Func<TEntity, bool>>. No Includes, Paginations and other extra features.
Flexible Use: It supports a mixed approach by allowing the use of separate specification classes as well as inline specifications. This makes it easy to combine expressions, including nested items, with no fragile code.
Usage
Full examples see in Integration Tests
Defining a Specification
You can define specifications either inline or create custom specification classes. Below is an example of a custom specification for filtering products by name:
using Raffinert.Spec;
using System.Linq.Expressions;
public class ProductNameSpec : Spec<Product>
{
private readonly string _name;
public ProductNameSpec(string name)
{
_name = name;
}
public override Expression<Func<Product, bool>> GetExpression()
{
return product => product.Name == _name;
}
}
Composing Specifications
You can combine specifications using logical operators (AND
, OR
, NOT
) with method chaining or operator overloads. Here are some examples using a test context:
Example: Filtering Products
// Arrange
var appleSpec = new ProductNameSpec("Apple");
var bananaSpec = Spec<Product>.Create(p => p.Name == "Banana");
var bananaOrAppleSpec = bananaSpec || appleSpec; // OR specification
var notBananaAndNotAppleSpec = !bananaOrAppleSpec; // NOT specification
// Act
var productsQuery = _context.Products.Where(notBananaAndNotAppleSpec);
var filteredProducts = await productsQuery.ToArrayAsync();
// Assert
Assert.Equivalent(new[]
{
new
{
Id = 3,
Name = "Cherry",
Price = 8.0m
}
}, filteredProducts);
Filtering with Methods
You can also define specifications using methods:
// Arrange
var bananaSpec = Spec<Product>.Create(p => p.Name == "Banana");
var bananaOrAppleSpec = bananaSpec.Or(p => p.Name == "Apple"); // OR specification
var notBananaAndNotAppleSpec = bananaOrAppleSpec.Not(); // NOT specification
// Act
var filteredProducts = _context.ProductArray.Where(notBananaAndNotAppleSpec).ToArray();
// Assert
Assert.Equivalent(new[]
{
new
{
Id = 3,
Name = "Cherry",
Price = 8.0m
}
}, filteredProducts);
Complex Specification Composition
You can also compose specifications across multiple entities, as shown in the following example:
// Arrange
var bananaStringSpec = Spec<string>.Create(n => n == "Banana");
var categoryWithBanana = Spec<Category>.Create(c => c.Products.Any(p => bananaStringSpec.IsSatisfiedBy(p.Name)));
var bananaSpec1 = Spec<Product>.Create(p => p.Name == "Banana");
var categoryWithBananaProductMethodGroup = Spec<Category>.Create(c => c.Products.Any(bananaSpec1.IsSatisfiedBy));
var appleSpec = new ProductNameSpec("Apple");
var categoryWithAppleProduct = Spec<Category>.Create(c => c.Products.Any(p => appleSpec.IsSatisfiedBy(p)));
var productName = "Apple";
var categoryWithDynamicProductMethodGroup = Spec<Category>.Create(c => c.Products.Any(new ProductNameSpec(productName).IsSatisfiedBy));
var productName1 = "Banana";
var categoryWithDynamicProduct = Spec<Category>.Create(c => c.Products.Any(p => new ProductNameSpec(productName1).IsSatisfiedBy(p)));
// Act1
var catQuery1 = _context.Categories.Where(categoryWithBanana);
var filteredCategories1 = await catQuery1.ToArrayAsync();
// Act2
var catQuery2 = _context.Categories.Where(categoryWithBananaProductMethodGroup);
var filteredCategories2 = await catQuery2.ToArrayAsync();
// Act3
var catQuery3 = _context.Categories.Where(categoryWithAppleProduct);
var filteredCategories3 = await catQuery3.ToArrayAsync();
// Act4
var catQuery4 = _context.Categories.Where(categoryWithDynamicProductMethodGroup);
var filteredCategories4 = await catQuery4.ToArrayAsync();
// Act5
var catQuery5 = _context.Categories.Where(categoryWithDynamicProduct);
var filteredCategories5 = await catQuery5.ToArrayAsync();
// Assert
var expectedCategories = new[]
{
new
{
Id = 1,
Name = "Fruit"
}
};
Assert.Equivalent(expectedCategories, filteredCategories1);
Assert.Equivalent(expectedCategories, filteredCategories2);
Assert.Equivalent(expectedCategories, filteredCategories3);
Assert.Equivalent(expectedCategories, filteredCategories4);
Assert.Equivalent(expectedCategories, filteredCategories5);
Evaluating Specifications
You can evaluate if an object satisfies a specification using the IsSatisfiedBy
method:
// Example usage
var isSatisfied = bananaSpec.IsSatisfiedBy(new Product { Name = "Banana" }); // true
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.
See also Raffinert.Proj library;
Product | Versions 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. |
-
.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.