Antanidoss.Specification 1.0.0

There is a newer version of this package available.
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
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Antanidoss.Specification" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Antanidoss.Specification --version 1.0.0
#r "nuget: Antanidoss.Specification, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

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.

Version Downloads Last updated
2.0.0 477 1/16/2023
1.0.1 373 10/24/2022
1.0.0 892 6/29/2022