QueryBuilderSpecs 1.1.1

dotnet add package QueryBuilderSpecs --version 1.1.1
                    
NuGet\Install-Package QueryBuilderSpecs -Version 1.1.1
                    
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="QueryBuilderSpecs" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QueryBuilderSpecs" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="QueryBuilderSpecs" />
                    
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 QueryBuilderSpecs --version 1.1.1
                    
#r "nuget: QueryBuilderSpecs, 1.1.1"
                    
#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 QueryBuilderSpecs@1.1.1
                    
#: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=QueryBuilderSpecs&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=QueryBuilderSpecs&version=1.1.1
                    
Install as a Cake Tool

QueryBuilderSpecsLibrary ๐Ÿš€

NuGet License

A powerful and clean .NET library for building query specifications using the Specification Pattern, Generic Repository, and Unit of Work.

โœ… Fully supports filtering via DTOs
โœ… Works with both EF Core DbContext and Unit of Work
โœ… Highly customizable and testable


๐Ÿ“ฆ Installation

Install via NuGet:

dotnet add package QueryBuilderSpecsLibrary

๐Ÿงฐ Features

  • ๐Ÿ”„ Unit of Work: Abstracts multiple repositories with transactional support
  • ๐Ÿ” Specification Pattern: Enables clean, reusable, and composable query logic
  • ๐Ÿงช Filter Builder: Apply filtering rules based on DTOs dynamically
  • ๐Ÿ’ก Generic Repository: Simplifies CRUD operations

๐Ÿ› ๏ธ Setup

1. Register Library Services in Program.cs

builder.Services.UseQueryBuilderSpecs(); // Registers UoW and repository

2. Register Filter Builder for a specific entity

builder.Services.AddFilterBuilder<User, UserFilter, UserFilterBuilder>();

๐Ÿงช Usage

โœ… Using DbContext with Filter Builder

app.MapGet("/api/users/from-dbcontext", async (
    UserFilter filter,
    [FromServices] AppDbContext dbContext,
    [FromServices] IGenericFiltersBuilder<User, UserFilter> filterBuilder
) =>
{
    var users = await dbContext.Users
        .ApplyFilter(filter, filterBuilder)
        .ToListAsync();

    return Results.Ok(users);
});

โœ… Using Unit of Work

app.MapGet("/api/users/from-uow", async (
    UserFilter filter,
    [FromServices] IUnitOfWork<AppDbContext> uow,
    [FromServices] IGenericFiltersBuilder<User, UserFilter> filterBuilder
) =>
{
    var spec = uow.Repository<User>()
        .InjectSpecification(filterBuilder, filter);

    var users = await uow.Repository<User>().GetAllAsync(extendQuery: spec);

    return Results.Ok(users);
});

๐Ÿ“ Folder Structure

๐Ÿ“ฆ QueryBuilderSpecsLibrary
 โ”ฃ ๐Ÿ“‚ src
 โ”ƒ โ”ฃ ๐Ÿ“œ QueryBuilderSpecs.csproj
 โ”ƒ โ”ฃ ๐Ÿ“œ Interfaces/
 โ”ƒ โ”ฃ ๐Ÿ“œ Extensions/
 โ”ƒ โ”— ๐Ÿ“œ Specifications/
 โ”— ๐Ÿ“‚ SampleWebApp
    โ”— ๐Ÿ“œ Program.cs

๐Ÿ“„ Sample DTO and Filter Builder

UserFilter.cs

public class UserFilter
{
    public string? Name { get; set; }
    public bool? IsActive { get; set; }
}

FilterByIsActiveSpecification.cs

public class FilterByIsActiveSpecification : BaseSpecification<User>, ISpecification<User>
{
    public FilterByIsActiveSpecification(bool? isActive)
    {
        if (isActive == null)
            return;

        SetCriteria(u => u.IsActive == isActive);
    }
}

UserFilterBuilder.cs


public class UserFilterBuilder: GenericFiltersBuilder<User,UserFilter>,IGenericFiltersBuilder<User,UserFilter> 
{
    public override void InitItemSpecifications()
    {
        AddSpecification(
            nameof(UserFilter.Name),
            filter => new FilterByNameSpecification(filter.Name)
        );
        
        
        AddSpecification(
            nameof(UserFilter.IsActive),
            filter => new FilterByIsActiveSpecification(filter.IsActive)
        );
    }
}

๐Ÿงช Testing

Use the sample project to test and verify functionality:

dotnet run --project SampleWebApp

Navigate to:

https://localhost:5001/swagger

Test your endpoints with filters such as:

  • /api/users/from-dbcontext?name=John
  • /api/users/from-uow?isActive=true

๐Ÿงพ Versioning

We follow Semantic Versioning:

  • 1.0.0 โ†’ Initial release
  • 1.1.0 โ†’ Minor feature additions (non-breaking)
  • 1.1.1 โ†’ Update package tags (description, tags) (non-breaking)

Update the version in .csproj and tag it:

<Version>1.1.1</Version>

๐Ÿ“ License

This project is licensed under the MIT License.


๐Ÿ™‹โ€โ™‚๏ธ Contributing

We welcome contributions! Feel free to open issues, PRs, or suggest improvements.


๐ŸŒ Author

Developed with โค๏ธ by GibranFahed

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.1.1 75 5/31/2025
1.1.0 74 5/31/2025
1.0.0 107 5/30/2025