百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

LinqSpecs

> DevOps
开源

一个用于在 LINQ 查询中使用规格模式的工具集.

192 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一个用于在 LINQ 查询中使用规格模式的工具集.

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:

csharp
public abstract class Specification
{
    public abstract Expression> ToExpression();
}

So this is our implementation:

csharp
using LinqSpecs;

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

public class CustomerFromCountrySpec : Specification
{
    public Country Country { get; set; }

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

    public override Expression> ToExpression()
    { 
        return c => c.Country == Country;
    }
}

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

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

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

The usage is very simple:

csharp
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:

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

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

Usage:

csharp
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:

csharp
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:

csharp
// 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.

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

TrueSpecification and FalseSpecification

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

csharp
// This returns all customers
customerRepository.Find(new TrueSpecification());

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

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:

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

In-memory queries

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

csharp
IEnumerable 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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#csharpddddotnetlinq

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理