All Projects → navozenko → LinqSpecs

navozenko / LinqSpecs

Licence: MS-PL license
A toolset for use the specification pattern in LINQ queries.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to LinqSpecs

QuerySpecification
Abstract package for building query specifications in your domain model.
Stars: ✭ 18 (-88.82%)
Mutual labels:  ddd, specification
spec-pattern
Specification design pattern for JavaScript and TypeScript with bonus classes
Stars: ✭ 43 (-73.29%)
Mutual labels:  specification, specification-pattern
LinqBuilder
LinqBuilder is an advanced implementation of the specification pattern specifically targeting LINQ query generation.
Stars: ✭ 34 (-78.88%)
Mutual labels:  linq, specification-pattern
beacon-APIs
Collection of RESTful APIs provided by Ethereum Beacon nodes
Stars: ✭ 209 (+29.81%)
Mutual labels:  specification
Joker
An example of microservices container based application which implemented different approaches within each microservice (DDD, CQRS, Simple CRUD)
Stars: ✭ 41 (-74.53%)
Mutual labels:  ddd
EFSqlTranslator
A standalone linq to sql translator that can be used with EF and Dapper.
Stars: ✭ 51 (-68.32%)
Mutual labels:  linq
FastLinq
Drop-in-place Memory and Performance optimizations for LINQ
Stars: ✭ 22 (-86.34%)
Mutual labels:  linq
Lumen-Doctrine-DDD-Example
Domain Driven Design Application Example, built with Lumen 5.3 and Doctrine.
Stars: ✭ 72 (-55.28%)
Mutual labels:  ddd
kingdom-python-server
Modular, cohesive, transparent and fast web server template
Stars: ✭ 20 (-87.58%)
Mutual labels:  ddd
gender-render
Template-system and proof-of-concept for rendering gender-neutral text-, email- and RPG-text-templates with the correct pronouns of all people involved.
Stars: ✭ 21 (-86.96%)
Mutual labels:  specification
muon-java
Muon Core for the JVM. APIs and Microservices taken to the next level
Stars: ✭ 18 (-88.82%)
Mutual labels:  ddd
event-store-mgmt-ui
Event Store Management UI
Stars: ✭ 23 (-85.71%)
Mutual labels:  ddd
archimedes-jvm
Archimedes's implementation for the Java Virtual Machine (JVM)
Stars: ✭ 24 (-85.09%)
Mutual labels:  ddd
openapi
OpenAPI 3 Specification for golang
Stars: ✭ 18 (-88.82%)
Mutual labels:  specification
eventcatalog
Discover, Explore and Document your Event Driven Architectures powered by Markdown.
Stars: ✭ 392 (+143.48%)
Mutual labels:  ddd
LinqBenchmarks
Benchmarking LINQ and alternative implementations
Stars: ✭ 138 (-14.29%)
Mutual labels:  linq
Contexture
Wizard for the Bounded-Context-Canvas
Stars: ✭ 92 (-42.86%)
Mutual labels:  ddd
framework
Lightweight, open source and magic-free framework for testing solidity smart contracts.
Stars: ✭ 36 (-77.64%)
Mutual labels:  specification
bdd-for-all
Flexible and easy to use library to enable your behavorial driven development (BDD) teams to easily collaborate while promoting automation, transparency and reporting.
Stars: ✭ 42 (-73.91%)
Mutual labels:  ddd
kekiri
A .NET framework that supports writing low-ceremony BDD tests using Gherkin language
Stars: ✭ 19 (-88.2%)
Mutual labels:  specification

LinqSpecs is a framework that will help you to create specifications for LINQ queries that can be executed by a remote server. You can read more about the specification pattern in Wikipedia.

Almost all users of LINQ create specifications in their daily work, but most of them write those specifications scattered all over the code. The idea behind this project is to help the user to write, test and expose specifications as first-class objects. You will learn how to use LinqSpecs in this brief document.

Defining simple specifications

In order to define our first specification named "CustomerFromCountrySpec" we need to inherit from Specification<T>:

public abstract class Specification<T>
{
    public abstract Expression<Func<T, bool>> ToExpression();
}

