Skip to content

AdemCatamak/Spectacular

Repository files navigation

Spectacular

This library is implementation of the Specification Design Pattern in C# language.

Expression has been used in this implementation. Thanks to Expression, developers can use Specification for querying database through libraries such as EntityFramework.

You can combine Specification classes through the And operator included as built-In. Via these operator, you can easily create your complex queries by combining smaller criteria.

For more information about the Specification Design Pattern, you can check the story on medium.

Platform Status
Travis Travis
GitHub .github/workflows/github.yml
NuGet Package Name Version
Spectacular Nuget

Give a Star! ⭐

If you like this project please give it a star 😊

Getting Started

Preparing custom specification via AbstractSpecification

public class GenderShould : AbstractSpecification<Person>
{
    private GenderShould(Genders gender)
        : base(person => person.Gender == gender)
    {
    }

    public static AbstractSpecification<Person> Be(Genders gender)
    {
        GenderShould genderShould = new(gender);
        return genderShould;
    }
    
    public static AbstractSpecification<Person> BeMale => Be(Genders.Male);
    public static AbstractSpecification<Person> BeFemale => Be(Genders.Female);
}

Checking whether an object satisfy the criteria :

Person person = new("adem", Genders.Male);

AbstractSpecification<Person> spec = GenderShould.BeMale;
bool isSatisfied = spec.IsSatisfiedBy(person);

Filtering the collection by criteria :

IQueryable<Person> personCollection = ...

AbstractSpecification<Person> spec = GenderShould.BeMale;
var filteredCollection = personCollection.Where(spec).ToList();

Combine criteria :

var ageCriteria = AgeShould.BeGreaterThan(18).And(AgeShould.BeLessThan(35));
SpecificationGroup<Person> specificationGroup = ageCriteria.And(GenderShould.BeFemale);

var filteredCollection = personCollection.Where(specificationGroup).ToList();

About

Implementation of Specification Design Pattern

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published