A .NET library for building query specifications.
- Pozitron.QuerySpecification A base package containing the core functionality, in-memory evaluators, and validators.
- Pozitron.QuerySpecification.EntityFrameworkCore
An
EntityFramework Core
plugin to the base package. It contains EF specific evaluators.
Create your specification classes by inheriting from the Specification<T>
class, and use the builder Query
to build your queries in the constructor.
public class CustomerSpec : Specification<Customer>
{
public CustomerSpec(int age, string nameTerm)
{
Query
.Where(x => x.Age > age)
.Like(x => x.Name, $"%{nameTerm}%")
.Include(x => x.Addresses)
.ThenInclude(x => x.Contact)
.OrderBy(x => x.Id)
.ThenBy(x => x.Name)
.Skip(10)
.Take(10)
.AsSplitQuery();
}
}
Apply the specification to DbSet<T>
or any IQueryable<T>
source.
var spec = new CustomerSpec(30, "John");
List<Customer> result = await _context
.Customers
.WithSpecification(spec)
.ToListAsync();
The specification can be used to project the result into a different type. Inherit from Specification<T, TResult>
class, where TResult is the type you want to project into. This offers strongly typed experience in the builder and during the evaluation.
public class CustomerDtoSpec : Specification<Customer, CustomerDto>
{
public CustomerDtoSpec(int age, string nameTerm)
{
Query
.Where(x => x.Age > age)
.Like(x => x.Name, $"%{nameTerm}%")
.OrderBy(x => x.Name)
.Select(x => new CustomerDto(x.Id, x.Name));
}
}
Apply the specification to DbSet<T>
or any IQueryable<T>
source.
var spec = new CustomerSpec(30, "John");
List<CustomerDto> result = await _context
.Customers
.WithSpecification(spec)
.ToListAsync();
If you like or are using this project please give it a star. Thanks!