Myth.Specification
3.0.0-preview
See the version list below for details.
dotnet add package Myth.Specification --version 3.0.0-preview
NuGet\Install-Package Myth.Specification -Version 3.0.0-preview
<PackageReference Include="Myth.Specification" Version="3.0.0-preview" />
paket add Myth.Specification --version 3.0.0-preview
#r "nuget: Myth.Specification, 3.0.0-preview"
// Install Myth.Specification as a Cake Addin #addin nuget:?package=Myth.Specification&version=3.0.0-preview&prerelease // Install Myth.Specification as a Cake Tool #tool nuget:?package=Myth.Specification&version=3.0.0-preview&prerelease
Myth.Specification
It is a .NET library for constructing queries in a very readable way, keeping the business rules in mind.
⭐ Features
- Easy readability
- Easy writing
- Business rules ahead
- Simplified use
🔮 Usage
To use, the following pattern must be followed:
var spec = SpecBuilder<Entity>
.Create()
.And(...)
.Or(...)
.Not()
...
The main idea is that every bit of your filter is built with the business rule in mind.
Suppose I have a Person
table and I need to filter people with a female gender identity, who are from generation Z and who live in city X.
To do this, I should create a static class with the creation of each part of this filter, as follows:
public static class PersonSpecifications {
public static ISpec<Person> IsGenerationX(this ISpec<Person> spec) {
return spec.And(person => person.Birthdate.Year >= 2000);
}
public static ISpec<Person> IsIdentifiedAsFemale(this ISpec<Person> spec) {
return spec.And(person => person.Gender == "female");
}
public static ISpec<Person> LivesOnCity(this ISpec<Person> spec, string city) {
return spec.And(person => person.Address.City == city);
}
}
And then when building my filter, search:
public class PersonService {
private IPersonRepository _personRepository;
...
public IEnumerable<Person> GetFemalePersonsOfGenerationXOfCityAsync( string city, CancellationToken cancellationToken ) {
var spec = SpecBuilder<Person>
.Create()
.IsGenerationX()
.IsIdentifiedAsFemale()
.LivesOnCity(city);
var result = _repository.SearchAsync(spec, cancellationToken);
return result;
}
}
So it's very clear that I'm looking for people with a female gender identity, from generation X and who live in the city I'm looking for.
🪄 Specifications
Specifications can be of three types and worked individually or in groups.
Applying all types can be done as follows:
var enumerable = Enumerable.Empty<Person>();
var spec = SpecBuilder<Person>
.Create()
.And(x => x.PersonId != null)
.Distinct()
.Order(x => x.Name)
.Order(x => x.Address)
.Skip(10)
.Take(10);
var result = enumerable
.Specify(spec)
.ToList();
🔽 Filters
Filters can be applied directly as follows:
var enumerable = Enumerable.Empty<Person>();
var spec = SpecBuilder<Person>
.Create()
.And(x => x.PersonId != null);
var result = enumerable
.Filter(spec)
.ToList();
The following filters are available:
And
AndIf
Or
OrIf
Not
⬇️ Ordering
Ordering can be applied directly as follows:
var enumerable = Enumerable.Empty<Person>();
var spec = SpecBuilder<Person>
.Create()
.Order(x => x.Name);
var result = enumerable
.Sort(spec)
.ToList();
The following orderings are available:
Order
OrderDescending
📃 Pagination and post processing
Paginations and post processing can be applied directly as follows:
var enumerable = Enumerable.Empty<Person>();
var spec = SpecBuilder<Person>
.Create()
.DistinctBy(x => x.Name)
.Skip(10)
.Take(10);
var result = enumerable
.Paginate(spec)
.ToList();
The following functions are available:
Skip
Take
DistinctBy
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. |
-
net8.0
- No dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on Myth.Specification:
Package | Downloads |
---|---|
Myth.Repository
It is a .NET library for defining database access repositories. |
|
Myth.Repository.EntityFramework
It is a .NET library for defining database access repositories using Entity Framework. |
|
Myth.OData
A simple OData implementation separating database entities and view models |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.0.0.1-preview | 121 | 6/29/2024 |
3.0.0-preview | 97 | 6/28/2024 |
2.0.0.17 | 1,431 | 12/15/2023 |
2.0.0.16 | 11,614 | 12/15/2023 |
2.0.0.15 | 184 | 12/15/2023 |
2.0.0.11 | 4,706 | 8/11/2022 |
2.0.0.10 | 2,643 | 7/20/2022 |
2.0.0.9 | 2,784 | 7/15/2022 |
2.0.0.8 | 2,784 | 7/12/2022 |
2.0.0.7 | 2,717 | 6/20/2022 |
2.0.0.6 | 2,822 | 5/23/2022 |
2.0.0.5 | 2,795 | 5/18/2022 |
2.0.0 | 2,891 | 2/17/2022 |