diff --git a/QuerySpecification.sln b/QuerySpecification.sln index ca5a987..7a90114 100644 --- a/QuerySpecification.sln +++ b/QuerySpecification.sln @@ -22,11 +22,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .github\workflows\build.yml = .github\workflows\build.yml .github\workflows\ci.yml = .github\workflows\ci.yml clean.sh = clean.sh - Directory.Packages.props = Directory.Packages.props tests\Directory.Build.props = tests\Directory.Build.props src\Directory.Build.props = src\Directory.Build.props + Directory.Packages.props = Directory.Packages.props exclusion.dic = exclusion.dic LICENSE.txt = LICENSE.txt + readme-nuget.md = readme-nuget.md README.md = README.md .github\workflows\release.yml = .github\workflows\release.yml run-tests.sh = run-tests.sh diff --git a/readme-nuget.md b/readme-nuget.md new file mode 100644 index 0000000..d87a650 --- /dev/null +++ b/readme-nuget.md @@ -0,0 +1,71 @@ +A .NET library for building query specifications. +- [Pozitron.QuerySpecification](https://www.nuget.org/packages/Pozitron.QuerySpecification) + A base package containing the core functionality, in-memory evaluators, and validators. +- [Pozitron.QuerySpecification.EntityFrameworkCore](https://www.nuget.org/packages/Pozitron.QuerySpecification.EntityFrameworkCore) + An `EntityFramework Core` plugin to the base package. It contains EF specific evaluators. + +## Usage + +Create your specification classes by inheriting from the `Specification` class, and use the builder `Query` to build your queries in the constructor. + +```csharp +public class CustomerSpec : Specification +{ + 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` or to any `IQueryable` source. + +```csharp +var spec = new CustomerSpec(30, "John"); + +List result = await _context + .Customers + .WithSpecification(spec) + .ToListAsync(); +``` + +### Projections + +The specification can be used to project the result into a different type. Inherit from `Specification` class, where TResult is the type you want to project into. This offers strongly typed experience in the builder and during the evaluation. + +```csharp +public class CustomerDtoSpec : Specification +{ + 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` or any `IQueryable` source. + +```csharp +var spec = new CustomerSpec(30, "John"); + +List result = await _context + .Customers + .WithSpecification(spec) + .ToListAsync(); +``` + +## Give a Star! :star: +If you like or are using this project please give it a star. Thanks! diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 8c3a297..22f5296 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -32,15 +32,15 @@ true git MIT - README.md + readme-nuget.md https://pozitrongroup.com/PozitronLogo.png pozitronicon.png - + - \ No newline at end of file +