RoyalCode.SmartSearch.Linq
0.8.1
dotnet add package RoyalCode.SmartSearch.Linq --version 0.8.1
NuGet\Install-Package RoyalCode.SmartSearch.Linq -Version 0.8.1
<PackageReference Include="RoyalCode.SmartSearch.Linq" Version="0.8.1" />
<PackageVersion Include="RoyalCode.SmartSearch.Linq" Version="0.8.1" />
<PackageReference Include="RoyalCode.SmartSearch.Linq" />
paket add RoyalCode.SmartSearch.Linq --version 0.8.1
#r "nuget: RoyalCode.SmartSearch.Linq, 0.8.1"
#:package RoyalCode.SmartSearch.Linq@0.8.1
#addin nuget:?package=RoyalCode.SmartSearch.Linq&version=0.8.1
#tool nuget:?package=RoyalCode.SmartSearch.Linq&version=0.8.1
RoyalCode SmartSearch
Overview
RoyalCode SmartSearch is a set of .NET libraries for implementing advanced search, filtering, sorting, and data projection in enterprise applications, following the Specification/Filter-Specifier-Pattern. The goal is to facilitate the creation of reusable, decoupled, and extensible search components, integrating with ORMs such as Entity Framework.
Applied Pattern
The core pattern is the Filter-Specifier-Pattern (Specification Pattern), which allows you to define filters and search criteria declaratively, compose LINQ expressions, and simplify testing and maintenance. The main components are:
- Filter: Object representing search criteria.
- Specifier: Function or component that applies the filter to a LINQ query.
- Selector: Projection from entities to DTOs.
- Sorting: Sorting of results.
Libraries
RoyalCode.SmartSearch.Core
Abstract components for search and filtering using LINQ and the Specification Pattern.
RoyalCode.SmartSearch.Linq
Implements property resolution, expression generation, selector mapping, and dynamic sorting.
RoyalCode.SmartSearch.EntityFramework
Integration with Entity Framework Core, adding services and extensions to register entities and perform persistent searches.
RoyalCode.SmartSearch.Abstractions
Interfaces and contracts for results, sorting, projection, and search criteria.
Main Components
ICriteria<TEntity>
Interface for defining search criteria, applying filters, sorting, and collecting results:
var criteria = provider.GetRequiredService<ICriteria<SimpleModel>>();
criteria.FilterBy(new SimpleFilter { Name = "B" });
var results = criteria.Collect(); // Returns only records with Name = "B"
ISearchManager<TDbContext>
Search manager for a DbContext, allowing you to create criteria for entities:
var manager = provider.GetRequiredService<ISearchManager<MyDbContext>>();
var criteria = manager.Criteria<MyEntity>();
Sorting and ResultList
Allows sorting and paging of results, as well as projection to DTOs:
var sorting = new Sorting { OrderBy = "Name", Direction = ListSortDirection.Ascending };
criteria.OrderBy(sorting);
ResultList<MyEntity> result = ...;
var items = result.Items;
Search Configuration
Use ISearchConfigurations to configure filters, sorting, and selectors:
services.AddEntityFrameworkSearches<MyDbContext>(cfg =>
{
cfg.Add<MyEntity>();
cfg.AddOrderBy<MyEntity, string>("Name", x => x.Name);
cfg.AddSelector<MyEntity, MyDto>(x => new MyDto { Id = x.Id, Name = x.Name });
});
Usage Examples
1. Simple Search with Filter
public class SimpleModel { public int Id; public string Name; }
public class SimpleFilter { public string Name; }
var criteria = provider.GetRequiredService<ICriteria<SimpleModel>>();
criteria.FilterBy(new SimpleFilter { Name = "B" });
var results = criteria.Collect(); // Returns only records with Name = "B"
2. Asynchronous Search
var results = await criteria.FilterBy(new SimpleFilter { Name = "A" }).CollectAsync();
3. Dynamic Sorting
criteria.OrderBy(new Sorting { OrderBy = "Name", Direction = ListSortDirection.Descending });
var results = criteria.Collect();
4. Projection to DTO
criteria.Select<MyDto>(); // Projects to the configured DTO
5. Advanced Filter Configuration
cfg.ConfigureSpecifierGenerator<MyEntity, MyFilter>(opt =>
{
opt.For(f => f.SomeProperty).Predicate(val => e => e.Collection.Any(x => x.Id == val));
});
Tests and Examples
Tests in RoyalCode.SmartSearch.Tests
demonstrate usage scenarios such as:
- Filtering by simple and complex properties
- Sorting and paging
- Projection to DTOs
- Custom filter configuration
References
- Specification Pattern
- LINQ, Entity Framework Core
For more examples, see the test files in the RoyalCode.SmartSearch.Tests
folder.
Product | Versions 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 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- RoyalCode.Extensions.PropertySelection (>= 1.0.2)
- RoyalCode.SmartSearch.Core (>= 0.8.1)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- RoyalCode.Extensions.PropertySelection (>= 1.0.2)
- RoyalCode.SmartSearch.Core (>= 0.8.1)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on RoyalCode.SmartSearch.Linq:
Package | Downloads |
---|---|
RoyalCode.WorkContext.EntityFramework
Persistence components implementation with EntityFrameworkCore, including the handling of unit of works, repositories and searches. The Work Context is an pattern that include the Unit Of Work functionality and add access to others patterns like Repository, Search, Event, Message Bus, etc. |
|
RoyalCode.SmartSearch.EntityFramework
Persistence components implementation with EntityFrameworkCore, including the handling of Searchable, Filters and Specifiers. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
0.8.1 | 18 | 3 days ago |
0.8.0 | 17 | 3 days ago |
0.8.0-preview-3 | 98 | 5 days ago |
0.8.0-preview-2 | 146 | a month ago |
0.8.0-preview-1 | 140 | a month ago |
0.7.0 | 175 | 3 months ago |