So this is our implementation:

using LinqSpecs;

public enum Country { Argentina, France, Italia, ... }

public class CustomerFromCountrySpec : Specification<Customer>
{
    public Country Country { get; set; }

    public CustomerFromCountrySpec(Country country)
    {
        Country = country;
    }

    public override Expression<Func<Customer, bool>> ToExpression()
    { 
        return c => c.Country == Country;
    }
}

Simple as is, to use this class, your repository or DAO should implement these kind of methods:

public IEnumerable<T> Find(Specification<T> specification)
{
    return [a queryable source].Where(specification).ToList();
}

public int Count(Specification<T> specification)
{
    return [a queryable source].Count(specification);
}

The usage is very simple:

var spec = new CustomerFromCountrySpec(Country.Argentina);
var customersFromArgentina = customerRepository.Find(spec);

Alternative way to expose specifications

An alternative way of exposing specifications is with a static class:

public static class CustomerSpecs
{
    public static Specification<Customer> FromCountry(Country country) 
    { 
        return new CustomerFromCountrySpec(country);
    }

    public static Specification<Customer> EligibleForDiscount(decimal discount)
    {
        return new AdHocSpecification<Customer>(
            c => c.IsPreferred && !c.HasDebt &&
                 c.LastPurchaseDate > DateTime.Today.AddDays(-30));
    }
}

Usage:

customerRepository.Find(
    CustomerSpecs.FromCountry(argentina) &&
    CustomerSpecs.EligibleForDiscount(3223));

Logical operations AND, OR, NOT

One of the most interesting features of LinqSpecs is that you can combine known specifications with "!", "&&" and "||" operators. For example:

var spec1 = new CustomerFromCountrySpec(Country.Argentina);
var spec2 = new CustomerPreferredSpec();
var result = customerRepository.Find(spec1 && !spec2);

This code returns all customers from Argentina that are not preferred. The & operator work as an && (AndAlso) operator. The same for | and ||.

Comparing

The result of and'ing, or'ing and negating specifications implements equality members. That's said:

// This returns true
(spec1 && spec2).Equals(spec1 && spec2);

// This returns true
(spec1 && (spec2 || !spec3)).Equals(spec1 && (spec2 || !spec3));

// This returns false, because AndAlso and OrElse are not commutable operations
(spec1 && spec2).Equals(spec2 && spec1);

This is an useful feature when you are writing Asserts in your unit tests.

AdHocSpecification

The AdHocSpecification is an alternative way to write a specification without writing a class. You should not abuse of them, and try to write those in a single place as explained above. Also AdHocSpecification doesn't implement Equals, so two AdHocSpecifications are equal only if they are the same instance.

var spec = new AdHocSpecification<Customer>(c => c.IsPreferred && !c.HasDebt);

TrueSpecification and FalseSpecification

The TrueSpecification is satisfied by any object. The FalseSpecification is not satisfied by any object.

// This returns all customers
customerRepository.Find(new TrueSpecification<Customer>());

// This returns nothing
customerRepository.Find(new FalseSpecification<Customer>());

These specifications can be useful when you want to retrieve all items from a data source or when you are building a chain of several specifications. For example:

Specification<Customer> spec = new FalseSpecification<Customer>();
foreach (var country in countries)
    spec |= new CustomerFromCountrySpec(country);
return spec;

In-memory queries

Although LinqSpecs is targeted towards IQueryable<T> data source, it is possible to use LinqSpecs specifications for filtering IEnumerable<T> collections and also for other checks in memory:

IEnumerable<Customer> customers = ...
var spec = new CustomerFromCountrySpec(Country.Argentina);
var result = customers.Where(spec.ToExpression().Compile());

Compiling of expression tree into a delegate is a very slow operation, so it's a good idea to cache the result of a compilation for reuse if it's possible.

Supported platforms

  • .NET Standard 2.0+
  • .NET Framework 4.0+
  • .NET Core 2.0+

License

LinqSpecs is open-sourced software licensed under the Microsoft Public License (MS-PL).

Contributors

LinqSpecs was created by José F. Romaniello and Sergey Navozenko.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].