Specification.Lite 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Specification.Lite --version 1.0.0
                    
NuGet\Install-Package Specification.Lite -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="Specification.Lite" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Specification.Lite" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Specification.Lite" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Specification.Lite --version 1.0.0
                    
#r "nuget: Specification.Lite, 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.
#:package Specification.Lite@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Specification.Lite&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Specification.Lite&version=1.0.0
                    
Install as a Cake Tool

Specification.Lite

NuGet NuGet GitHub

Specification.Lite is a lightweight .NET library that streamlines the implementation of the Specification pattern. It helps you encapsulate, reuse, and combine business rules, predicates, and query logic in a flexible and maintainable way.


Features

  • Specification Pattern:
    Define reusable business rules and query logic using strongly-typed specifications. Encapsulate complex predicates into composable objects.

  • Query Extensions:
    Apply specifications directly to IQueryable objects for filtering, ordering, and projecting data. Supports asynchronous LINQ operations like ToListAsync, FirstOrDefaultAsync, SingleOrDefaultAsync, and AnyAsync.

  • Include Expressions:
    Eagerly load related entities in queries using Include and ThenInclude, just like in Entity Framework.

  • Ordering:
    Easily apply ordering to queries with OrderBy and OrderByDescending methods on your specifications.

  • Projection:
    Transform entities to DTOs or other result types within the specification using Select and SelectMany.

  • Skip & Take:
    Effortlessly paginate query results using Skip and Take inside your specifications.

  • Tracking:
    Control whether entities are tracked by the context with AsTracking and AsNoTracking for optimal performance.

  • SplitQuery:
    Enables the use of EF Core�s AsSplitQuery to optimize queries containing multiple includes, preventing the cartesian explosion problem.

  • IgnoreQueryFilters:
    Allows you to bypass global query filters (such as soft delete or multi-tenancy) by applying IgnoreQueryFilters in your specifications.

  • Entity Framework Integration:
    Seamlessly integrates with Entity Framework Core, making it easy to use specifications in your repositories or DbContext queries.


Installation

Install via NuGet Package Manager:

dotnet add package Specification.Lite --version 1.0.0

Or add to your project file:

<PackageReference Include="Specification.Lite" Version="1.0.0" />

Usage Examples

Simple Example

Define a basic specification and use it to query active users:

// Define a simple specification for active users
public class ActiveUsersSpecification : Specification<User>
{
    public ActiveUsersSpecification()
    {
        Query
            .Include(u => u.Orders)
            .Where(user => user.IsActive);
    }
}

// Usage in your repository or DbContext
var spec = new ActiveUsersSpecification();
var activeUsers = await dbContext.Users.WithSpecification(spec).ToListAsync();

// Or directly using the DbContext
var activeUsers = await dbContext.Users.ToListAsync(spec);

Complex Example

Combine filtering, includes, ordering, projection, pagination, split queries, ignoring query filters, and no-tracking:

// Complex specification: Get all active users who registered after 2024-01-01,
// include their orders (with order items), ordered by registration date descending,
// project to a custom DTO, paginate results, use split queries, ignore global query filters, and return as no-tracking.

public class RecentActiveUsersWithOrdersSpec : Specification<User, UserSummaryDto>
{
    public RecentActiveUsersWithOrdersSpec(DateTime since, int skip, int take)
    {
        Query
            .Where(u => u.IsActive && u.RegisteredAt >= since)
            .Include(u => u.Orders)
                .ThenInclude(o => o.OrderItems)
            .OrderByDescending(u => u.RegisteredAt)
            .Skip(skip)
            .Take(take)
            .AsNoTracking()
            .AsSplitQuery()
            .IgnoreQueryFilters()
            .Select(u => new UserSummaryDto
            {
                Id = u.Id,
                Name = u.Name,
                OrderCount = u.Orders.Count,
                TotalSpent = u.Orders.Sum(o => o.TotalAmount)
            });
    }
}

// Usage in your application code
var spec = new RecentActiveUsersWithOrdersSpec(new DateTime(2024, 1, 1), skip: 20, take: 10);
var summaries = await dbContext.Users.WithSpecification(spec).ToListAsync();

// Or directly using the DbContext
var summaries = await dbContext.Users.ToListAsync(spec);

See the examples folder.


Contributing

Contributions, issues, and feature requests are welcome! or open an issue to get started.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.3.0 134 7/15/2025
1.2.0 135 7/6/2025
1.1.0 71 7/5/2025
1.0.3 68 7/5/2025
1.0.2 80 7/4/2025
1.0.1 119 7/4/2025
1.0.0 130 7/4/2025
0.0.1 140 7/1/2025