Antanidoss.Specification
1.0.0
See the version list below for details.
dotnet add package Antanidoss.Specification --version 1.0.0
NuGet\Install-Package Antanidoss.Specification -Version 1.0.0
<PackageReference Include="Antanidoss.Specification" Version="1.0.0" />
paket add Antanidoss.Specification --version 1.0.0
#r "nuget: Antanidoss.Specification, 1.0.0"
// Install Antanidoss.Specification as a Cake Addin
#addin nuget:?package=Antanidoss.Specification&version=1.0.0
// Install Antanidoss.Specification as a Cake Tool
#tool nuget:?package=Antanidoss.Specification&version=1.0.0
Give a Star! ⭐
If you like or are using this project please give it a star. Thanks!
Install
Current version of nuget package - https://www.nuget.org/packages/Antanidoss.Specification/1.0.0
The framework is provided as a set of NuGet packages.
To install the minimum requirements:
Install-Package Antanidoss.Specification
Usage
This project is needed to facilitate the use of filters on large projects. For example, there is the following class
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
And for this class, we want to have a repository in which we want to filter this class for certain scenarios
public interface IPersonRepository
{
IEnumerable<Person> GetByName(string name);
IEnumerable<Person> GetByAge(int age);
IEnumerable<Person> GetByAgeAndName(int age, string name);
}
In this case, we see that this class is difficult to expand and the more properties the Person class has, the more filtering methods there will be. What solution do I propose? I propose to implement filters as a specification pattern, and connect and combine them as we need. Let's make the following filter specifications
public class PersonByNameSpec : ISpecification<Person>
{
private readonly string _name;
public PersonByNameSpec(string name)
{
_name = name;
}
public Expression<Func<Person, bool>> ToExpression()
{
return p => p.Name.Contains(_name);
}
}
public class PersonByAgeSpec : ISpecification<Person>
{
private readonly int _age;
public PersonByAgeSpec(int age)
{
_age = age;
}
public Expression<Func<Person, bool>> ToExpression()
{
return p => p.Age == _age;
}
}
Now we have specification. Let's change our IPersonRepository
public interface IPersonRepository
{
IEnumerable<Person> GetFilter(ISpecification<Person> specification);
}
public class PersonRepository : IPersonRepository
{
private readonly EFDbContext _context;
public PersonRepository(EFDbContext context)
{
_context = context;
}
public IEnumerable<Person> GetBySpecification(ISpecification<Person> specification)
{
return _context.Persons.Where(specification.ToExpression());
}
}
Excellent! But how do we search by name and age?
//Search by name
var specification = new PersonByNameSpec("Anton");
_personRepository.GetBySpecification(specification);
//Search by name or age
var specification = new PersonByNameSpec("Anton").Or(new PersonByAgeSpec(20));
_personRepository.GetBySpecification(specification);
//Search by name and age
var specification = new PersonByNameSpec("Anton").And(new PersonByAgeSpec(20));
_personRepository.GetBySpecification(specification);
Thus, you can connect and combine specifications as much as you like. At the moment, you can connect filters using AND, Or, wrap filters in brackets, add a Not condition to filters
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- LinqKit (>= 1.2.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Antanidoss.Specification:
Package | Downloads |
---|---|
Antanidoss.Specification.Filters
Using Filters with Specifications |
GitHub repositories
This package is not used by any popular GitHub repositories.