├── src ├── EF6 │ ├── LinqBuilder.EF6.Tests │ │ ├── xunit.runner.json │ │ ├── Data │ │ │ ├── SomeChildEntity.cs │ │ │ ├── SomeEntity.cs │ │ │ ├── Specifications │ │ │ │ ├── OrderSpecification.cs │ │ │ │ └── ChildValueSpecification.cs │ │ │ ├── SqliteConfiguration.cs │ │ │ ├── TestDbContext.cs │ │ │ └── TestDb.cs │ │ ├── Properties │ │ │ └── AssemblyInfo.cs │ │ ├── .globalconfig │ │ ├── IntegrationTests.cs │ │ ├── LinqBuilder.EF6.Tests.csproj │ │ └── EntityFrameworkExtensionsTests.cs │ └── LinqBuilder.EF6 │ │ ├── Properties │ │ └── AssemblyInfo.cs │ │ ├── README.md │ │ ├── LinqBuilder.EF6.csproj │ │ └── EntityFrameworkExtensions.cs ├── EFCore │ ├── LinqBuilder.EFCore.Tests │ │ ├── xunit.runner.json │ │ ├── Data │ │ │ ├── SomeChildEntity.cs │ │ │ ├── SomeEntity.cs │ │ │ ├── TestDbContext.cs │ │ │ ├── Specifications │ │ │ │ ├── OrderSpecification.cs │ │ │ │ └── ChildValueSpecification.cs │ │ │ └── TestDb.cs │ │ ├── Properties │ │ │ └── AssemblyInfo.cs │ │ ├── .globalconfig │ │ ├── LinqBuilder.EFCore.Tests.csproj │ │ ├── IntegrationTests.cs │ │ └── EntityFrameworkCoreExtensionsTests.cs │ └── LinqBuilder.EFCore │ │ ├── Properties │ │ └── AssemblyInfo.cs │ │ ├── README.md │ │ ├── LinqBuilder.EFCore.csproj │ │ └── EntityFrameworkCoreExtensions.cs ├── LinqBuilder │ ├── OrderBy │ │ ├── Sort.cs │ │ ├── IOrderedSpecification.cs │ │ ├── OrderSpec.cs │ │ ├── IOrderSpecification.cs │ │ ├── LinqExtensions.cs │ │ ├── OrderSpecification.cs │ │ └── SpecificationExtensions.cs │ ├── ISpecification.cs │ ├── IDynamicSpecification{TEntity,TValue}.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── IDynamicSpecification{TEntity,TValue1,TValue2,TValue3}.cs │ ├── IDynamicSpecification{TEntity,TValue1,TValue2,TValue3,TValue4}.cs │ ├── IDynamicSpecification{TEntity,TValue1,TValue2}.cs │ ├── Internal │ │ ├── IQuerySpecification.cs │ │ ├── DummySpecification.cs │ │ ├── QuerySpecification.cs │ │ └── SpecificationBase.cs │ ├── Spec.cs │ ├── DynamicSpecification{TEntity,TValue}.cs │ ├── DynamicSpecification{TEntity,TValue1,TValue2}.cs │ ├── DynamicSpecification{TEntity,TValue1,TValue2,TValue3}.cs │ ├── LinqBuilder.csproj │ ├── MultiSpecification{TEntity1,TEntity2,TEntity3}.cs │ ├── DynamicSpecification{TEntity,TValue1,TValue2,TValue3,TValue4}.cs │ ├── MultiSpecification{TEntity1,TEntity2,TEntity3,TEntity4}.cs │ ├── Specification.cs │ ├── MultiSpecification{TEntity1,TEntity2}.cs │ ├── SpecificationExtensions.cs │ ├── README.md │ └── LinqExtensions.cs └── LinqBuilder.Tests │ ├── Properties │ └── AssemblyInfo.cs │ ├── .globalconfig │ ├── Data │ ├── Entity.cs │ ├── Entity2.cs │ ├── Entity3.cs │ ├── Entity4.cs │ ├── Entity5.cs │ ├── Specifications │ │ ├── Specification.cs │ │ ├── OrderSpecification.cs │ │ ├── MultiEntitySpecification2.cs │ │ ├── MultipleValueSpecification1.cs │ │ ├── MultipleValueSpecification2.cs │ │ ├── MultiEntitySpecification3.cs │ │ ├── MultipleValueSpecification3.cs │ │ ├── MultipleValueSpecification4.cs │ │ └── MultiEntitySpecification4.cs │ ├── EntityTheoryData.cs │ └── Fixture.cs │ ├── Internal │ ├── QuerySpecificationTests.cs │ └── SpecificationBaseTests.cs │ ├── SpecTests.cs │ ├── LinqBuilder.Tests.csproj │ ├── OrderBy │ ├── OrderSpecTests.cs │ ├── LinqExtensionsTests.cs │ ├── OrderSpecificationTests.cs │ └── SpecificationExtensionsTests.cs │ ├── DynamicSpecificationTests.cs │ ├── SpecificationTests.cs │ ├── MultiSpecificationTests.cs │ ├── SpecificationExtensionsTests.cs │ ├── LinqExtensionsExeSpecTests.cs │ └── LinqExtensionsTests.cs ├── .nuke ├── parameters.json └── build.schema.json ├── .vscode ├── extensions.json └── settings.json ├── GitVersion.yml ├── .idea └── .idea.LinqBuilder │ └── .idea │ ├── codeStyles │ └── codeStyleConfig.xml │ ├── vcs.xml │ ├── indexLayout.xml │ ├── misc.xml │ └── projectSettingsUpdater.xml ├── NuGet.Config ├── .editorconfig ├── stylecop.json ├── .globalconfig ├── appveyor.yml ├── LICENSE.txt ├── LinqBuilder.sln.DotSettings ├── Directory.Build.props ├── Directory.Packages.props ├── .gitattributes ├── LinqBuilder.sln ├── README.md └── .gitignore /src/EF6/LinqBuilder.EF6.Tests/xunit.runner.json: -------------------------------------------------------------------------------- 1 | { 2 | "parallelizeTestCollections": false 3 | } 4 | -------------------------------------------------------------------------------- /.nuke/parameters.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./build.schema.json", 3 | "Solution": "LinqBuilder.sln" 4 | } -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/xunit.runner.json: -------------------------------------------------------------------------------- 1 | { 2 | "parallelizeTestCollections": false 3 | } 4 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "ms-dotnettools.csharp", 4 | "editorconfig.editorconfig" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /GitVersion.yml: -------------------------------------------------------------------------------- 1 | next-version: 3.2.0 2 | branches: 3 | main: 4 | label: beta 5 | develop: 6 | increment: Patch 7 | ignore: 8 | sha: [] 9 | -------------------------------------------------------------------------------- /src/LinqBuilder/OrderBy/Sort.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder.OrderBy 2 | { 3 | public enum Sort 4 | { 5 | Ascending, 6 | Descending, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /.idea/.idea.LinqBuilder/.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/Data/SomeChildEntity.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder.EF6.Tests.Data; 2 | 3 | public class SomeChildEntity 4 | { 5 | public int Id { get; set; } 6 | 7 | public int Value { get; set; } 8 | } 9 | -------------------------------------------------------------------------------- /src/LinqBuilder/OrderBy/IOrderedSpecification.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder.OrderBy 2 | { 3 | public interface IOrderedSpecification : ISpecification 4 | where TEntity : class 5 | { 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /.idea/.idea.LinqBuilder/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/Data/SomeChildEntity.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder.EFCore.Tests.Data; 2 | 3 | public class SomeChildEntity 4 | { 5 | public int Id { get; set; } 6 | 7 | public int Value { get; set; } 8 | } 9 | -------------------------------------------------------------------------------- /NuGet.Config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/.idea.LinqBuilder/.idea/indexLayout.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.encoding": "utf8", 3 | "files.exclude": { 4 | ".git": true, 5 | ".vs": true, 6 | "artifacts": true, 7 | "coverage": true, 8 | "**/obj": true, 9 | "**/bin": true, 10 | "tools/*": true 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | // These attributes will be patched by GitVersion 4 | [assembly: AssemblyVersion("1.0.0.0")] 5 | [assembly: AssemblyFileVersion("1.0.0.0")] 6 | [assembly: AssemblyInformationalVersion("1.0.0.0")] 7 | -------------------------------------------------------------------------------- /src/LinqBuilder/ISpecification.cs: -------------------------------------------------------------------------------- 1 | using LinqBuilder.Internal; 2 | 3 | namespace LinqBuilder 4 | { 5 | public interface ISpecification 6 | where TEntity : class 7 | { 8 | SpecificationBase Internal { get; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.idea/.idea.LinqBuilder/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | // These attributes will be patched by GitVersion 4 | [assembly: AssemblyVersion("1.0.0.0")] 5 | [assembly: AssemblyFileVersion("1.0.0.0")] 6 | [assembly: AssemblyInformationalVersion("1.0.0.0")] 7 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | // These attributes will be patched by GitVersion 4 | [assembly: AssemblyVersion("1.0.0.0")] 5 | [assembly: AssemblyFileVersion("1.0.0.0")] 6 | [assembly: AssemblyInformationalVersion("1.0.0.0")] 7 | -------------------------------------------------------------------------------- /src/LinqBuilder/IDynamicSpecification{TEntity,TValue}.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder 2 | { 3 | public interface IDynamicSpecification 4 | : ISpecification 5 | where TEntity : class 6 | { 7 | TValue Value { get; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | insert_final_newline = true 6 | trim_trailing_whitespace = true 7 | indent_style = space 8 | indent_size = 2 9 | end_of_line = lf 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.cs] 15 | indent_size = 4 16 | -------------------------------------------------------------------------------- /.idea/.idea.LinqBuilder/.idea/projectSettingsUpdater.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /src/LinqBuilder/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | [assembly:CLSCompliant(true)] 5 | 6 | // These attributes will be patched by GitVersion 7 | [assembly: AssemblyVersion("1.0.0.0")] 8 | [assembly: AssemblyFileVersion("1.0.0.0")] 9 | [assembly: AssemblyInformationalVersion("1.0.0.0")] 10 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | [assembly:CLSCompliant(true)] 5 | 6 | // These attributes will be patched by GitVersion 7 | [assembly: AssemblyVersion("1.0.0.0")] 8 | [assembly: AssemblyFileVersion("1.0.0.0")] 9 | [assembly: AssemblyInformationalVersion("1.0.0.0")] 10 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/.globalconfig: -------------------------------------------------------------------------------- 1 | dotnet_diagnostic.CA1014.severity = none 2 | dotnet_diagnostic.CA1062.severity = none 3 | dotnet_diagnostic.CA1515.severity = none 4 | dotnet_diagnostic.CA1707.severity = none 5 | dotnet_diagnostic.CA1812.severity = none 6 | dotnet_diagnostic.CA2007.severity = none 7 | dotnet_diagnostic.CA2227.severity = none 8 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/.globalconfig: -------------------------------------------------------------------------------- 1 | dotnet_diagnostic.CA1014.severity = none 2 | dotnet_diagnostic.CA1062.severity = none 3 | dotnet_diagnostic.CA1515.severity = none 4 | dotnet_diagnostic.CA1707.severity = none 5 | dotnet_diagnostic.CA1812.severity = none 6 | dotnet_diagnostic.CA2007.severity = none 7 | dotnet_diagnostic.CA2227.severity = none 8 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | [assembly:CLSCompliant(true)] 5 | 6 | // These attributes will be patched by GitVersion 7 | [assembly: AssemblyVersion("1.0.0.0")] 8 | [assembly: AssemblyFileVersion("1.0.0.0")] 9 | [assembly: AssemblyInformationalVersion("1.0.0.0")] 10 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Entity.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable PropertyCanBeMadeInitOnly.Global 2 | namespace LinqBuilder.Tests.Data; 3 | 4 | public class Entity 5 | { 6 | public int Value1 { get; set; } 7 | 8 | public int Value2 { get; set; } 9 | 10 | public int Value3 { get; set; } 11 | 12 | public int Value4 { get; set; } 13 | } 14 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/.globalconfig: -------------------------------------------------------------------------------- 1 | dotnet_diagnostic.CA1014.severity = none 2 | dotnet_diagnostic.CA1062.severity = none 3 | dotnet_diagnostic.CA1515.severity = none 4 | dotnet_diagnostic.CA1707.severity = none 5 | dotnet_diagnostic.CA1812.severity = none 6 | dotnet_diagnostic.CA2007.severity = none 7 | dotnet_diagnostic.CA2227.severity = none 8 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Entity2.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable PropertyCanBeMadeInitOnly.Global 2 | namespace LinqBuilder.Tests.Data; 3 | 4 | public class Entity2 5 | { 6 | public int Value1 { get; set; } 7 | 8 | public int Value2 { get; set; } 9 | 10 | public int Value3 { get; set; } 11 | 12 | public int Value4 { get; set; } 13 | } 14 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Entity3.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable PropertyCanBeMadeInitOnly.Global 2 | namespace LinqBuilder.Tests.Data; 3 | 4 | public class Entity3 5 | { 6 | public int Value1 { get; set; } 7 | 8 | public int Value2 { get; set; } 9 | 10 | public int Value3 { get; set; } 11 | 12 | public int Value4 { get; set; } 13 | } 14 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Entity4.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable PropertyCanBeMadeInitOnly.Global 2 | namespace LinqBuilder.Tests.Data; 3 | 4 | public class Entity4 5 | { 6 | public int Value1 { get; set; } 7 | 8 | public int Value2 { get; set; } 9 | 10 | public int Value3 { get; set; } 11 | 12 | public int Value4 { get; set; } 13 | } 14 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Entity5.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable PropertyCanBeMadeInitOnly.Global 2 | namespace LinqBuilder.Tests.Data; 3 | 4 | public class Entity5 5 | { 6 | public int Value1 { get; set; } 7 | 8 | public int Value2 { get; set; } 9 | 10 | public int Value3 { get; set; } 11 | 12 | public int Value4 { get; set; } 13 | } 14 | -------------------------------------------------------------------------------- /src/LinqBuilder/IDynamicSpecification{TEntity,TValue1,TValue2,TValue3}.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder 2 | { 3 | public interface IDynamicSpecification 4 | : IDynamicSpecification 5 | where TEntity : class 6 | { 7 | TValue3 Value3 { get; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/Specification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Tests.Data.Specifications; 5 | 6 | public class Specification : Specification 7 | { 8 | public override Expression> AsExpression() 9 | { 10 | return entity => entity.Value1 == 1; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/LinqBuilder/IDynamicSpecification{TEntity,TValue1,TValue2,TValue3,TValue4}.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder 2 | { 3 | public interface IDynamicSpecification 4 | : IDynamicSpecification 5 | where TEntity : class 6 | { 7 | TValue4 Value4 { get; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/LinqBuilder/IDynamicSpecification{TEntity,TValue1,TValue2}.cs: -------------------------------------------------------------------------------- 1 | using LinqBuilder.Internal; 2 | 3 | namespace LinqBuilder 4 | { 5 | public interface IDynamicSpecification 6 | : IQuerySpecification 7 | where TEntity : class 8 | { 9 | TValue1 Value1 { get; } 10 | 11 | TValue2 Value2 { get; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/LinqBuilder/Internal/IQuerySpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Internal 5 | { 6 | public interface IQuerySpecification : ISpecification 7 | where TEntity : class 8 | { 9 | Expression>? AsExpression(); 10 | 11 | Func? AsFunc(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Internal/QuerySpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using LinqBuilder.Tests.Data; 3 | using Xunit; 4 | 5 | namespace LinqBuilder.Tests.Internal; 6 | 7 | public class QuerySpecificationTests 8 | { 9 | [Fact] 10 | public void AsFunc_NoExpression_ShouldBeNull() 11 | { 12 | new Specification().AsFunc().Should().BeNull(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stylecop.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", 3 | "settings": { 4 | "indentation": { 5 | "indentationSize": 4, 6 | "useTabs": false 7 | }, 8 | "orderingRules": { 9 | "usingDirectivesPlacement": "outsideNamespace" 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/Data/SomeEntity.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace LinqBuilder.EF6.Tests.Data; 4 | 5 | public class SomeEntity 6 | { 7 | public int Id { get; set; } 8 | 9 | public int Value1 { get; set; } 10 | 11 | public int Value2 { get; set; } 12 | 13 | public virtual ICollection ChildEntities { get; set; } = new HashSet(); 14 | } 15 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/Data/SomeEntity.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace LinqBuilder.EFCore.Tests.Data; 4 | 5 | public class SomeEntity 6 | { 7 | public int Id { get; set; } 8 | 9 | public int Value1 { get; set; } 10 | 11 | public int Value2 { get; set; } 12 | 13 | public virtual ICollection ChildEntities { get; set; } = new HashSet(); 14 | } 15 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/Data/Specifications/OrderSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using LinqBuilder.OrderBy; 4 | 5 | namespace LinqBuilder.EF6.Tests.Data.Specifications; 6 | 7 | public class OrderSpecification(Sort sort = Sort.Ascending) : OrderSpecification(sort) 8 | { 9 | public override Expression> AsExpression() 10 | { 11 | return entity => entity.Value1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/Data/TestDbContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace LinqBuilder.EFCore.Tests.Data; 4 | 5 | public class TestDbContext : DbContext 6 | { 7 | public TestDbContext(DbContextOptions options) 8 | : base(options) 9 | { 10 | } 11 | 12 | public virtual DbSet Entities { get; set; } = null!; 13 | 14 | public virtual DbSet ChildEntities { get; set; } = null!; 15 | } 16 | -------------------------------------------------------------------------------- /src/LinqBuilder/Spec.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder 5 | { 6 | // Just an alias of Specification 7 | public sealed class Spec : Specification 8 | where TEntity : class 9 | { 10 | public Spec() 11 | { 12 | } 13 | 14 | public Spec(Expression> expression) 15 | : base(expression) 16 | { 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/Data/Specifications/ChildValueSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Linq.Expressions; 4 | 5 | namespace LinqBuilder.EF6.Tests.Data.Specifications; 6 | 7 | public class ChildValueSpecification(int value) : Specification 8 | { 9 | public override Expression> AsExpression() 10 | { 11 | return entity => entity.ChildEntities.Any(x => x.Value == value); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.globalconfig: -------------------------------------------------------------------------------- 1 | is_global = true 2 | 3 | dotnet_diagnostic.CA1000.severity = none 4 | dotnet_diagnostic.CA1510.severity = none # Does not work in .NET Standard 5 | dotnet_diagnostic.CA1859.severity = none 6 | 7 | dotnet_diagnostic.CS1591.severity = none 8 | 9 | dotnet_diagnostic.SA1101.severity = none 10 | dotnet_diagnostic.SA1309.severity = none 11 | dotnet_diagnostic.SA1600.severity = none 12 | dotnet_diagnostic.SA1602.severity = none 13 | dotnet_diagnostic.SA1633.severity = none 14 | -------------------------------------------------------------------------------- /src/LinqBuilder/OrderBy/OrderSpec.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.OrderBy 5 | { 6 | // Just an alias of OrderSpecification 7 | public sealed class OrderSpec : OrderSpecification 8 | where TEntity : class 9 | { 10 | public OrderSpec(Expression> expression, Sort sort = Sort.Ascending) 11 | : base(expression, sort) 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/LinqBuilder/Internal/DummySpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Internal 5 | { 6 | internal sealed class DummySpecification : IQuerySpecification 7 | where TEntity : class 8 | { 9 | public SpecificationBase Internal => new SpecificationBase(this); 10 | 11 | public Expression>? AsExpression() => null; 12 | 13 | public Func? AsFunc() => null; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/OrderSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using LinqBuilder.OrderBy; 4 | 5 | namespace LinqBuilder.Tests.Data.Specifications; 6 | 7 | public class OrderSpecification : OrderSpecification 8 | { 9 | public OrderSpecification(Sort sort = Sort.Ascending) 10 | : base(sort) 11 | { 12 | } 13 | 14 | public override Expression> AsExpression() 15 | { 16 | return entity => entity.Value1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/Data/Specifications/OrderSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using LinqBuilder.OrderBy; 4 | 5 | namespace LinqBuilder.EFCore.Tests.Data.Specifications; 6 | 7 | public class OrderSpecification : OrderSpecification 8 | { 9 | public OrderSpecification(Sort sort = Sort.Ascending) 10 | : base(sort) 11 | { 12 | } 13 | 14 | public override Expression> AsExpression() 15 | { 16 | return entity => entity.Value1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/MultiEntitySpecification2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Tests.Data.Specifications; 5 | 6 | public class MultiEntitySpecification2 : MultiSpecification 7 | { 8 | public override Expression> AsExpressionForEntity1() 9 | { 10 | return entity => entity.Value1 == 1; 11 | } 12 | 13 | public override Expression> AsExpressionForEntity2() 14 | { 15 | return entity => entity.Value2 == 2; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/MultipleValueSpecification1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Tests.Data.Specifications; 5 | 6 | public class MultipleValueSpecification1 : DynamicSpecification 7 | { 8 | public MultipleValueSpecification1() 9 | { 10 | } 11 | 12 | public MultipleValueSpecification1(int value1) 13 | : base(value1) 14 | { 15 | } 16 | 17 | public override Expression> AsExpression() 18 | { 19 | return entity => entity.Value1 == Value; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/Data/Specifications/ChildValueSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Linq.Expressions; 4 | 5 | namespace LinqBuilder.EFCore.Tests.Data.Specifications; 6 | 7 | public class ChildValueSpecification : Specification 8 | { 9 | private readonly int _value; 10 | 11 | public ChildValueSpecification(int value) 12 | { 13 | _value = value; 14 | } 15 | 16 | public override Expression> AsExpression() 17 | { 18 | return entity => entity.ChildEntities.Any(x => x.Value == _value); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/LinqBuilder/OrderBy/IOrderSpecification.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace LinqBuilder.OrderBy 5 | { 6 | public interface IOrderSpecification : ISpecification 7 | where TEntity : class 8 | { 9 | IOrderedQueryable InvokeSort(IQueryable query); 10 | 11 | IOrderedEnumerable InvokeSort(IEnumerable collection); 12 | 13 | IOrderedQueryable InvokeSort(IOrderedQueryable query); 14 | 15 | IOrderedEnumerable InvokeSort(IOrderedEnumerable collection); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/Data/SqliteConfiguration.cs: -------------------------------------------------------------------------------- 1 | using System.Data.Entity; 2 | using System.Data.Entity.Core.Common; 3 | using System.Data.SQLite; 4 | using System.Data.SQLite.EF6; 5 | 6 | namespace LinqBuilder.EF6.Tests.Data; 7 | 8 | public class SqliteConfiguration : DbConfiguration 9 | { 10 | public SqliteConfiguration() 11 | { 12 | SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance); 13 | SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance); 14 | SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices))); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/MultipleValueSpecification2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Tests.Data.Specifications; 5 | 6 | public class MultipleValueSpecification2 : DynamicSpecification 7 | { 8 | public MultipleValueSpecification2() 9 | { 10 | } 11 | 12 | public MultipleValueSpecification2(int value1, int value2) 13 | : base(value1, value2) 14 | { 15 | } 16 | 17 | public override Expression> AsExpression() 18 | { 19 | return entity => entity.Value1 == Value1 && 20 | entity.Value2 == Value2; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6/README.md: -------------------------------------------------------------------------------- 1 | # LinqBuilder.EF6 2 | 3 | **LinqBuilder.EF6** packages extends the following extensions to support ```ISpecification```. 4 | ```csharp 5 | bool result = await _sampleContext.Entities.AnyAsync(specification); 6 | bool result = await _sampleContext.Entities.AllAsync(specification); 7 | int result = await _sampleContext.Entities.CountAsync(specification); 8 | Entity result = await _sampleContext.Entities.FirstAsync(specification); 9 | Entity result = await _sampleContext.Entities.FirstOrDefaultAsync(specification); 10 | Entity result = await _sampleContext.Entities.SingleAsync(specification); 11 | Entity result = await _sampleContext.Entities.SingleOrDefaultAsync(specification); 12 | ``` 13 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore/README.md: -------------------------------------------------------------------------------- 1 | # LinqBuilder.EFCore 2 | 3 | **LinqBuilder.EFCore** packages extends the following extensions to support ```ISpecification```. 4 | ```csharp 5 | bool result = await _sampleContext.Entities.AnyAsync(specification); 6 | bool result = await _sampleContext.Entities.AllAsync(specification); 7 | int result = await _sampleContext.Entities.CountAsync(specification); 8 | Entity result = await _sampleContext.Entities.FirstAsync(specification); 9 | Entity result = await _sampleContext.Entities.FirstOrDefaultAsync(specification); 10 | Entity result = await _sampleContext.Entities.SingleAsync(specification); 11 | Entity result = await _sampleContext.Entities.SingleOrDefaultAsync(specification); 12 | ``` 13 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/MultiEntitySpecification3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Tests.Data.Specifications; 5 | 6 | public class MultiEntitySpecification3 : MultiSpecification 7 | { 8 | public override Expression> AsExpressionForEntity1() 9 | { 10 | return entity => entity.Value1 == 1; 11 | } 12 | 13 | public override Expression> AsExpressionForEntity2() 14 | { 15 | return entity => entity.Value2 == 2; 16 | } 17 | 18 | public override Expression> AsExpressionForEntity3() 19 | { 20 | return entity => entity.Value3 == 3; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/MultipleValueSpecification3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Tests.Data.Specifications; 5 | 6 | public class MultipleValueSpecification3 : DynamicSpecification 7 | { 8 | public MultipleValueSpecification3() 9 | { 10 | } 11 | 12 | public MultipleValueSpecification3(int value1, int value2, int value3) 13 | : base(value1, value2, value3) 14 | { 15 | } 16 | 17 | public override Expression> AsExpression() 18 | { 19 | return entity => entity.Value1 == Value1 && 20 | entity.Value2 == Value2 && 21 | entity.Value3 == Value3; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/LinqBuilder/DynamicSpecification{TEntity,TValue}.cs: -------------------------------------------------------------------------------- 1 | using LinqBuilder.Internal; 2 | 3 | namespace LinqBuilder 4 | { 5 | public abstract class DynamicSpecification 6 | : QuerySpecification, IDynamicSpecification 7 | where TEntity : class 8 | { 9 | protected DynamicSpecification() 10 | { 11 | } 12 | 13 | protected DynamicSpecification(TValue value) 14 | { 15 | Set(value); 16 | } 17 | 18 | public TValue Value { get; private set; } = default!; 19 | 20 | public IDynamicSpecification Set(TValue value) 21 | { 22 | Value = value; 23 | return this; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/EntityTheoryData.cs: -------------------------------------------------------------------------------- 1 | using Xunit; 2 | 3 | namespace LinqBuilder.Tests.Data; 4 | 5 | public abstract class EntityTheoryData : TheoryData 6 | { 7 | protected void AddEntity(int value1, int value2, bool expected) 8 | { 9 | var entity = new Entity 10 | { 11 | Value1 = value1, 12 | Value2 = value2, 13 | }; 14 | Add(entity, expected); 15 | } 16 | 17 | protected void AddEntity(int value1, int value2, int value3, int value4, bool expected) 18 | { 19 | var entity = new Entity 20 | { 21 | Value1 = value1, 22 | Value2 = value2, 23 | Value3 = value3, 24 | Value4 = value4, 25 | }; 26 | Add(entity, expected); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/MultipleValueSpecification4.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Tests.Data.Specifications; 5 | 6 | public class MultipleValueSpecification4 : DynamicSpecification 7 | { 8 | public MultipleValueSpecification4() 9 | { 10 | } 11 | 12 | public MultipleValueSpecification4(int value1, int value2, int value3, int value4) 13 | : base(value1, value2, value3, value4) 14 | { 15 | } 16 | 17 | public override Expression> AsExpression() 18 | { 19 | return entity => entity.Value1 == Value1 && 20 | entity.Value2 == Value2 && 21 | entity.Value3 == Value3 && 22 | entity.Value4 == Value4; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Specifications/MultiEntitySpecification4.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Tests.Data.Specifications; 5 | 6 | public class MultiEntitySpecification4 : MultiSpecification 7 | { 8 | public override Expression> AsExpressionForEntity1() 9 | { 10 | return entity => entity.Value1 == 1; 11 | } 12 | 13 | public override Expression> AsExpressionForEntity2() 14 | { 15 | return entity => entity.Value2 == 2; 16 | } 17 | 18 | public override Expression> AsExpressionForEntity3() 19 | { 20 | return entity => entity.Value3 == 3; 21 | } 22 | 23 | public override Expression> AsExpressionForEntity4() 24 | { 25 | return entity => entity.Value4 == 4; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/SpecTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using LinqBuilder.Tests.Data; 3 | using Xunit; 4 | 5 | namespace LinqBuilder.Tests; 6 | 7 | public class SpecTests 8 | { 9 | [Fact] 10 | public void Constructor_DefaultExpression_ShouldBeTrue() 11 | { 12 | new Spec() 13 | .IsSatisfiedBy(new Entity()) 14 | .Should().BeTrue(); 15 | } 16 | 17 | [Fact] 18 | public void Constructor_InlineExpression_ShouldBeTrue() 19 | { 20 | new Spec(entity => entity.Value1 == 1) 21 | .IsSatisfiedBy(new Entity { Value1 = 1 }) 22 | .Should().BeTrue(); 23 | } 24 | 25 | [Fact] 26 | public void Constructor_InlineExpression_ShouldBeFalse() 27 | { 28 | new Spec(entity => entity.Value1 == 2) 29 | .IsSatisfiedBy(new Entity { Value1 = 1 }) 30 | .Should().BeFalse(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/LinqBuilder/DynamicSpecification{TEntity,TValue1,TValue2}.cs: -------------------------------------------------------------------------------- 1 | using LinqBuilder.Internal; 2 | 3 | namespace LinqBuilder 4 | { 5 | public abstract class DynamicSpecification 6 | : QuerySpecification, IDynamicSpecification 7 | where TEntity : class 8 | { 9 | protected DynamicSpecification() 10 | { 11 | } 12 | 13 | protected DynamicSpecification(TValue1 value1, TValue2 value2) 14 | { 15 | Set(value1, value2); 16 | } 17 | 18 | public TValue1 Value1 { get; private set; } = default!; 19 | 20 | public TValue2 Value2 { get; private set; } = default!; 21 | 22 | public IDynamicSpecification Set(TValue1 value1, TValue2 value2) 23 | { 24 | Value1 = value1; 25 | Value2 = value2; 26 | return this; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Data/Fixture.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | 4 | namespace LinqBuilder.Tests.Data; 5 | 6 | public class Fixture 7 | { 8 | private readonly List _store = new(); 9 | 10 | public IReadOnlyList Store => _store; 11 | 12 | public IEnumerable Collection => _store.AsEnumerable(); 13 | 14 | public IQueryable Query => _store.AsQueryable(); 15 | 16 | public void AddToCollection(int value1, int value2, int? value3 = null, int? value4 = null) 17 | { 18 | var entity = new Entity 19 | { 20 | Value1 = value1, 21 | Value2 = value2, 22 | }; 23 | 24 | if (value3.HasValue) 25 | { 26 | entity.Value3 = value3.Value; 27 | } 28 | 29 | if (value4.HasValue) 30 | { 31 | entity.Value4 = value4.Value; 32 | } 33 | 34 | _store.Add(entity); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/LinqBuilder/DynamicSpecification{TEntity,TValue1,TValue2,TValue3}.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder 2 | { 3 | public abstract class DynamicSpecification 4 | : DynamicSpecification, IDynamicSpecification 5 | where TEntity : class 6 | { 7 | protected DynamicSpecification() 8 | { 9 | } 10 | 11 | protected DynamicSpecification(TValue1 value1, TValue2 value2, TValue3 value3) 12 | : base(value1, value2) 13 | { 14 | Value3 = value3; 15 | } 16 | 17 | public TValue3 Value3 { get; private set; } = default!; 18 | 19 | public IDynamicSpecification Set(TValue1 value1, TValue2 value2, TValue3 value3) 20 | { 21 | Set(value1, value2); 22 | Value3 = value3; 23 | return this; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/LinqBuilder/LinqBuilder.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0;net6.0;net8.0;net9.0 5 | Morten Larsen 6 | MIT 7 | LinqBuilder is an implementation of the specification pattern. 8 | Made for use with IEnumerable and IQueryable. 9 | git 10 | https://github.com/Baune8D/LinqBuilder 11 | https://github.com/Baune8D/LinqBuilder 12 | Specification, Linq 13 | README.md 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/LinqBuilder/MultiSpecification{TEntity1,TEntity2,TEntity3}.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder 5 | { 6 | public abstract class MultiSpecification 7 | : MultiSpecification 8 | where TEntity1 : class 9 | where TEntity2 : class 10 | where TEntity3 : class 11 | { 12 | // ReSharper disable once MemberCanBeProtected.Global 13 | public abstract Expression> AsExpressionForEntity3(); 14 | 15 | protected override Specification? Transform() 16 | { 17 | object? specification = base.Transform(); 18 | 19 | if (specification == null && typeof(TEntity) == typeof(TEntity3)) 20 | { 21 | specification = new Specification(AsExpressionForEntity3()); 22 | } 23 | 24 | return (Specification?)specification; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/LinqBuilder.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0;net8.0;net9.0 5 | false 6 | 7 | 8 | 9 | 10 | all 11 | runtime; build; native; contentfiles; analyzers; buildtransitive 12 | 13 | 14 | 15 | 16 | 17 | all 18 | runtime; build; native; contentfiles; analyzers; buildtransitive 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/LinqBuilder/DynamicSpecification{TEntity,TValue1,TValue2,TValue3,TValue4}.cs: -------------------------------------------------------------------------------- 1 | namespace LinqBuilder 2 | { 3 | public abstract class DynamicSpecification 4 | : DynamicSpecification, IDynamicSpecification 5 | where TEntity : class 6 | { 7 | protected DynamicSpecification() 8 | { 9 | } 10 | 11 | protected DynamicSpecification(TValue1 value1, TValue2 value2, TValue3 value3, TValue4 value4) 12 | : base(value1, value2, value3) 13 | { 14 | Value4 = value4; 15 | } 16 | 17 | public TValue4 Value4 { get; private set; } = default!; 18 | 19 | public IDynamicSpecification Set(TValue1 value1, TValue2 value2, TValue3 value3, TValue4 value4) 20 | { 21 | Set(value1, value2, value3); 22 | Value4 = value4; 23 | return this; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------------ 2 | # 3 | # 4 | # This code was generated. 5 | # 6 | # - To turn off auto-generation set: 7 | # 8 | # [AppVeyor (AutoGenerate = false)] 9 | # 10 | # - To trigger manual generation invoke: 11 | # 12 | # nuke --generate-configuration AppVeyor --host AppVeyor 13 | # 14 | # 15 | # ------------------------------------------------------------------------------ 16 | 17 | image: 18 | - Visual Studio 2022 19 | 20 | build_script: 21 | - cmd: .\build.cmd UploadCodecov PushNuGet PushMyGet 22 | - sh: ./build.cmd UploadCodecov PushNuGet PushMyGet 23 | 24 | artifacts: 25 | - path: 'artifacts/*.nupkg' 26 | 27 | environment: 28 | MYGET_API_KEY: 29 | secure: 78qy8e6pKfJlQV7RAG5tJOWegzXpjASkUs3aFdVBoPYA5gi6+mWdjbuAmNa5OQPe 30 | NUGET_API_KEY: 31 | secure: aMbj+EdePo74elFCi6lrQZcO81mru5j8cqD5FxGoDBWgXFFHwok/z4B+BtS4H1Sw 32 | CODECOV_TOKEN: 33 | secure: 3FxtGPNTgZyQGToJBaH68/oIjptV79CcViR9mHt2aOKGh3++oKTehBIuPSb7oYCE 34 | -------------------------------------------------------------------------------- /src/LinqBuilder/MultiSpecification{TEntity1,TEntity2,TEntity3,TEntity4}.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder 5 | { 6 | public abstract class MultiSpecification 7 | : MultiSpecification 8 | where TEntity1 : class 9 | where TEntity2 : class 10 | where TEntity3 : class 11 | where TEntity4 : class 12 | { 13 | // ReSharper disable once MemberCanBeProtected.Global 14 | public abstract Expression> AsExpressionForEntity4(); 15 | 16 | protected override Specification? Transform() 17 | { 18 | object? specification = base.Transform(); 19 | 20 | if (specification == null && typeof(TEntity) == typeof(TEntity4)) 21 | { 22 | specification = new Specification(AsExpressionForEntity4()); 23 | } 24 | 25 | return (Specification?)specification; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6/LinqBuilder.EF6.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.1 5 | Morten Larsen 6 | MIT 7 | Entity Framework 6 extensions. 8 | Enables most async methods to use ISpecification. 9 | git 10 | https://github.com/Baune8D/LinqBuilder 11 | https://github.com/Baune8D/LinqBuilder 12 | Specification, Linq, EntityFramework 13 | README.md 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore/LinqBuilder.EFCore.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0;net8.0;net9.0 5 | Morten Larsen 6 | MIT 7 | Entity Framework Core extensions. 8 | Enables most async methods to use ISpecification. 9 | git 10 | https://github.com/Baune8D/LinqBuilder 11 | https://github.com/Baune8D/LinqBuilder 12 | Specification, Linq, EntityFrameworkCore 13 | README.md 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Morten Larsen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /src/LinqBuilder/Internal/QuerySpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | 4 | namespace LinqBuilder.Internal 5 | { 6 | public abstract class QuerySpecification : IQuerySpecification 7 | where TEntity : class 8 | { 9 | private readonly Expression>? _expression; 10 | private Func? _func; 11 | 12 | protected QuerySpecification() 13 | { 14 | } 15 | 16 | protected QuerySpecification(Expression> expression) 17 | { 18 | _expression = expression; 19 | } 20 | 21 | public SpecificationBase Internal => new SpecificationBase(this); 22 | 23 | public virtual Expression>? AsExpression() 24 | { 25 | return _expression; 26 | } 27 | 28 | public Func? AsFunc() 29 | { 30 | var expression = AsExpression(); 31 | if (expression == null) 32 | { 33 | return null; 34 | } 35 | 36 | return _func ??= expression.Compile(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/Data/TestDbContext.cs: -------------------------------------------------------------------------------- 1 | using System.Data.Common; 2 | using System.Data.Entity; 3 | using SQLite.CodeFirst; 4 | 5 | namespace LinqBuilder.EF6.Tests.Data; 6 | 7 | public sealed class TestDbContext : DbContext 8 | { 9 | private static DbModelBuilder? _modelBuilder; 10 | 11 | public TestDbContext(DbConnection connection) 12 | : base(connection, true) 13 | { 14 | if (_modelBuilder == null) 15 | { 16 | return; 17 | } 18 | 19 | var model = _modelBuilder.Build(Database.Connection); 20 | var sqliteDatabaseCreator = new SqliteDatabaseCreator(); 21 | sqliteDatabaseCreator.Create(Database, model); 22 | } 23 | 24 | public DbSet Entities { get; set; } = null!; 25 | 26 | public DbSet ChildEntities { get; set; } = null!; 27 | 28 | protected override void OnModelCreating(DbModelBuilder modelBuilder) 29 | { 30 | var sqliteConnectionInitializer = new SqliteDropCreateDatabaseAlways(modelBuilder); 31 | Database.SetInitializer(sqliteConnectionInitializer); 32 | _modelBuilder = modelBuilder; 33 | base.OnModelCreating(modelBuilder); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LinqBuilder.sln.DotSettings: -------------------------------------------------------------------------------- 1 | 2 | True 3 | <data /> 4 | <data><IncludeFilters /><ExcludeFilters><Filter ModuleMask="LinqBuilder.*Tests" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /></ExcludeFilters></data> 5 | True 6 | True 7 | True 8 | True -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/LinqBuilder.EFCore.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0;net8.0;net9.0 5 | false 6 | 7 | 8 | 9 | 10 | all 11 | runtime; build; native; contentfiles; analyzers; buildtransitive 12 | 13 | 14 | 15 | 16 | 17 | 18 | all 19 | runtime; build; native; contentfiles; analyzers; buildtransitive 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | PreserveNewest 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/IntegrationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Entity; 3 | using System.Threading.Tasks; 4 | using FluentAssertions; 5 | using LinqBuilder.EF6.Tests.Data; 6 | using LinqBuilder.EF6.Tests.Data.Specifications; 7 | using LinqBuilder.OrderBy; 8 | using Xunit; 9 | 10 | namespace LinqBuilder.EF6.Tests; 11 | 12 | public sealed class IntegrationTests : IDisposable 13 | { 14 | private readonly TestDb _testDb; 15 | 16 | public IntegrationTests() 17 | { 18 | _testDb = new TestDb(); 19 | _testDb.AddEntity(2, 1, 2); 20 | _testDb.AddEntity(1, 2, 3); 21 | _testDb.AddEntity(3, 1, 1); 22 | _testDb.Context.SaveChanges(); 23 | } 24 | 25 | public void Dispose() 26 | { 27 | _testDb.Dispose(); 28 | } 29 | 30 | [Fact] 31 | public async Task ExeSpecAsync_ChildSpecification_ShouldReturnCorrectResult() 32 | { 33 | var specification = new ChildValueSpecification(1) 34 | .Or(new ChildValueSpecification(2)) 35 | .OrderBy(new OrderSpecification()); 36 | 37 | var result = await _testDb.Context.Entities 38 | .ExeSpec(specification) 39 | .ToListAsync(); 40 | 41 | result.Count.Should().Be(2); 42 | result[0].Id.Should().Be(1); 43 | result[1].Id.Should().Be(3); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/IntegrationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using FluentAssertions; 4 | using LinqBuilder.EFCore.Tests.Data; 5 | using LinqBuilder.EFCore.Tests.Data.Specifications; 6 | using LinqBuilder.OrderBy; 7 | using Microsoft.EntityFrameworkCore; 8 | using Xunit; 9 | 10 | namespace LinqBuilder.EFCore.Tests; 11 | 12 | public sealed class IntegrationTests : IDisposable 13 | { 14 | private readonly TestDb _testDb; 15 | 16 | public IntegrationTests() 17 | { 18 | _testDb = new TestDb(); 19 | _testDb.AddEntity(2, 1, 2); 20 | _testDb.AddEntity(1, 2, 3); 21 | _testDb.AddEntity(3, 1, 1); 22 | _testDb.Context.SaveChanges(); 23 | } 24 | 25 | public void Dispose() 26 | { 27 | _testDb.Dispose(); 28 | } 29 | 30 | [Fact] 31 | public async Task ExeSpecAsync_ChildSpecification_ShouldReturnCorrectResult() 32 | { 33 | var specification = new ChildValueSpecification(1) 34 | .Or(new ChildValueSpecification(2)) 35 | .OrderBy(new OrderSpecification()); 36 | 37 | var result = await _testDb.Context.Entities 38 | .ExeSpec(specification) 39 | .ToListAsync(); 40 | 41 | result.Count.Should().Be(2); 42 | result[0].Id.Should().Be(1); 43 | result[1].Id.Should().Be(3); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/OrderBy/OrderSpecTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using FluentAssertions; 3 | using LinqBuilder.OrderBy; 4 | using LinqBuilder.Tests.Data; 5 | using Xunit; 6 | 7 | namespace LinqBuilder.Tests.OrderBy; 8 | 9 | public class OrderSpecTests 10 | { 11 | private readonly Fixture _fixture; 12 | 13 | public OrderSpecTests() 14 | { 15 | _fixture = new Fixture(); 16 | _fixture.AddToCollection(3, 1, 1); 17 | _fixture.AddToCollection(1, 1, 1); 18 | _fixture.AddToCollection(2, 1, 1); 19 | } 20 | 21 | [Fact] 22 | public void Constructor_InlineExpression__ShouldReturnOrderedList() 23 | { 24 | var specification = new OrderSpec(entity => entity.Value1); 25 | var result = _fixture.Query.OrderBy(specification).ToList(); 26 | result[0].Value1.Should().Be(1); 27 | result[1].Value1.Should().Be(2); 28 | result[2].Value1.Should().Be(3); 29 | } 30 | 31 | [Fact] 32 | public void Constructor_InlineExpression_ShouldReturnOrderedList() 33 | { 34 | var specification = new OrderSpec(entity => entity.Value1, Sort.Descending); 35 | var result = _fixture.Query.OrderBy(specification).ToList(); 36 | result[0].Value1.Should().Be(3); 37 | result[1].Value1.Should().Be(2); 38 | result[2].Value1.Should().Be(1); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/LinqBuilder.EF6.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net9.0 5 | false 6 | 7 | 8 | 9 | 10 | all 11 | runtime; build; native; contentfiles; analyzers; buildtransitive 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | all 22 | runtime; build; native; contentfiles; analyzers; buildtransitive 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | PreserveNewest 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | enable 5 | AnyCPU 6 | true 7 | AllEnabledByDefault 8 | false 9 | false 10 | false 11 | 12 | 13 | 14 | 15 | 8 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | all 25 | runtime; build; native; contentfiles; analyzers; buildtransitive 26 | 27 | 28 | all 29 | runtime; build; native; contentfiles; analyzers; buildtransitive 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/LinqBuilder/Specification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Linq.Expressions; 4 | using LinqBuilder.Internal; 5 | 6 | namespace LinqBuilder 7 | { 8 | public class Specification : QuerySpecification 9 | where TEntity : class 10 | { 11 | public Specification() 12 | { 13 | } 14 | 15 | public Specification(Expression> expression) 16 | : base(expression) 17 | { 18 | } 19 | 20 | public static ISpecification New() 21 | { 22 | return new Specification(); 23 | } 24 | 25 | public static ISpecification New(Expression> expression) 26 | { 27 | return new Specification(expression); 28 | } 29 | 30 | public static ISpecification All(params ISpecification[] specifications) 31 | { 32 | return specifications.Aggregate(New(), (current, specification) => current.And(specification)); 33 | } 34 | 35 | public static ISpecification None(params ISpecification[] specifications) 36 | { 37 | return specifications.Aggregate(New(), (current, specification) => current.And(specification)).Not(); 38 | } 39 | 40 | public static ISpecification Any(params ISpecification[] specifications) 41 | { 42 | return specifications.Aggregate(New(), (current, specification) => current.Or(specification)); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/Data/TestDb.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data.SQLite; 4 | 5 | namespace LinqBuilder.EF6.Tests.Data; 6 | 7 | public sealed class TestDb : IDisposable 8 | { 9 | private readonly SQLiteConnection _connection; 10 | 11 | public TestDb() 12 | { 13 | _connection = CreateSqliteConnection(); 14 | _connection.Open(); 15 | Context = new TestDbContext(_connection); 16 | } 17 | 18 | public TestDbContext Context { get; } 19 | 20 | // Tear down in-memory database. 21 | public void Dispose() 22 | { 23 | Context.Dispose(); 24 | _connection.Dispose(); 25 | } 26 | 27 | public void AddEntity(int value1, int value2, int? childValue = null) 28 | { 29 | var entity = new SomeEntity 30 | { 31 | Value1 = value1, 32 | Value2 = value2, 33 | }; 34 | 35 | if (childValue.HasValue) 36 | { 37 | entity.ChildEntities = new List 38 | { 39 | new() 40 | { 41 | Value = childValue.Value, 42 | }, 43 | }; 44 | } 45 | 46 | Context.Entities.Add(entity); 47 | } 48 | 49 | private static SQLiteConnection CreateSqliteConnection() 50 | { 51 | var connectionStringBuilder = new SQLiteConnectionStringBuilder 52 | { 53 | DataSource = ":memory:", 54 | ForeignKeys = true, 55 | }; 56 | var connectionString = connectionStringBuilder.ConnectionString; 57 | return new SQLiteConnection(connectionString); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/LinqBuilder/MultiSpecification{TEntity1,TEntity2}.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using LinqBuilder.Internal; 4 | 5 | namespace LinqBuilder 6 | { 7 | public abstract class MultiSpecification 8 | : ISpecification 9 | where TEntity1 : class 10 | where TEntity2 : class 11 | { 12 | public SpecificationBase Internal => new SpecificationBase(For()); 13 | 14 | // ReSharper disable once MemberCanBeProtected.Global 15 | public abstract Expression> AsExpressionForEntity1(); 16 | 17 | // ReSharper disable once MemberCanBeProtected.Global 18 | public abstract Expression> AsExpressionForEntity2(); 19 | 20 | public Specification For() 21 | where TEntity : class 22 | { 23 | var result = Transform(); 24 | if (result == null) 25 | { 26 | throw new InvalidOperationException("Type is not defined in specification!"); 27 | } 28 | 29 | return result; 30 | } 31 | 32 | protected virtual Specification? Transform() 33 | where TEntity : class 34 | { 35 | object? specification = null; 36 | 37 | if (typeof(TEntity) == typeof(TEntity1)) 38 | { 39 | specification = new Specification(AsExpressionForEntity1()); 40 | } 41 | else if (typeof(TEntity) == typeof(TEntity2)) 42 | { 43 | specification = new Specification(AsExpressionForEntity2()); 44 | } 45 | 46 | return (Specification?)specification; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/LinqBuilder/OrderBy/LinqExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace LinqBuilder.OrderBy 6 | { 7 | public static class LinqExtensions 8 | { 9 | public static IOrderedQueryable OrderBy(this IQueryable query, IOrderSpecification orderSpecification) 10 | where TEntity : class 11 | { 12 | if (orderSpecification == null) 13 | { 14 | throw new ArgumentNullException(nameof(orderSpecification)); 15 | } 16 | 17 | return orderSpecification.InvokeSort(query); 18 | } 19 | 20 | public static IOrderedEnumerable OrderBy(this IEnumerable collection, IOrderSpecification orderSpecification) 21 | where TEntity : class 22 | { 23 | if (orderSpecification == null) 24 | { 25 | throw new ArgumentNullException(nameof(orderSpecification)); 26 | } 27 | 28 | return orderSpecification.InvokeSort(collection); 29 | } 30 | 31 | public static IOrderedQueryable ThenBy(this IOrderedQueryable query, IOrderSpecification orderSpecification) 32 | where TEntity : class 33 | { 34 | if (orderSpecification == null) 35 | { 36 | throw new ArgumentNullException(nameof(orderSpecification)); 37 | } 38 | 39 | return orderSpecification.InvokeSort(query); 40 | } 41 | 42 | public static IOrderedEnumerable ThenBy(this IOrderedEnumerable collection, IOrderSpecification orderSpecification) 43 | where TEntity : class 44 | { 45 | if (orderSpecification == null) 46 | { 47 | throw new ArgumentNullException(nameof(orderSpecification)); 48 | } 49 | 50 | return orderSpecification.InvokeSort(collection); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/LinqBuilder/Internal/SpecificationBase.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using LinqBuilder.OrderBy; 4 | 5 | namespace LinqBuilder.Internal 6 | { 7 | public sealed class SpecificationBase : IOrderedSpecification 8 | where TEntity : class 9 | { 10 | private readonly List> _orderSpecifications; 11 | 12 | public SpecificationBase(IQuerySpecification querySpecification) 13 | { 14 | _orderSpecifications = new List>(); 15 | QuerySpecification = querySpecification; 16 | } 17 | 18 | public SpecificationBase(IOrderSpecification orderSpecification) 19 | { 20 | _orderSpecifications = new List> { orderSpecification }; 21 | QuerySpecification = new DummySpecification(); 22 | } 23 | 24 | public SpecificationBase(IQuerySpecification querySpecification, IEnumerable> orderSpecifications) 25 | { 26 | _orderSpecifications = orderSpecifications.ToList(); 27 | QuerySpecification = querySpecification; 28 | } 29 | 30 | public IQuerySpecification QuerySpecification { get; set; } 31 | 32 | public int? Skip { get; set; } 33 | 34 | public int? Take { get; set; } 35 | 36 | public IReadOnlyList> OrderSpecifications => _orderSpecifications; 37 | 38 | public SpecificationBase Internal => this; 39 | 40 | public void AddOrderSpecifications(params IOrderSpecification[] specifications) 41 | { 42 | _orderSpecifications.AddRange(specifications); 43 | } 44 | 45 | public ISpecification Clone() 46 | { 47 | return new SpecificationBase(QuerySpecification, _orderSpecifications) 48 | { 49 | Skip = Skip, 50 | Take = Take, 51 | }; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/Data/TestDb.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Microsoft.Data.Sqlite; 4 | using Microsoft.EntityFrameworkCore; 5 | 6 | namespace LinqBuilder.EFCore.Tests.Data; 7 | 8 | public sealed class TestDb : IDisposable 9 | { 10 | private readonly SqliteConnection _connection; 11 | 12 | public TestDb() 13 | { 14 | // We need to hold the SQLite connection open to force the in-memory database not to reset. 15 | _connection = CreateSqliteConnection(); 16 | _connection.Open(); 17 | 18 | // Create the context and ensure the schema is created. 19 | Context = CreateContext(); 20 | Context.Database.EnsureCreated(); 21 | } 22 | 23 | public TestDbContext Context { get; } 24 | 25 | // Tear down in-memory database. 26 | public void Dispose() 27 | { 28 | Context.Dispose(); 29 | _connection.Dispose(); 30 | } 31 | 32 | public void AddEntity(int value1, int value2, int? childValue = null) 33 | { 34 | var entity = new SomeEntity 35 | { 36 | Value1 = value1, 37 | Value2 = value2, 38 | }; 39 | 40 | if (childValue.HasValue) 41 | { 42 | entity.ChildEntities = new List 43 | { 44 | new() 45 | { 46 | Value = childValue.Value, 47 | }, 48 | }; 49 | } 50 | 51 | Context.Entities.Add(entity); 52 | } 53 | 54 | private static SqliteConnection CreateSqliteConnection() 55 | { 56 | var connectionStringBuilder = new SqliteConnectionStringBuilder 57 | { 58 | DataSource = ":memory:", 59 | }; 60 | var connectionString = connectionStringBuilder.ToString(); 61 | return new SqliteConnection(connectionString); 62 | } 63 | 64 | private TestDbContext CreateContext() 65 | { 66 | var optionsBuilder = new DbContextOptionsBuilder(); 67 | optionsBuilder.UseSqlite(_connection); 68 | return new TestDbContext(optionsBuilder.Options); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/Internal/SpecificationBaseTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using FluentAssertions; 4 | using LinqBuilder.Internal; 5 | using LinqBuilder.OrderBy; 6 | using LinqBuilder.Tests.Data; 7 | using Xunit; 8 | 9 | namespace LinqBuilder.Tests.Internal; 10 | 11 | public class SpecificationBaseTests 12 | { 13 | [Fact] 14 | public void Configuration_NewOrderSpecification_ShouldBeCorrectlyConfigured() 15 | { 16 | var configuration = new SpecificationBase(new OrderSpecification(x => x.Value1)); 17 | configuration.QuerySpecification.Internal.Should().NotBeNull(); 18 | configuration.QuerySpecification.Should().NotBeNull(); 19 | configuration.QuerySpecification.AsExpression().Should().BeNull(); 20 | configuration.QuerySpecification.AsFunc().Should().BeNull(); 21 | configuration.OrderSpecifications.Count.Should().Be(1); 22 | } 23 | 24 | [Fact] 25 | public void Configuration_NewSpecification_ShouldBeCorrectlyConfigured() 26 | { 27 | var configuration = new SpecificationBase(new Specification()); 28 | configuration.QuerySpecification.Internal.Should().NotBeNull(); 29 | configuration.QuerySpecification.Should().NotBeNull(); 30 | configuration.QuerySpecification.AsExpression().Should().BeNull(); 31 | configuration.QuerySpecification.AsFunc().Should().BeNull(); 32 | configuration.OrderSpecifications.Any().Should().BeFalse(); 33 | } 34 | 35 | [Fact] 36 | public void Configuration_NewOrderSpecificationAndNewSpecification_ShouldBeCorrectlyConfigured() 37 | { 38 | var configuration = new SpecificationBase(new Specification(), new List> 39 | { 40 | new OrderSpecification(x => x.Value1), 41 | }); 42 | configuration.QuerySpecification.Internal.Should().NotBeNull(); 43 | configuration.QuerySpecification.Should().NotBeNull(); 44 | configuration.QuerySpecification.AsExpression().Should().BeNull(); 45 | configuration.QuerySpecification.AsFunc().Should().BeNull(); 46 | configuration.OrderSpecifications.Count.Should().Be(1); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Directory.Packages.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | true 4 | 5 | 6 | 7 | all 8 | runtime; build; native; contentfiles; analyzers; buildtransitive 9 | 10 | 11 | all 12 | runtime; build; native; contentfiles; analyzers; buildtransitive 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | all 21 | runtime; build; native; contentfiles; analyzers; buildtransitive 22 | 23 | 24 | 25 | 26 | 27 | all 28 | runtime; build; native; contentfiles; analyzers; buildtransitive 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text eol=lf 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/OrderBy/LinqExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using FluentAssertions; 3 | using LinqBuilder.OrderBy; 4 | using LinqBuilder.Tests.Data; 5 | using Xunit; 6 | 7 | namespace LinqBuilder.Tests.OrderBy; 8 | 9 | public class LinqExtensionsTests 10 | { 11 | private readonly IOrderSpecification _orderValue1Asc = OrderSpec.New(entity => entity.Value1); 12 | private readonly IOrderSpecification _orderValue2Asc = OrderSpec.New(entity => entity.Value2); 13 | 14 | private readonly Fixture _fixture; 15 | 16 | public LinqExtensionsTests() 17 | { 18 | _fixture = new Fixture(); 19 | _fixture.AddToCollection(3, 1, 1); 20 | _fixture.AddToCollection(1, 1, 1); 21 | _fixture.AddToCollection(2, 2, 1); 22 | _fixture.AddToCollection(2, 1, 1); 23 | } 24 | 25 | [Fact] 26 | public void OrderBy_IQueryable_ShouldReturnCorrectResult() 27 | { 28 | var result = _fixture.Query 29 | .OrderBy(_orderValue1Asc) 30 | .ToList(); 31 | 32 | result.Count.Should().Be(4); 33 | result[0].Value1.Should().Be(1); 34 | result[1].Value1.Should().Be(2); 35 | result[1].Value2.Should().Be(2); 36 | result[2].Value1.Should().Be(2); 37 | result[2].Value2.Should().Be(1); 38 | result[3].Value1.Should().Be(3); 39 | } 40 | 41 | [Fact] 42 | public void OrderBy_IEnumerable_ShouldReturnCorrectResult() 43 | { 44 | var result = _fixture.Collection 45 | .OrderBy(_orderValue1Asc) 46 | .ToList(); 47 | 48 | result.Count.Should().Be(4); 49 | result[0].Value1.Should().Be(1); 50 | result[1].Value1.Should().Be(2); 51 | result[1].Value2.Should().Be(2); 52 | result[2].Value1.Should().Be(2); 53 | result[2].Value2.Should().Be(1); 54 | result[3].Value1.Should().Be(3); 55 | } 56 | 57 | [Fact] 58 | public void ThenBy_IQueryable_ShouldReturnCorrectResult() 59 | { 60 | var result = _fixture.Query 61 | .OrderBy(_orderValue1Asc) 62 | .ThenBy(_orderValue2Asc) 63 | .ToList(); 64 | 65 | result.Count.Should().Be(4); 66 | result[0].Value1.Should().Be(1); 67 | result[1].Value1.Should().Be(2); 68 | result[1].Value2.Should().Be(1); 69 | result[2].Value1.Should().Be(2); 70 | result[2].Value2.Should().Be(2); 71 | result[3].Value1.Should().Be(3); 72 | } 73 | 74 | [Fact] 75 | public void ThenBy_IEnumerable_ShouldReturnCorrectResult() 76 | { 77 | var result = _fixture.Collection 78 | .OrderBy(_orderValue1Asc) 79 | .ThenBy(_orderValue2Asc) 80 | .ToList(); 81 | 82 | result.Count.Should().Be(4); 83 | result[0].Value1.Should().Be(1); 84 | result[1].Value1.Should().Be(2); 85 | result[1].Value2.Should().Be(1); 86 | result[2].Value1.Should().Be(2); 87 | result[2].Value2.Should().Be(2); 88 | result[3].Value1.Should().Be(3); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/DynamicSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using LinqBuilder.Tests.Data; 3 | using LinqBuilder.Tests.Data.Specifications; 4 | using Xunit; 5 | 6 | namespace LinqBuilder.Tests; 7 | 8 | public class DynamicSpecificationTests 9 | { 10 | [Theory] 11 | [ClassData(typeof(TestData))] 12 | public void IsSatisfiedBy_TheoryData(Entity entity, bool expected) 13 | { 14 | new MultipleValueSpecification4(3, 5, 4, 2) 15 | .IsSatisfiedBy(entity) 16 | .Should().Be(expected); 17 | } 18 | 19 | [Fact] 20 | public void Constructor_MultipleValueSpecification1_ShouldHaveCorrectValue() 21 | { 22 | var specification = new MultipleValueSpecification1(1); 23 | specification.Value.Should().Be(1); 24 | } 25 | 26 | [Fact] 27 | public void Constructor_MultipleValueSpecification2_ShouldHaveCorrectValues() 28 | { 29 | var specification = new MultipleValueSpecification2(1, 2); 30 | specification.Value1.Should().Be(1); 31 | specification.Value2.Should().Be(2); 32 | } 33 | 34 | [Fact] 35 | public void Constructor_MultipleValueSpecification3_ShouldHaveCorrectValues() 36 | { 37 | var specification = new MultipleValueSpecification3(1, 2, 3); 38 | specification.Value1.Should().Be(1); 39 | specification.Value2.Should().Be(2); 40 | specification.Value3.Should().Be(3); 41 | } 42 | 43 | [Fact] 44 | public void Constructor_MultipleValueSpecification4_ShouldHaveCorrectValues() 45 | { 46 | var specification = new MultipleValueSpecification4(1, 2, 3, 4); 47 | specification.Value1.Should().Be(1); 48 | specification.Value2.Should().Be(2); 49 | specification.Value3.Should().Be(3); 50 | specification.Value4.Should().Be(4); 51 | } 52 | 53 | [Fact] 54 | public void Set_MultipleValueSpecification1_ShouldHaveCorrectValue() 55 | { 56 | var specification = new MultipleValueSpecification1().Set(1); 57 | specification.Value.Should().Be(1); 58 | } 59 | 60 | [Fact] 61 | public void Set_MultipleValueSpecification2_ShouldHaveCorrectValues() 62 | { 63 | var specification = new MultipleValueSpecification2().Set(1, 2); 64 | specification.Value1.Should().Be(1); 65 | specification.Value2.Should().Be(2); 66 | } 67 | 68 | [Fact] 69 | public void Set_MultipleValueSpecification3_ShouldHaveCorrectValues() 70 | { 71 | var specification = new MultipleValueSpecification3().Set(1, 2, 3); 72 | specification.Value1.Should().Be(1); 73 | specification.Value2.Should().Be(2); 74 | specification.Value3.Should().Be(3); 75 | } 76 | 77 | [Fact] 78 | public void Set_MultipleValueSpecification4_ShouldHaveCorrectValues() 79 | { 80 | var specification = new MultipleValueSpecification4().Set(1, 2, 3, 4); 81 | specification.Value1.Should().Be(1); 82 | specification.Value2.Should().Be(2); 83 | specification.Value3.Should().Be(3); 84 | specification.Value4.Should().Be(4); 85 | } 86 | 87 | private sealed class TestData : EntityTheoryData 88 | { 89 | public TestData() 90 | { 91 | AddEntity(3, 5, 4, 2, true); 92 | AddEntity(3, 4, 4, 2, false); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/SpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using LinqBuilder.Tests.Data; 3 | using Xunit; 4 | 5 | namespace LinqBuilder.Tests; 6 | 7 | public class SpecificationTests 8 | { 9 | private readonly ISpecification _emptySpecification = Spec.New(); 10 | private readonly ISpecification _value1ShouldBe1 = Spec.New(entity => entity.Value1 == 1); 11 | private readonly ISpecification _value2ShouldBe2 = Spec.New(entity => entity.Value2 == 2); 12 | 13 | [Fact] 14 | public void Constructor_DefaultExpression_ShouldBeTrue() 15 | { 16 | _emptySpecification 17 | .IsSatisfiedBy(new Entity()) 18 | .Should().BeTrue(); 19 | } 20 | 21 | [Fact] 22 | public void Constructor_InlineExpression_ShouldBeTrue() 23 | { 24 | _value1ShouldBe1 25 | .IsSatisfiedBy(new Entity { Value1 = 1 }) 26 | .Should().BeTrue(); 27 | } 28 | 29 | [Fact] 30 | public void Constructor_InlineExpression_ShouldBeFalse() 31 | { 32 | _value1ShouldBe1 33 | .IsSatisfiedBy(new Entity { Value1 = 2 }) 34 | .Should().BeFalse(); 35 | } 36 | 37 | [Fact] 38 | public void New_DefaultExpression_ShouldBeTrue() 39 | { 40 | _emptySpecification 41 | .IsSatisfiedBy(new Entity()) 42 | .Should().BeTrue(); 43 | } 44 | 45 | [Fact] 46 | public void New_InlineExpression_ShouldBeTrue() 47 | { 48 | _value1ShouldBe1 49 | .IsSatisfiedBy(new Entity { Value1 = 1 }) 50 | .Should().BeTrue(); 51 | } 52 | 53 | [Fact] 54 | public void New_InlineExpression_ShouldBeFalse() 55 | { 56 | _value1ShouldBe1 57 | .IsSatisfiedBy(new Entity { Value1 = 2 }) 58 | .Should().BeFalse(); 59 | } 60 | 61 | [Fact] 62 | public void All_Specifications_ShouldBeTrue() 63 | { 64 | Specification.All(_value1ShouldBe1, _value2ShouldBe2) 65 | .IsSatisfiedBy(new Entity { Value1 = 1, Value2 = 2 }); 66 | } 67 | 68 | [Fact] 69 | public void All_Specifications_ShouldBeFalse() 70 | { 71 | Specification.All(_value1ShouldBe1, _value2ShouldBe2) 72 | .IsSatisfiedBy(new Entity { Value1 = 1, Value2 = 1 }); 73 | } 74 | 75 | [Fact] 76 | public void None_Specifications_ShouldBeTrue() 77 | { 78 | Specification.None(_value1ShouldBe1, _value2ShouldBe2) 79 | .IsSatisfiedBy(new Entity { Value1 = 2, Value2 = 1 }); 80 | } 81 | 82 | [Fact] 83 | public void None_Specifications_ShouldBeFalse() 84 | { 85 | Specification.None(_value1ShouldBe1, _value2ShouldBe2) 86 | .IsSatisfiedBy(new Entity { Value1 = 1, Value2 = 2 }); 87 | } 88 | 89 | [Fact] 90 | public void Any_Specifications_ShouldBeTrue() 91 | { 92 | Specification.Any(_value1ShouldBe1, _value2ShouldBe2) 93 | .IsSatisfiedBy(new Entity { Value1 = 1, Value2 = 1 }); 94 | } 95 | 96 | [Fact] 97 | public void Any_Specifications_ShouldBeFalse() 98 | { 99 | Specification.Any(_value1ShouldBe1, _value2ShouldBe2) 100 | .IsSatisfiedBy(new Entity { Value1 = 2, Value2 = 1 }); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/OrderBy/OrderSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using FluentAssertions; 3 | using LinqBuilder.OrderBy; 4 | using LinqBuilder.Tests.Data; 5 | using Xunit; 6 | 7 | namespace LinqBuilder.Tests.OrderBy; 8 | 9 | public class OrderSpecificationTests 10 | { 11 | private readonly IOrderSpecification _orderValue1Asc = OrderSpec.New(entity => entity.Value1); 12 | private readonly IOrderSpecification _orderValue1Desc = OrderSpec.New(entity => entity.Value1, Sort.Descending); 13 | 14 | private readonly Fixture _fixture; 15 | 16 | public OrderSpecificationTests() 17 | { 18 | _fixture = new Fixture(); 19 | _fixture.AddToCollection(3, 1, 1); 20 | _fixture.AddToCollection(1, 1, 1); 21 | _fixture.AddToCollection(2, 1, 1); 22 | } 23 | 24 | [Fact] 25 | public void Constructor_InlineExpression__ShouldReturnOrderedList() 26 | { 27 | var result = _fixture.Query.OrderBy(_orderValue1Asc).ToList(); 28 | result[0].Value1.Should().Be(1); 29 | result[1].Value1.Should().Be(2); 30 | result[2].Value1.Should().Be(3); 31 | } 32 | 33 | [Fact] 34 | public void Constructor_InlineExpression_ShouldReturnOrderedList() 35 | { 36 | var result = _fixture.Query.OrderBy(_orderValue1Desc).ToList(); 37 | result[0].Value1.Should().Be(3); 38 | result[1].Value1.Should().Be(2); 39 | result[2].Value1.Should().Be(1); 40 | } 41 | 42 | [Fact] 43 | public void New_InlineExpression__ShouldReturnOrderedList() 44 | { 45 | var result = _fixture.Query.OrderBy(_orderValue1Asc).ToList(); 46 | result[0].Value1.Should().Be(1); 47 | result[1].Value1.Should().Be(2); 48 | result[2].Value1.Should().Be(3); 49 | } 50 | 51 | [Fact] 52 | public void New_InlineExpression_ShouldReturnOrderedList() 53 | { 54 | var result = _fixture.Query.OrderBy(_orderValue1Desc).ToList(); 55 | result[0].Value1.Should().Be(3); 56 | result[1].Value1.Should().Be(2); 57 | result[2].Value1.Should().Be(1); 58 | } 59 | 60 | [Fact] 61 | public void InvokeSort_IQueryableAscending_ShouldReturnOrderedList() 62 | { 63 | var result = _orderValue1Asc.InvokeSort(_fixture.Query).ToList(); 64 | result[0].Value1.Should().Be(1); 65 | result[1].Value1.Should().Be(2); 66 | result[2].Value1.Should().Be(3); 67 | } 68 | 69 | [Fact] 70 | public void InvokeSort_IEnumerableAscending_ShouldReturnOrderedList() 71 | { 72 | var result = _orderValue1Asc.InvokeSort(_fixture.Collection).ToList(); 73 | result[0].Value1.Should().Be(1); 74 | result[1].Value1.Should().Be(2); 75 | result[2].Value1.Should().Be(3); 76 | } 77 | 78 | [Fact] 79 | public void InvokeSort_IQueryableDescending_ShouldReturnOrderedList() 80 | { 81 | var result = _orderValue1Desc.InvokeSort(_fixture.Query).ToList(); 82 | result[0].Value1.Should().Be(3); 83 | result[1].Value1.Should().Be(2); 84 | result[2].Value1.Should().Be(1); 85 | } 86 | 87 | [Fact] 88 | public void InvokeSort_IEnumerableDescending_ShouldReturnOrderedList() 89 | { 90 | var result = _orderValue1Desc.InvokeSort(_fixture.Collection).ToList(); 91 | result[0].Value1.Should().Be(3); 92 | result[1].Value1.Should().Be(2); 93 | result[2].Value1.Should().Be(1); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/LinqBuilder/OrderBy/OrderSpecification.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Linq.Expressions; 5 | using LinqBuilder.Internal; 6 | 7 | namespace LinqBuilder.OrderBy 8 | { 9 | public class OrderSpecification : IOrderSpecification 10 | where TEntity : class 11 | { 12 | private readonly Sort _sort; 13 | private readonly Expression>? _expression; 14 | private Func? _func; 15 | 16 | public OrderSpecification(Expression> expression, Sort sort = Sort.Ascending) 17 | : this(sort) 18 | { 19 | _expression = expression; 20 | } 21 | 22 | protected OrderSpecification(Sort sort = Sort.Ascending) 23 | { 24 | _sort = sort; 25 | } 26 | 27 | public SpecificationBase Internal => new SpecificationBase(this); 28 | 29 | public static IOrderSpecification New(Expression> expression, Sort sort = Sort.Ascending) 30 | { 31 | return new OrderSpecification(expression, sort); 32 | } 33 | 34 | // ReSharper disable once MemberCanBeProtected.Global 35 | public virtual Expression>? AsExpression() 36 | { 37 | return _expression; 38 | } 39 | 40 | // ReSharper disable once MemberCanBePrivate.Global 41 | public Func? AsFunc() 42 | { 43 | var expression = AsExpression(); 44 | if (expression == null) 45 | { 46 | return null; 47 | } 48 | 49 | return _func ??= expression.Compile(); 50 | } 51 | 52 | public IOrderedQueryable InvokeSort(IQueryable query) 53 | { 54 | var expression = AsExpression(); 55 | if (expression == null) 56 | { 57 | throw new InvalidOperationException("AsExpression returns null"); 58 | } 59 | 60 | return _sort == Sort.Descending 61 | ? query.OrderByDescending(expression) 62 | : query.OrderBy(expression); 63 | } 64 | 65 | public IOrderedEnumerable InvokeSort(IEnumerable collection) 66 | { 67 | var func = AsFunc(); 68 | if (func == null) 69 | { 70 | throw new InvalidOperationException("AsExpression returns null"); 71 | } 72 | 73 | return _sort == Sort.Descending 74 | ? collection.OrderByDescending(func) 75 | : collection.OrderBy(func); 76 | } 77 | 78 | public IOrderedQueryable InvokeSort(IOrderedQueryable query) 79 | { 80 | var expression = AsExpression(); 81 | if (expression == null) 82 | { 83 | throw new InvalidOperationException("AsExpression returns null"); 84 | } 85 | 86 | return _sort == Sort.Descending 87 | ? query.ThenByDescending(expression) 88 | : query.ThenBy(expression); 89 | } 90 | 91 | public IOrderedEnumerable InvokeSort(IOrderedEnumerable collection) 92 | { 93 | var func = AsFunc(); 94 | if (func == null) 95 | { 96 | throw new InvalidOperationException("AsExpression returns null"); 97 | } 98 | 99 | return _sort == Sort.Descending 100 | ? collection.ThenByDescending(func) 101 | : collection.ThenBy(func); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore/EntityFrameworkCoreExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using Microsoft.EntityFrameworkCore; 5 | 6 | namespace LinqBuilder.EFCore; 7 | 8 | public static class EntityFrameworkCoreExtensions 9 | { 10 | public static async Task AnyAsync(this IQueryable query, ISpecification specification) 11 | where TEntity : class 12 | { 13 | if (specification == null) 14 | { 15 | throw new ArgumentNullException(nameof(specification)); 16 | } 17 | 18 | var expression = specification.Internal.QuerySpecification.AsExpression(); 19 | return await (expression == null ? query.AnyAsync() : query.AnyAsync(expression)).ConfigureAwait(false); 20 | } 21 | 22 | public static async Task AllAsync(this IQueryable query, ISpecification specification) 23 | where TEntity : class 24 | { 25 | if (specification == null) 26 | { 27 | throw new ArgumentNullException(nameof(specification)); 28 | } 29 | 30 | var expression = specification.Internal.QuerySpecification.AsExpression(); 31 | return expression == null || await query.AllAsync(expression).ConfigureAwait(false); 32 | } 33 | 34 | public static async Task CountAsync(this IQueryable query, ISpecification specification) 35 | where TEntity : class 36 | { 37 | if (specification == null) 38 | { 39 | throw new ArgumentNullException(nameof(specification)); 40 | } 41 | 42 | var expression = specification.Internal.QuerySpecification.AsExpression(); 43 | return await (expression == null ? query.CountAsync() : query.CountAsync(expression)).ConfigureAwait(false); 44 | } 45 | 46 | public static async Task FirstAsync(this IQueryable query, ISpecification specification) 47 | where TEntity : class 48 | { 49 | if (specification == null) 50 | { 51 | throw new ArgumentNullException(nameof(specification)); 52 | } 53 | 54 | var expression = specification.Internal.QuerySpecification.AsExpression(); 55 | return await (expression == null ? query.FirstAsync() : query.FirstAsync(expression)).ConfigureAwait(false); 56 | } 57 | 58 | public static async Task FirstOrDefaultAsync(this IQueryable query, ISpecification specification) 59 | where TEntity : class 60 | { 61 | if (specification == null) 62 | { 63 | throw new ArgumentNullException(nameof(specification)); 64 | } 65 | 66 | var expression = specification.Internal.QuerySpecification.AsExpression(); 67 | return await (expression == null ? query.FirstOrDefaultAsync() : query.FirstOrDefaultAsync(expression)).ConfigureAwait(false); 68 | } 69 | 70 | public static async Task SingleAsync(this IQueryable query, ISpecification specification) 71 | where TEntity : class 72 | { 73 | if (specification == null) 74 | { 75 | throw new ArgumentNullException(nameof(specification)); 76 | } 77 | 78 | var expression = specification.Internal.QuerySpecification.AsExpression(); 79 | return await (expression == null ? query.SingleAsync() : query.SingleAsync(expression)).ConfigureAwait(false); 80 | } 81 | 82 | public static async Task SingleOrDefaultAsync(this IQueryable query, ISpecification specification) 83 | where TEntity : class 84 | { 85 | if (specification == null) 86 | { 87 | throw new ArgumentNullException(nameof(specification)); 88 | } 89 | 90 | var expression = specification.Internal.QuerySpecification.AsExpression(); 91 | return await (expression == null ? query.SingleOrDefaultAsync() : query.SingleOrDefaultAsync(expression)).ConfigureAwait(false); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/MultiSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentAssertions; 3 | using LinqBuilder.Tests.Data; 4 | using LinqBuilder.Tests.Data.Specifications; 5 | using Xunit; 6 | 7 | namespace LinqBuilder.Tests; 8 | 9 | public class MultiSpecificationTests 10 | { 11 | [Fact] 12 | public void IsSatisfiedBy_MultiEntitySpecification2_Default() 13 | { 14 | new MultiEntitySpecification2() 15 | .IsSatisfiedBy(new Entity { Value1 = 1 }); 16 | } 17 | 18 | [Fact] 19 | public void IsSatisfiedBy_MultiEntitySpecification2_Entity() 20 | { 21 | new MultiEntitySpecification2() 22 | .For() 23 | .IsSatisfiedBy(new Entity { Value1 = 1 }); 24 | } 25 | 26 | [Fact] 27 | public void IsSatisfiedBy_MultiEntitySpecification2_Entity2() 28 | { 29 | new MultiEntitySpecification2() 30 | .For() 31 | .IsSatisfiedBy(new Entity2 { Value2 = 2 }); 32 | } 33 | 34 | [Fact] 35 | public void IsSatisfiedBy_MultiEntitySpecification2_ShouldThrowException() 36 | { 37 | Action act = () => new MultiEntitySpecification2() 38 | .For() 39 | .IsSatisfiedBy(new Entity3()); 40 | 41 | act.Should().Throw(); 42 | } 43 | 44 | [Fact] 45 | public void IsSatisfiedBy_MultiEntitySpecification3_Default() 46 | { 47 | new MultiEntitySpecification3() 48 | .IsSatisfiedBy(new Entity { Value1 = 1 }); 49 | } 50 | 51 | [Fact] 52 | public void IsSatisfiedBy_MultiEntitySpecification3_Entity() 53 | { 54 | new MultiEntitySpecification3() 55 | .For() 56 | .IsSatisfiedBy(new Entity { Value1 = 1 }); 57 | } 58 | 59 | [Fact] 60 | public void IsSatisfiedBy_MultiEntitySpecification3_Entity2() 61 | { 62 | new MultiEntitySpecification3() 63 | .For() 64 | .IsSatisfiedBy(new Entity2 { Value2 = 2 }); 65 | } 66 | 67 | [Fact] 68 | public void IsSatisfiedBy_MultiEntitySpecification3_Entity3() 69 | { 70 | new MultiEntitySpecification3() 71 | .For() 72 | .IsSatisfiedBy(new Entity3 { Value3 = 3 }); 73 | } 74 | 75 | [Fact] 76 | public void IsSatisfiedBy_MultiEntitySpecification3_ShouldThrowException() 77 | { 78 | Action act = () => new MultiEntitySpecification3() 79 | .For() 80 | .IsSatisfiedBy(new Entity4()); 81 | 82 | act.Should().Throw(); 83 | } 84 | 85 | [Fact] 86 | public void IsSatisfiedBy_MultiEntitySpecification4_Default() 87 | { 88 | new MultiEntitySpecification4() 89 | .IsSatisfiedBy(new Entity { Value1 = 1 }); 90 | } 91 | 92 | [Fact] 93 | public void IsSatisfiedBy_MultiEntitySpecification4_Entity() 94 | { 95 | new MultiEntitySpecification4() 96 | .For() 97 | .IsSatisfiedBy(new Entity { Value1 = 1 }); 98 | } 99 | 100 | [Fact] 101 | public void IsSatisfiedBy_MultiEntitySpecification4_Entity2() 102 | { 103 | new MultiEntitySpecification4() 104 | .For() 105 | .IsSatisfiedBy(new Entity2 { Value2 = 2 }); 106 | } 107 | 108 | [Fact] 109 | public void IsSatisfiedBy_MultiEntitySpecification4_Entity3() 110 | { 111 | new MultiEntitySpecification4() 112 | .For() 113 | .IsSatisfiedBy(new Entity3 { Value3 = 3 }); 114 | } 115 | 116 | [Fact] 117 | public void IsSatisfiedBy_MultiEntitySpecification4_Entity4() 118 | { 119 | new MultiEntitySpecification4() 120 | .For() 121 | .IsSatisfiedBy(new Entity4 { Value4 = 4 }); 122 | } 123 | 124 | [Fact] 125 | public void IsSatisfiedBy_MultiEntitySpecification4_ShouldThrowException() 126 | { 127 | Action act = () => new MultiEntitySpecification4() 128 | .For() 129 | .IsSatisfiedBy(new Entity5()); 130 | 131 | act.Should().Throw(); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /.nuke/build.schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/draft-04/schema#", 3 | "definitions": { 4 | "Host": { 5 | "type": "string", 6 | "enum": [ 7 | "AppVeyor", 8 | "AzurePipelines", 9 | "Bamboo", 10 | "Bitbucket", 11 | "Bitrise", 12 | "GitHubActions", 13 | "GitLab", 14 | "Jenkins", 15 | "Rider", 16 | "SpaceAutomation", 17 | "TeamCity", 18 | "Terminal", 19 | "TravisCI", 20 | "VisualStudio", 21 | "VSCode" 22 | ] 23 | }, 24 | "ExecutableTarget": { 25 | "type": "string", 26 | "enum": [ 27 | "Clean", 28 | "Compile", 29 | "Package", 30 | "PushMyGet", 31 | "PushNuGet", 32 | "Test", 33 | "UploadCodecov" 34 | ] 35 | }, 36 | "Verbosity": { 37 | "type": "string", 38 | "description": "", 39 | "enum": [ 40 | "Verbose", 41 | "Normal", 42 | "Minimal", 43 | "Quiet" 44 | ] 45 | }, 46 | "NukeBuild": { 47 | "properties": { 48 | "Continue": { 49 | "type": "boolean", 50 | "description": "Indicates to continue a previously failed build attempt" 51 | }, 52 | "Help": { 53 | "type": "boolean", 54 | "description": "Shows the help text for this build assembly" 55 | }, 56 | "Host": { 57 | "description": "Host for execution. Default is 'automatic'", 58 | "$ref": "#/definitions/Host" 59 | }, 60 | "NoLogo": { 61 | "type": "boolean", 62 | "description": "Disables displaying the NUKE logo" 63 | }, 64 | "Partition": { 65 | "type": "string", 66 | "description": "Partition to use on CI" 67 | }, 68 | "Plan": { 69 | "type": "boolean", 70 | "description": "Shows the execution plan (HTML)" 71 | }, 72 | "Profile": { 73 | "type": "array", 74 | "description": "Defines the profiles to load", 75 | "items": { 76 | "type": "string" 77 | } 78 | }, 79 | "Root": { 80 | "type": "string", 81 | "description": "Root directory during build execution" 82 | }, 83 | "Skip": { 84 | "type": "array", 85 | "description": "List of targets to be skipped. Empty list skips all dependencies", 86 | "items": { 87 | "$ref": "#/definitions/ExecutableTarget" 88 | } 89 | }, 90 | "Target": { 91 | "type": "array", 92 | "description": "List of targets to be invoked. Default is '{default_target}'", 93 | "items": { 94 | "$ref": "#/definitions/ExecutableTarget" 95 | } 96 | }, 97 | "Verbosity": { 98 | "description": "Logging verbosity during build execution. Default is 'Normal'", 99 | "$ref": "#/definitions/Verbosity" 100 | } 101 | } 102 | } 103 | }, 104 | "allOf": [ 105 | { 106 | "properties": { 107 | "CODECOV_TOKEN": { 108 | "type": "string", 109 | "default": "Secrets must be entered via 'nuke :secrets [profile]'" 110 | }, 111 | "Configuration": { 112 | "type": "string", 113 | "description": "Configuration to build - Default is 'Debug' (local) or 'Release' (server)", 114 | "enum": [ 115 | "Debug", 116 | "Release" 117 | ] 118 | }, 119 | "MYGET_API_KEY": { 120 | "type": "string", 121 | "default": "Secrets must be entered via 'nuke :secrets [profile]'" 122 | }, 123 | "NUGET_API_KEY": { 124 | "type": "string", 125 | "default": "Secrets must be entered via 'nuke :secrets [profile]'" 126 | }, 127 | "Solution": { 128 | "type": "string", 129 | "description": "Path to a solution file that is automatically loaded" 130 | } 131 | } 132 | }, 133 | { 134 | "$ref": "#/definitions/NukeBuild" 135 | } 136 | ] 137 | } 138 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6/EntityFrameworkExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Entity; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace LinqBuilder.EF6 7 | { 8 | public static class EntityFrameworkExtensions 9 | { 10 | public static async Task AnyAsync(this IQueryable query, ISpecification specification) 11 | where TEntity : class 12 | { 13 | if (specification == null) 14 | { 15 | throw new ArgumentNullException(nameof(specification)); 16 | } 17 | 18 | var expression = specification.Internal.QuerySpecification.AsExpression(); 19 | return await (expression == null ? query.AnyAsync() : query.AnyAsync(expression)).ConfigureAwait(false); 20 | } 21 | 22 | public static async Task AllAsync(this IQueryable query, ISpecification specification) 23 | where TEntity : class 24 | { 25 | if (specification == null) 26 | { 27 | throw new ArgumentNullException(nameof(specification)); 28 | } 29 | 30 | var expression = specification.Internal.QuerySpecification.AsExpression(); 31 | return expression == null || await query.AllAsync(expression).ConfigureAwait(false); 32 | } 33 | 34 | public static async Task CountAsync(this IQueryable query, ISpecification specification) 35 | where TEntity : class 36 | { 37 | if (specification == null) 38 | { 39 | throw new ArgumentNullException(nameof(specification)); 40 | } 41 | 42 | var expression = specification.Internal.QuerySpecification.AsExpression(); 43 | return await (expression == null ? query.CountAsync() : query.CountAsync(expression)).ConfigureAwait(false); 44 | } 45 | 46 | public static async Task FirstAsync(this IQueryable query, ISpecification specification) 47 | where TEntity : class 48 | { 49 | if (specification == null) 50 | { 51 | throw new ArgumentNullException(nameof(specification)); 52 | } 53 | 54 | var expression = specification.Internal.QuerySpecification.AsExpression(); 55 | return await (expression == null ? query.FirstAsync() : query.FirstAsync(expression)).ConfigureAwait(false); 56 | } 57 | 58 | public static async Task FirstOrDefaultAsync(this IQueryable query, ISpecification specification) 59 | where TEntity : class 60 | { 61 | if (specification == null) 62 | { 63 | throw new ArgumentNullException(nameof(specification)); 64 | } 65 | 66 | var expression = specification.Internal.QuerySpecification.AsExpression(); 67 | return await (expression == null ? query.FirstOrDefaultAsync() : query.FirstOrDefaultAsync(expression)).ConfigureAwait(false); 68 | } 69 | 70 | public static async Task SingleAsync(this IQueryable query, ISpecification specification) 71 | where TEntity : class 72 | { 73 | if (specification == null) 74 | { 75 | throw new ArgumentNullException(nameof(specification)); 76 | } 77 | 78 | var expression = specification.Internal.QuerySpecification.AsExpression(); 79 | return await (expression == null ? query.SingleAsync() : query.SingleAsync(expression)).ConfigureAwait(false); 80 | } 81 | 82 | public static async Task SingleOrDefaultAsync(this IQueryable query, ISpecification specification) 83 | where TEntity : class 84 | { 85 | if (specification == null) 86 | { 87 | throw new ArgumentNullException(nameof(specification)); 88 | } 89 | 90 | var expression = specification.Internal.QuerySpecification.AsExpression(); 91 | return await (expression == null ? query.SingleOrDefaultAsync() : query.SingleOrDefaultAsync(expression)).ConfigureAwait(false); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/SpecificationExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using FluentAssertions; 2 | using LinqBuilder.Tests.Data; 3 | using Xunit; 4 | 5 | namespace LinqBuilder.Tests; 6 | 7 | public class SpecificationExtensionsTests 8 | { 9 | private readonly ISpecification _emptySpecification = Spec.New(); 10 | private readonly ISpecification _value1ShouldBe3 = Spec.New(entity => entity.Value1 == 3); 11 | private readonly ISpecification _value1ShouldBe5 = Spec.New(entity => entity.Value1 == 5); 12 | private readonly ISpecification _value2ShouldBe5 = Spec.New(entity => entity.Value2 == 5); 13 | 14 | [Theory] 15 | [ClassData(typeof(TestData))] 16 | public void IsSatisfiedBy_Theory(Entity entity, bool expected) 17 | { 18 | _value1ShouldBe3 19 | .IsSatisfiedBy(entity) 20 | .Should().Be(expected); 21 | } 22 | 23 | [Theory] 24 | [ClassData(typeof(AndTestData))] 25 | public void And_IsSatisfiedBy_Theory(Entity entity, bool expected) 26 | { 27 | _value1ShouldBe3 28 | .And(_value2ShouldBe5) 29 | .IsSatisfiedBy(entity) 30 | .Should().Be(expected); 31 | } 32 | 33 | [Fact] 34 | public void AndNew_IsSatisfiedBy_ShouldBeTrue() 35 | { 36 | _value1ShouldBe3 37 | .And(_emptySpecification) 38 | .IsSatisfiedBy(new Entity { Value1 = 3 }) 39 | .Should().Be(true); 40 | } 41 | 42 | [Fact] 43 | public void NewAnd_IsSatisfiedBy_ShouldBeTrue() 44 | { 45 | _emptySpecification 46 | .And(_value2ShouldBe5) 47 | .IsSatisfiedBy(new Entity { Value2 = 5 }) 48 | .Should().Be(true); 49 | } 50 | 51 | [Fact] 52 | public void AndEmpty_IsSatisfiedBy_ShouldBeTrue() 53 | { 54 | _emptySpecification 55 | .And(_emptySpecification) 56 | .IsSatisfiedBy(new Entity()) 57 | .Should().Be(true); 58 | } 59 | 60 | [Theory] 61 | [ClassData(typeof(OrTestData))] 62 | public void Or_IsSatisfiedBy_Theory(Entity entity, bool expected) 63 | { 64 | _value1ShouldBe3 65 | .Or(_value1ShouldBe5) 66 | .IsSatisfiedBy(entity) 67 | .Should().Be(expected); 68 | } 69 | 70 | [Fact] 71 | public void OrNew_IsSatisfiedBy_ShouldBeTrue() 72 | { 73 | _value1ShouldBe3 74 | .Or(_emptySpecification) 75 | .IsSatisfiedBy(new Entity { Value1 = 3 }) 76 | .Should().Be(true); 77 | } 78 | 79 | [Fact] 80 | public void NewOr_IsSatisfiedBy_ShouldBeTrue() 81 | { 82 | _emptySpecification 83 | .Or(_value1ShouldBe5) 84 | .IsSatisfiedBy(new Entity { Value1 = 5 }) 85 | .Should().Be(true); 86 | } 87 | 88 | [Fact] 89 | public void OrEmpty_IsSatisfiedBy_ShouldBeTrue() 90 | { 91 | _emptySpecification 92 | .Or(_emptySpecification) 93 | .IsSatisfiedBy(new Entity()) 94 | .Should().Be(true); 95 | } 96 | 97 | [Theory] 98 | [ClassData(typeof(NotTestData))] 99 | public void Not_IsSatisfiedBy_Theory(Entity entity, bool expected) 100 | { 101 | _value1ShouldBe5 102 | .Not() 103 | .IsSatisfiedBy(entity) 104 | .Should().Be(expected); 105 | } 106 | 107 | [Fact] 108 | public void NewNot_IsSatisfiedBy_ShouldBeTrue() 109 | { 110 | _emptySpecification 111 | .Not() 112 | .IsSatisfiedBy(new Entity()) 113 | .Should().Be(true); 114 | } 115 | 116 | [Fact] 117 | public void Clone_Specifications_ShouldNotBeEqual() 118 | { 119 | var specClone = _value1ShouldBe3.Clone(); 120 | _value1ShouldBe3.Should().NotBe(specClone); 121 | _value1ShouldBe3.Internal.QuerySpecification.Should().Be(specClone.Internal.QuerySpecification); 122 | _value1ShouldBe3.Internal.OrderSpecifications.Should().Equal(specClone.Internal.OrderSpecifications); 123 | _value1ShouldBe3.Internal.Skip.Should().Be(specClone.Internal.Skip); 124 | _value1ShouldBe3.Internal.Take.Should().Be(specClone.Internal.Take); 125 | } 126 | 127 | private sealed class TestData : EntityTheoryData 128 | { 129 | public TestData() 130 | { 131 | AddEntity(3, 1, true); 132 | AddEntity(4, 1, false); 133 | } 134 | } 135 | 136 | private sealed class AndTestData : EntityTheoryData 137 | { 138 | public AndTestData() 139 | { 140 | AddEntity(3, 5, true); 141 | AddEntity(3, 4, false); 142 | } 143 | } 144 | 145 | private sealed class OrTestData : EntityTheoryData 146 | { 147 | public OrTestData() 148 | { 149 | AddEntity(3, 1, true); 150 | AddEntity(5, 1, true); 151 | AddEntity(4, 1, false); 152 | } 153 | } 154 | 155 | private sealed class NotTestData : EntityTheoryData 156 | { 157 | public NotTestData() 158 | { 159 | AddEntity(3, 1, true); 160 | AddEntity(5, 1, false); 161 | } 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/LinqBuilder/SpecificationExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using LinqBuilder.Internal; 4 | using LinqKit; 5 | 6 | namespace LinqBuilder 7 | { 8 | public static class SpecificationExtensions 9 | { 10 | public static ISpecification And(this ISpecification left, ISpecification right) 11 | where TEntity : class 12 | { 13 | if (left == null) 14 | { 15 | throw new ArgumentNullException(nameof(left)); 16 | } 17 | 18 | if (right == null) 19 | { 20 | throw new ArgumentNullException(nameof(right)); 21 | } 22 | 23 | var compositeSpecification = And(left.Internal.QuerySpecification.AsExpression(), right.Internal.QuerySpecification.AsExpression()); 24 | return SetQuerySpecification(left, compositeSpecification); 25 | } 26 | 27 | public static ISpecification Or(this ISpecification left, ISpecification right) 28 | where TEntity : class 29 | { 30 | if (left == null) 31 | { 32 | throw new ArgumentNullException(nameof(left)); 33 | } 34 | 35 | if (right == null) 36 | { 37 | throw new ArgumentNullException(nameof(right)); 38 | } 39 | 40 | var compositeSpecification = Or(left.Internal.QuerySpecification.AsExpression(), right.Internal.QuerySpecification.AsExpression()); 41 | return SetQuerySpecification(left, compositeSpecification); 42 | } 43 | 44 | public static ISpecification Not(this ISpecification specification) 45 | where TEntity : class 46 | { 47 | if (specification == null) 48 | { 49 | throw new ArgumentNullException(nameof(specification)); 50 | } 51 | 52 | var compositeSpecification = Not(specification.Internal.QuerySpecification.AsExpression()); 53 | return SetQuerySpecification(specification, compositeSpecification); 54 | } 55 | 56 | public static bool IsSatisfiedBy(this ISpecification specification, TEntity entity) 57 | where TEntity : class 58 | { 59 | if (specification == null) 60 | { 61 | throw new ArgumentNullException(nameof(specification)); 62 | } 63 | 64 | var predicate = specification.Internal.QuerySpecification.AsFunc(); 65 | return predicate == null || predicate(entity); 66 | } 67 | 68 | public static ISpecification Clone(this ISpecification specification) 69 | where TEntity : class 70 | { 71 | if (specification == null) 72 | { 73 | throw new ArgumentNullException(nameof(specification)); 74 | } 75 | 76 | return specification.Internal.Clone(); 77 | } 78 | 79 | private static ISpecification SetQuerySpecification(ISpecification specification, IQuerySpecification querySpecification) 80 | where TEntity : class 81 | { 82 | var configuration = specification.Internal.Clone().Internal; 83 | configuration.QuerySpecification = querySpecification; 84 | return configuration; 85 | } 86 | 87 | private static Specification And(Expression>? leftExpression, Expression>? rightExpression) 88 | where TEntity : class 89 | { 90 | if (leftExpression != null && rightExpression != null) 91 | { 92 | var predicate = PredicateBuilder.New(leftExpression); 93 | return new Specification(predicate.And(rightExpression)); 94 | } 95 | 96 | if (leftExpression == null && rightExpression != null) 97 | { 98 | return new Specification(rightExpression); 99 | } 100 | 101 | return leftExpression != null ? new Specification(leftExpression) : new Specification(); 102 | } 103 | 104 | private static Specification Or(Expression>? leftExpression, Expression>? rightExpression) 105 | where TEntity : class 106 | { 107 | if (leftExpression != null && rightExpression != null) 108 | { 109 | var predicate = PredicateBuilder.New(leftExpression); 110 | return new Specification(predicate.Or(rightExpression)); 111 | } 112 | 113 | if (leftExpression == null && rightExpression != null) 114 | { 115 | return new Specification(rightExpression); 116 | } 117 | 118 | return leftExpression != null ? new Specification(leftExpression) : new Specification(); 119 | } 120 | 121 | private static Specification Not(Expression>? specificationExpression) 122 | where TEntity : class 123 | { 124 | if (specificationExpression == null) 125 | { 126 | return new Specification(); 127 | } 128 | 129 | var notExpression = Expression.Not(specificationExpression.Body); 130 | var expression = Expression.Lambda>(notExpression, specificationExpression.Parameters); 131 | return new Specification(expression); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /src/EF6/LinqBuilder.EF6.Tests/EntityFrameworkExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using FluentAssertions; 5 | using LinqBuilder.EF6.Tests.Data; 6 | using Xunit; 7 | 8 | namespace LinqBuilder.EF6.Tests; 9 | 10 | public sealed class EntityFrameworkExtensionsTests : IDisposable 11 | { 12 | private readonly ISpecification _emptySpecification = Spec.New(); 13 | private readonly ISpecification _value1ShouldBe1 = Spec.New(entity => entity.Value1 == 1); 14 | private readonly ISpecification _value1ShouldBe2 = Spec.New(entity => entity.Value1 == 2); 15 | private readonly ISpecification _value1ShouldBe4 = Spec.New(entity => entity.Value1 == 4); 16 | private readonly ISpecification _value2ShouldBe3 = Spec.New(entity => entity.Value2 == 3); 17 | 18 | private readonly TestDb _testDb; 19 | 20 | public EntityFrameworkExtensionsTests() 21 | { 22 | _testDb = new TestDb(); 23 | _testDb.AddEntity(2, 3); 24 | _testDb.AddEntity(1, 3); 25 | _testDb.AddEntity(1, 3); 26 | _testDb.Context.SaveChanges(); 27 | } 28 | 29 | public void Dispose() 30 | { 31 | _testDb.Dispose(); 32 | } 33 | 34 | [Fact] 35 | public async Task AnyAsync_Specification_ShouldBeTrue() 36 | { 37 | var result = await _testDb.Context.Entities 38 | .AnyAsync(_value1ShouldBe1); 39 | 40 | result.Should().BeTrue(); 41 | } 42 | 43 | [Fact] 44 | public async Task AnyAsync_Specification_ShouldBeFalse() 45 | { 46 | var result = await _testDb.Context.Entities 47 | .AnyAsync(_value1ShouldBe4); 48 | 49 | result.Should().BeFalse(); 50 | } 51 | 52 | [Fact] 53 | public async Task AnyAsync_EmptySpecification_ShouldBeTrue() 54 | { 55 | var result = await _testDb.Context.Entities 56 | .AnyAsync(_emptySpecification); 57 | 58 | result.Should().BeTrue(); 59 | } 60 | 61 | [Fact] 62 | public async Task AllAsync_Specification_ShouldBeTrue() 63 | { 64 | var result = await _testDb.Context.Entities 65 | .AllAsync(_value2ShouldBe3); 66 | 67 | result.Should().BeTrue(); 68 | } 69 | 70 | [Fact] 71 | public async Task AllAsync_Specification_ShouldBeFalse() 72 | { 73 | var result = await _testDb.Context.Entities 74 | .AllAsync(_value1ShouldBe1); 75 | 76 | result.Should().BeFalse(); 77 | } 78 | 79 | [Fact] 80 | public async Task AllAsync_EmptySpecification_ShouldBeTrue() 81 | { 82 | var result = await _testDb.Context.Entities 83 | .AllAsync(_emptySpecification); 84 | 85 | result.Should().BeTrue(); 86 | } 87 | 88 | [Fact] 89 | public async Task CountAsync_Specification_ShouldBeTrue() 90 | { 91 | var result = await _testDb.Context.Entities 92 | .CountAsync(_value1ShouldBe1); 93 | 94 | result.Should().Be(2); 95 | } 96 | 97 | [Fact] 98 | public async Task CountAsync_EmptySpecification_ShouldBeEqualCount() 99 | { 100 | var result = await _testDb.Context.Entities 101 | .CountAsync(_emptySpecification); 102 | 103 | result.Should().Be(_testDb.Context.Entities.Count()); 104 | } 105 | 106 | [Fact] 107 | public async Task FirstAsync_Specification_ShouldReturnCorrectResult() 108 | { 109 | var result = await _testDb.Context.Entities 110 | .FirstAsync(_value1ShouldBe1); 111 | 112 | result.Should().Be(await _testDb.Context.Entities.FindAsync(2)); 113 | } 114 | 115 | [Fact] 116 | public async Task FirstAsync_EmptySpecification_ShouldReturnCorrectResult() 117 | { 118 | var result = await _testDb.Context.Entities 119 | .FirstAsync(_emptySpecification); 120 | 121 | result.Should().Be(await _testDb.Context.Entities.FindAsync(1)); 122 | } 123 | 124 | [Fact] 125 | public async Task FirstOrDefaultAsync_Specification_ShouldReturnCorrectResult() 126 | { 127 | var result = await _testDb.Context.Entities 128 | .FirstOrDefaultAsync(_value1ShouldBe1); 129 | 130 | result.Should().Be(await _testDb.Context.Entities.FindAsync(2)); 131 | } 132 | 133 | [Fact] 134 | public async Task FirstOrDefaultAsync_EmptySpecification_ShouldReturnCorrectResult() 135 | { 136 | var result = await _testDb.Context.Entities 137 | .FirstOrDefaultAsync(_emptySpecification); 138 | 139 | result.Should().Be(await _testDb.Context.Entities.FindAsync(1)); 140 | } 141 | 142 | [Fact] 143 | public async Task SingleAsync_Specification_ShouldReturnCorrectResult() 144 | { 145 | var result = await _testDb.Context.Entities 146 | .SingleAsync(_value1ShouldBe2); 147 | 148 | result.Should().Be(await _testDb.Context.Entities.FindAsync(1)); 149 | } 150 | 151 | [Fact] 152 | public async Task SingleAsync_EmptySpecification_ShouldReturnCorrectResult() 153 | { 154 | Func act = () => _testDb.Context.Entities.SingleAsync(_emptySpecification); 155 | 156 | await act.Should().ThrowAsync(); 157 | } 158 | 159 | [Fact] 160 | public async Task SingleOrDefaultAsync_Specification_ShouldReturnCorrectResult() 161 | { 162 | var result = await _testDb.Context.Entities 163 | .SingleOrDefaultAsync(_value1ShouldBe2); 164 | 165 | result.Should().Be(await _testDb.Context.Entities.FindAsync(1)); 166 | } 167 | 168 | [Fact] 169 | public async Task SingleOrDefaultAsync_EmptySpecification_ShouldReturnCorrectResult() 170 | { 171 | Func act = () => _testDb.Context.Entities.SingleOrDefaultAsync(_emptySpecification); 172 | 173 | await act.Should().ThrowAsync(); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /src/EFCore/LinqBuilder.EFCore.Tests/EntityFrameworkCoreExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using FluentAssertions; 5 | using LinqBuilder.EFCore.Tests.Data; 6 | using Xunit; 7 | 8 | namespace LinqBuilder.EFCore.Tests; 9 | 10 | public sealed class EntityFrameworkCoreExtensionsTests : IDisposable 11 | { 12 | private readonly ISpecification _emptySpecification = Spec.New(); 13 | private readonly ISpecification _value1ShouldBe1 = Spec.New(entity => entity.Value1 == 1); 14 | private readonly ISpecification _value1ShouldBe2 = Spec.New(entity => entity.Value1 == 2); 15 | private readonly ISpecification _value1ShouldBe4 = Spec.New(entity => entity.Value1 == 4); 16 | private readonly ISpecification _value2ShouldBe3 = Spec.New(entity => entity.Value2 == 3); 17 | 18 | private readonly TestDb _testDb; 19 | 20 | public EntityFrameworkCoreExtensionsTests() 21 | { 22 | _testDb = new TestDb(); 23 | _testDb.AddEntity(2, 3); 24 | _testDb.AddEntity(1, 3); 25 | _testDb.AddEntity(1, 3); 26 | _testDb.Context.SaveChanges(); 27 | } 28 | 29 | public void Dispose() 30 | { 31 | _testDb.Dispose(); 32 | } 33 | 34 | [Fact] 35 | public async Task AnyAsync_Specification_ShouldBeTrue() 36 | { 37 | var result = await _testDb.Context.Entities 38 | .AnyAsync(_value1ShouldBe1); 39 | 40 | result.Should().BeTrue(); 41 | } 42 | 43 | [Fact] 44 | public async Task AnyAsync_Specification_ShouldBeFalse() 45 | { 46 | var result = await _testDb.Context.Entities 47 | .AnyAsync(_value1ShouldBe4); 48 | 49 | result.Should().BeFalse(); 50 | } 51 | 52 | [Fact] 53 | public async Task AnyAsync_EmptySpecification_ShouldBeTrue() 54 | { 55 | var result = await _testDb.Context.Entities 56 | .AnyAsync(_emptySpecification); 57 | 58 | result.Should().BeTrue(); 59 | } 60 | 61 | [Fact] 62 | public async Task AllAsync_Specification_ShouldBeTrue() 63 | { 64 | var result = await _testDb.Context.Entities 65 | .AllAsync(_value2ShouldBe3); 66 | 67 | result.Should().BeTrue(); 68 | } 69 | 70 | [Fact] 71 | public async Task AllAsync_Specification_ShouldBeFalse() 72 | { 73 | var result = await _testDb.Context.Entities 74 | .AllAsync(_value1ShouldBe1); 75 | 76 | result.Should().BeFalse(); 77 | } 78 | 79 | [Fact] 80 | public async Task AllAsync_EmptySpecification_ShouldBeTrue() 81 | { 82 | var result = await _testDb.Context.Entities 83 | .AllAsync(_emptySpecification); 84 | 85 | result.Should().BeTrue(); 86 | } 87 | 88 | [Fact] 89 | public async Task CountAsync_Specification_ShouldBeTrue() 90 | { 91 | var result = await _testDb.Context.Entities 92 | .CountAsync(_value1ShouldBe1); 93 | 94 | result.Should().Be(2); 95 | } 96 | 97 | [Fact] 98 | public async Task CountAsync_EmptySpecification_ShouldBeEqualCount() 99 | { 100 | var result = await _testDb.Context.Entities 101 | .CountAsync(_emptySpecification); 102 | 103 | result.Should().Be(_testDb.Context.Entities.Count()); 104 | } 105 | 106 | [Fact] 107 | public async Task FirstAsync_Specification_ShouldReturnCorrectResult() 108 | { 109 | var result = await _testDb.Context.Entities 110 | .FirstAsync(_value1ShouldBe1); 111 | 112 | result.Should().Be(await _testDb.Context.Entities.FindAsync(2)); 113 | } 114 | 115 | [Fact] 116 | public async Task FirstAsync_EmptySpecification_ShouldReturnCorrectResult() 117 | { 118 | var result = await _testDb.Context.Entities 119 | .FirstAsync(_emptySpecification); 120 | 121 | result.Should().Be(await _testDb.Context.Entities.FindAsync(1)); 122 | } 123 | 124 | [Fact] 125 | public async Task FirstOrDefaultAsync_Specification_ShouldReturnCorrectResult() 126 | { 127 | var result = await _testDb.Context.Entities 128 | .FirstOrDefaultAsync(_value1ShouldBe1); 129 | 130 | result.Should().Be(await _testDb.Context.Entities.FindAsync(2)); 131 | } 132 | 133 | [Fact] 134 | public async Task FirstOrDefaultAsync_EmptySpecification_ShouldReturnCorrectResult() 135 | { 136 | var result = await _testDb.Context.Entities 137 | .FirstOrDefaultAsync(_emptySpecification); 138 | 139 | result.Should().Be(await _testDb.Context.Entities.FindAsync(1)); 140 | } 141 | 142 | [Fact] 143 | public async Task SingleAsync_Specification_ShouldReturnCorrectResult() 144 | { 145 | var result = await _testDb.Context.Entities 146 | .SingleAsync(_value1ShouldBe2); 147 | 148 | result.Should().Be(await _testDb.Context.Entities.FindAsync(1)); 149 | } 150 | 151 | [Fact] 152 | public async Task SingleAsync_EmptySpecification_ShouldReturnCorrectResult() 153 | { 154 | Func act = () => _testDb.Context.Entities.SingleAsync(_emptySpecification); 155 | 156 | await act.Should().ThrowAsync(); 157 | } 158 | 159 | [Fact] 160 | public async Task SingleOrDefaultAsync_Specification_ShouldReturnCorrectResult() 161 | { 162 | var result = await _testDb.Context.Entities 163 | .SingleOrDefaultAsync(_value1ShouldBe2); 164 | 165 | result.Should().Be(await _testDb.Context.Entities.FindAsync(1)); 166 | } 167 | 168 | [Fact] 169 | public async Task SingleOrDefaultAsync_EmptySpecification_ShouldReturnCorrectResult() 170 | { 171 | Func act = () => _testDb.Context.Entities.SingleOrDefaultAsync(_emptySpecification); 172 | 173 | await act.Should().ThrowAsync(); 174 | } 175 | } 176 | -------------------------------------------------------------------------------- /src/LinqBuilder/OrderBy/SpecificationExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace LinqBuilder.OrderBy 5 | { 6 | public static class SpecificationExtensions 7 | { 8 | public static IOrderedSpecification OrderBy(this ISpecification specification, IOrderSpecification orderSpecification) 9 | where TEntity : class 10 | { 11 | if (specification == null) 12 | { 13 | throw new ArgumentNullException(nameof(specification)); 14 | } 15 | 16 | return AddOrderSpecification(specification, orderSpecification); 17 | } 18 | 19 | public static IOrderedSpecification ThenBy(this IOrderSpecification specification, IOrderSpecification orderSpecification) 20 | where TEntity : class 21 | { 22 | if (specification == null) 23 | { 24 | throw new ArgumentNullException(nameof(specification)); 25 | } 26 | 27 | return AddOrderSpecification(specification, orderSpecification); 28 | } 29 | 30 | public static IOrderedSpecification ThenBy(this IOrderedSpecification specification, IOrderSpecification orderSpecification) 31 | where TEntity : class 32 | { 33 | if (specification == null) 34 | { 35 | throw new ArgumentNullException(nameof(specification)); 36 | } 37 | 38 | return AddOrderSpecification(specification, orderSpecification); 39 | } 40 | 41 | public static ISpecification UseOrdering(this ISpecification specification, ISpecification orderedSpecification) 42 | where TEntity : class 43 | { 44 | if (specification == null) 45 | { 46 | throw new ArgumentNullException(nameof(specification)); 47 | } 48 | 49 | if (orderedSpecification == null) 50 | { 51 | throw new ArgumentNullException(nameof(orderedSpecification)); 52 | } 53 | 54 | var configuration = specification.Internal; 55 | var orderedConfiguration = orderedSpecification.Internal; 56 | configuration.AddOrderSpecifications(orderedConfiguration.OrderSpecifications.ToArray()); 57 | configuration.Skip = orderedConfiguration.Skip; 58 | configuration.Take = orderedConfiguration.Take; 59 | return configuration; 60 | } 61 | 62 | public static ISpecification Skip(this ISpecification specification, int count) 63 | where TEntity : class 64 | { 65 | if (specification == null) 66 | { 67 | throw new ArgumentNullException(nameof(specification)); 68 | } 69 | 70 | var configuration = specification.Internal.Clone().Internal; 71 | configuration.Skip = count; 72 | return configuration; 73 | } 74 | 75 | public static ISpecification Take(this ISpecification specification, int count) 76 | where TEntity : class 77 | { 78 | if (specification == null) 79 | { 80 | throw new ArgumentNullException(nameof(specification)); 81 | } 82 | 83 | var configuration = specification.Internal.Clone().Internal; 84 | configuration.Take = count; 85 | return configuration; 86 | } 87 | 88 | public static ISpecification Paginate(this ISpecification specification, int pageNo, int pageSize) 89 | where TEntity : class 90 | { 91 | if (specification == null) 92 | { 93 | throw new ArgumentNullException(nameof(specification)); 94 | } 95 | 96 | if (pageNo < 1) 97 | { 98 | throw new ArgumentException("Cannot be less than 1!", nameof(pageNo)); 99 | } 100 | 101 | if (pageSize < 1) 102 | { 103 | throw new ArgumentException("Cannot be less than 1!", nameof(pageSize)); 104 | } 105 | 106 | var configuration = specification.Internal.Clone().Internal; 107 | configuration.Skip = (pageNo - 1) * pageSize; 108 | configuration.Take = pageSize; 109 | return configuration; 110 | } 111 | 112 | public static bool IsOrdered(this ISpecification specification) 113 | where TEntity : class 114 | { 115 | if (specification == null) 116 | { 117 | throw new ArgumentNullException(nameof(specification)); 118 | } 119 | 120 | return specification.Internal.OrderSpecifications.Any(); 121 | } 122 | 123 | public static bool HasSkip(this ISpecification specification) 124 | where TEntity : class 125 | { 126 | if (specification == null) 127 | { 128 | throw new ArgumentNullException(nameof(specification)); 129 | } 130 | 131 | return specification.Internal.Skip != null; 132 | } 133 | 134 | public static bool HasTake(this ISpecification specification) 135 | where TEntity : class 136 | { 137 | if (specification == null) 138 | { 139 | throw new ArgumentNullException(nameof(specification)); 140 | } 141 | 142 | return specification.Internal.Take != null; 143 | } 144 | 145 | private static IOrderedSpecification AddOrderSpecification(ISpecification specification, IOrderSpecification orderSpecification) 146 | where TEntity : class 147 | { 148 | var configuration = specification.Internal.Clone().Internal; 149 | configuration.AddOrderSpecifications(orderSpecification); 150 | return configuration; 151 | } 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /LinqBuilder.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2020 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqBuilder", "src\LinqBuilder\LinqBuilder.csproj", "{8FE1E60D-7D6B-44F0-9CC4-5E41B70F03A9}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqBuilder.Tests", "src\LinqBuilder.Tests\LinqBuilder.Tests.csproj", "{749A19C0-9689-4046-9EED-99651D9B413A}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E9CE7133-E0CC-469B-81F9-529EE0AE003D}" 11 | ProjectSection(SolutionItems) = preProject 12 | .editorconfig = .editorconfig 13 | .gitattributes = .gitattributes 14 | .gitignore = .gitignore 15 | appveyor.yml = appveyor.yml 16 | Directory.Build.props = Directory.Build.props 17 | GitVersion.yml = GitVersion.yml 18 | LICENSE.txt = LICENSE.txt 19 | README.md = README.md 20 | stylecop.json = stylecop.json 21 | Directory.Packages.props = Directory.Packages.props 22 | .globalconfig = .globalconfig 23 | NuGet.Config = NuGet.Config 24 | EndProjectSection 25 | EndProject 26 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{DAB01770-8D12-4638-9BBA-ABC31C2A2D6E}" 27 | EndProject 28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqBuilder.EF6", "src\EF6\LinqBuilder.EF6\LinqBuilder.EF6.csproj", "{F0DDC57B-F7AF-403C-9C18-D2CBA4C769D5}" 29 | EndProject 30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqBuilder.EFCore", "src\EFCore\LinqBuilder.EFCore\LinqBuilder.EFCore.csproj", "{C496F944-92FD-410D-ABB1-019F640D6C79}" 31 | EndProject 32 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqBuilder.EFCore.Tests", "src\EFCore\LinqBuilder.EFCore.Tests\LinqBuilder.EFCore.Tests.csproj", "{3E0B788B-2B01-4F27-B2FD-12FAEDD8C88A}" 33 | EndProject 34 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinqBuilder.EF6.Tests", "src\EF6\LinqBuilder.EF6.Tests\LinqBuilder.EF6.Tests.csproj", "{318A1093-3885-4E30-BF3D-F7D331F4A626}" 35 | EndProject 36 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".vscode", ".vscode", "{9D486D51-00D2-47A0-87EA-3128DC8A12AF}" 37 | ProjectSection(SolutionItems) = preProject 38 | .vscode\extensions.json = .vscode\extensions.json 39 | .vscode\settings.json = .vscode\settings.json 40 | EndProjectSection 41 | EndProject 42 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "EF6", "EF6", "{BAF26E71-25FB-4E1C-ADF2-E9F8C2E78661}" 43 | EndProject 44 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "EFCore", "EFCore", "{865F6FAD-36D3-4675-9191-3AEBF9E717B2}" 45 | EndProject 46 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "build\_build.csproj", "{11A7E48F-5E13-4957-B055-8A6EB24FE6C6}" 47 | EndProject 48 | Global 49 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 50 | Debug|Any CPU = Debug|Any CPU 51 | Release|Any CPU = Release|Any CPU 52 | EndGlobalSection 53 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 54 | {11A7E48F-5E13-4957-B055-8A6EB24FE6C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 55 | {11A7E48F-5E13-4957-B055-8A6EB24FE6C6}.Release|Any CPU.ActiveCfg = Release|Any CPU 56 | {8FE1E60D-7D6B-44F0-9CC4-5E41B70F03A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 57 | {8FE1E60D-7D6B-44F0-9CC4-5E41B70F03A9}.Debug|Any CPU.Build.0 = Debug|Any CPU 58 | {8FE1E60D-7D6B-44F0-9CC4-5E41B70F03A9}.Release|Any CPU.ActiveCfg = Release|Any CPU 59 | {8FE1E60D-7D6B-44F0-9CC4-5E41B70F03A9}.Release|Any CPU.Build.0 = Release|Any CPU 60 | {749A19C0-9689-4046-9EED-99651D9B413A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 61 | {749A19C0-9689-4046-9EED-99651D9B413A}.Debug|Any CPU.Build.0 = Debug|Any CPU 62 | {749A19C0-9689-4046-9EED-99651D9B413A}.Release|Any CPU.ActiveCfg = Release|Any CPU 63 | {749A19C0-9689-4046-9EED-99651D9B413A}.Release|Any CPU.Build.0 = Release|Any CPU 64 | {F0DDC57B-F7AF-403C-9C18-D2CBA4C769D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 65 | {F0DDC57B-F7AF-403C-9C18-D2CBA4C769D5}.Debug|Any CPU.Build.0 = Debug|Any CPU 66 | {F0DDC57B-F7AF-403C-9C18-D2CBA4C769D5}.Release|Any CPU.ActiveCfg = Release|Any CPU 67 | {F0DDC57B-F7AF-403C-9C18-D2CBA4C769D5}.Release|Any CPU.Build.0 = Release|Any CPU 68 | {C496F944-92FD-410D-ABB1-019F640D6C79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 69 | {C496F944-92FD-410D-ABB1-019F640D6C79}.Debug|Any CPU.Build.0 = Debug|Any CPU 70 | {C496F944-92FD-410D-ABB1-019F640D6C79}.Release|Any CPU.ActiveCfg = Release|Any CPU 71 | {C496F944-92FD-410D-ABB1-019F640D6C79}.Release|Any CPU.Build.0 = Release|Any CPU 72 | {3E0B788B-2B01-4F27-B2FD-12FAEDD8C88A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 73 | {3E0B788B-2B01-4F27-B2FD-12FAEDD8C88A}.Debug|Any CPU.Build.0 = Debug|Any CPU 74 | {3E0B788B-2B01-4F27-B2FD-12FAEDD8C88A}.Release|Any CPU.ActiveCfg = Release|Any CPU 75 | {3E0B788B-2B01-4F27-B2FD-12FAEDD8C88A}.Release|Any CPU.Build.0 = Release|Any CPU 76 | {318A1093-3885-4E30-BF3D-F7D331F4A626}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 77 | {318A1093-3885-4E30-BF3D-F7D331F4A626}.Debug|Any CPU.Build.0 = Debug|Any CPU 78 | {318A1093-3885-4E30-BF3D-F7D331F4A626}.Release|Any CPU.ActiveCfg = Release|Any CPU 79 | {318A1093-3885-4E30-BF3D-F7D331F4A626}.Release|Any CPU.Build.0 = Release|Any CPU 80 | EndGlobalSection 81 | GlobalSection(SolutionProperties) = preSolution 82 | HideSolutionNode = FALSE 83 | EndGlobalSection 84 | GlobalSection(NestedProjects) = preSolution 85 | {9D486D51-00D2-47A0-87EA-3128DC8A12AF} = {E9CE7133-E0CC-469B-81F9-529EE0AE003D} 86 | {8FE1E60D-7D6B-44F0-9CC4-5E41B70F03A9} = {DAB01770-8D12-4638-9BBA-ABC31C2A2D6E} 87 | {749A19C0-9689-4046-9EED-99651D9B413A} = {DAB01770-8D12-4638-9BBA-ABC31C2A2D6E} 88 | {BAF26E71-25FB-4E1C-ADF2-E9F8C2E78661} = {DAB01770-8D12-4638-9BBA-ABC31C2A2D6E} 89 | {865F6FAD-36D3-4675-9191-3AEBF9E717B2} = {DAB01770-8D12-4638-9BBA-ABC31C2A2D6E} 90 | {F0DDC57B-F7AF-403C-9C18-D2CBA4C769D5} = {BAF26E71-25FB-4E1C-ADF2-E9F8C2E78661} 91 | {318A1093-3885-4E30-BF3D-F7D331F4A626} = {BAF26E71-25FB-4E1C-ADF2-E9F8C2E78661} 92 | {C496F944-92FD-410D-ABB1-019F640D6C79} = {865F6FAD-36D3-4675-9191-3AEBF9E717B2} 93 | {3E0B788B-2B01-4F27-B2FD-12FAEDD8C88A} = {865F6FAD-36D3-4675-9191-3AEBF9E717B2} 94 | EndGlobalSection 95 | GlobalSection(ExtensibilityGlobals) = postSolution 96 | SolutionGuid = {4029E7C4-F013-4A5C-A3A7-9BD01FA6ED4A} 97 | EndGlobalSection 98 | EndGlobal 99 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/LinqExtensionsExeSpecTests.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using FluentAssertions; 3 | using LinqBuilder.OrderBy; 4 | using LinqBuilder.Tests.Data; 5 | using Xunit; 6 | 7 | namespace LinqBuilder.Tests; 8 | 9 | public class LinqExtensionsExeSpecTests 10 | { 11 | private readonly ISpecification _value1ShouldBe2 = Spec.New(entity => entity.Value1 == 2); 12 | private readonly IOrderSpecification _orderValue1Asc = OrderSpec.New(entity => entity.Value1); 13 | private readonly IOrderSpecification _orderValue2Asc = OrderSpec.New(entity => entity.Value2); 14 | 15 | private readonly Fixture _fixture; 16 | 17 | public LinqExtensionsExeSpecTests() 18 | { 19 | _fixture = new Fixture(); 20 | _fixture.AddToCollection(3, 1); 21 | _fixture.AddToCollection(1, 1); 22 | _fixture.AddToCollection(2, 2); 23 | _fixture.AddToCollection(2, 1); 24 | } 25 | 26 | [Fact] 27 | public void ExeSpecNoSort_IQueryable_ShouldReturnCorrectResult() 28 | { 29 | var result = _fixture.Query 30 | .ExeSpec(_value1ShouldBe2) 31 | .ToList(); 32 | 33 | result.Count.Should().Be(2); 34 | result[0].Value1.Should().Be(2); 35 | result[0].Value2.Should().Be(2); 36 | result[1].Value1.Should().Be(2); 37 | result[1].Value2.Should().Be(1); 38 | } 39 | 40 | [Fact] 41 | public void ExeSpecNoSort_IEnumerable_ShouldReturnCorrectResult() 42 | { 43 | var result = _fixture.Collection 44 | .ExeSpec(_value1ShouldBe2) 45 | .ToList(); 46 | 47 | result.Count.Should().Be(2); 48 | result[0].Value1.Should().Be(2); 49 | result[0].Value2.Should().Be(2); 50 | result[1].Value1.Should().Be(2); 51 | result[1].Value2.Should().Be(1); 52 | } 53 | 54 | [Fact] 55 | public void ExeSpecSort_IQueryable_ShouldReturnCorrectResult() 56 | { 57 | var result = _fixture.Query 58 | .ExeSpec(_value1ShouldBe2.OrderBy(_orderValue2Asc)) 59 | .ToList(); 60 | 61 | result.Count.Should().Be(2); 62 | result[0].Value1.Should().Be(2); 63 | result[0].Value2.Should().Be(1); 64 | result[1].Value1.Should().Be(2); 65 | result[1].Value2.Should().Be(2); 66 | } 67 | 68 | [Fact] 69 | public void ExeSpecSort_IEnumerable_ShouldReturnCorrectResult() 70 | { 71 | var result = _fixture.Collection 72 | .ExeSpec(_value1ShouldBe2.OrderBy(_orderValue2Asc)) 73 | .ToList(); 74 | 75 | result.Count.Should().Be(2); 76 | result[0].Value1.Should().Be(2); 77 | result[0].Value2.Should().Be(1); 78 | result[1].Value1.Should().Be(2); 79 | result[1].Value2.Should().Be(2); 80 | } 81 | 82 | [Fact] 83 | public void ExeSpecSkipSort_IQueryable_ShouldReturnCorrectResult() 84 | { 85 | var result = _fixture.Query 86 | .ExeSpec(_value1ShouldBe2.OrderBy(_orderValue2Asc), true) 87 | .ToList(); 88 | 89 | result.Count.Should().Be(2); 90 | result[0].Value1.Should().Be(2); 91 | result[0].Value2.Should().Be(2); 92 | result[1].Value1.Should().Be(2); 93 | result[1].Value2.Should().Be(1); 94 | } 95 | 96 | [Fact] 97 | public void ExeSpecSkipSort_IEnumerable_ShouldReturnCorrectResult() 98 | { 99 | var result = _fixture.Collection 100 | .ExeSpec(_value1ShouldBe2.OrderBy(_orderValue2Asc), true) 101 | .ToList(); 102 | 103 | result.Count.Should().Be(2); 104 | result[0].Value1.Should().Be(2); 105 | result[0].Value2.Should().Be(2); 106 | result[1].Value1.Should().Be(2); 107 | result[1].Value2.Should().Be(1); 108 | } 109 | 110 | [Fact] 111 | public void ExeSpecOnlySort_IQueryable_ShouldReturnCorrectResult() 112 | { 113 | var result = _fixture.Query 114 | .ExeSpec(_orderValue1Asc) 115 | .ToList(); 116 | 117 | result.Count.Should().Be(4); 118 | result[0].Value1.Should().Be(1); 119 | result[1].Value1.Should().Be(2); 120 | result[1].Value2.Should().Be(2); 121 | result[2].Value1.Should().Be(2); 122 | result[2].Value2.Should().Be(1); 123 | result[3].Value1.Should().Be(3); 124 | } 125 | 126 | [Fact] 127 | public void ExeSpecOnlySort_IEnumerable_ShouldReturnCorrectResult() 128 | { 129 | var result = _fixture.Collection 130 | .ExeSpec(_orderValue1Asc) 131 | .ToList(); 132 | 133 | result.Count.Should().Be(4); 134 | result[0].Value1.Should().Be(1); 135 | result[1].Value1.Should().Be(2); 136 | result[1].Value2.Should().Be(2); 137 | result[2].Value1.Should().Be(2); 138 | result[2].Value2.Should().Be(1); 139 | result[3].Value1.Should().Be(3); 140 | } 141 | 142 | [Fact] 143 | public void ExeSpecMultipleSort_IQueryable_ShouldReturnCorrectResult() 144 | { 145 | var result = _fixture.Query 146 | .ExeSpec(_orderValue1Asc.ThenBy(_orderValue2Asc)) 147 | .ToList(); 148 | 149 | result.Count.Should().Be(4); 150 | result[0].Value1.Should().Be(1); 151 | result[1].Value1.Should().Be(2); 152 | result[1].Value2.Should().Be(1); 153 | result[2].Value1.Should().Be(2); 154 | result[2].Value2.Should().Be(2); 155 | result[3].Value1.Should().Be(3); 156 | } 157 | 158 | [Fact] 159 | public void ExeSpecMultipleSort_IEnumerable_ShouldReturnCorrectResult() 160 | { 161 | var result = _fixture.Collection 162 | .ExeSpec(_orderValue1Asc.ThenBy(_orderValue2Asc)) 163 | .ToList(); 164 | 165 | result.Count.Should().Be(4); 166 | result[0].Value1.Should().Be(1); 167 | result[1].Value1.Should().Be(2); 168 | result[1].Value2.Should().Be(1); 169 | result[2].Value1.Should().Be(2); 170 | result[2].Value2.Should().Be(2); 171 | result[3].Value1.Should().Be(3); 172 | } 173 | 174 | [Fact] 175 | public void ExeSpecSkipTake_IQueryable_ShouldReturnCorrectResult() 176 | { 177 | var result = _fixture.Query 178 | .ExeSpec(_orderValue1Asc.Skip(1).Take(2)) 179 | .ToList(); 180 | 181 | result.Count.Should().Be(2); 182 | result[0].Value1.Should().Be(2); 183 | result[0].Value2.Should().Be(2); 184 | result[1].Value1.Should().Be(2); 185 | result[1].Value2.Should().Be(1); 186 | } 187 | 188 | [Fact] 189 | public void ExeSpecSkipTake_IEnumerable_ShouldReturnCorrectResult() 190 | { 191 | var result = _fixture.Collection 192 | .ExeSpec(_orderValue1Asc.Skip(1).Take(2)) 193 | .ToList(); 194 | 195 | result.Count.Should().Be(2); 196 | result[0].Value1.Should().Be(2); 197 | result[0].Value2.Should().Be(2); 198 | result[1].Value1.Should().Be(2); 199 | result[1].Value2.Should().Be(1); 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /src/LinqBuilder/README.md: -------------------------------------------------------------------------------- 1 | # LinqBuilder 2 | 3 | ## Table of Contents 4 | 1. [LinqBuilder Specifications](#linqbuilder-specifications) 5 | 2. [LinqBuilder OrderSpecifications](#linqbuilder-orderspecifications) 6 | 7 | ## LinqBuilder Specifications 8 | 9 | ### Usage 10 | Specifications can be constructed in three different ways. 11 | 12 | **By extending Specification:** 13 | ```csharp 14 | public class FirstnameIsFoo : Specification 15 | { 16 | public override Expression> AsExpression() 17 | { 18 | return person => person.Firstname == "Foo"; 19 | } 20 | } 21 | 22 | ISpecification firstnameIsFoo = new FirstnameIsFoo(); 23 | ``` 24 | 25 | **By extending DynamicSpecification:** 26 | ```csharp 27 | public class FirstnameIs : DynamicSpecification 28 | { 29 | public override Expression> AsExpression() 30 | { 31 | return person => person.Firstname == Value; 32 | } 33 | } 34 | 35 | ISpecification firstnameIsFoo = new FirstnameIs().Set("Foo"); 36 | ``` 37 | 38 | **By extending MultiSpecification:** 39 | ```csharp 40 | public class FirstnameIsFoo : MultiSpecification 41 | { 42 | public override Expression> AsExpressionForEntity1() 43 | { 44 | return person => person.Firstname == "Foo"; 45 | } 46 | 47 | public override Expression> AsExpressionForEntity2() 48 | { 49 | return person => person.Firstname == "Foo"; 50 | } 51 | } 52 | 53 | ISpecification firstnameIsFoo = new FirstnameIsFoo(); // First generic is default 54 | ISpecification firstnameIsFoo = new FirstnameIsFoo().For(); 55 | ISpecification firstnameIsFoo = new FirstnameIsFoo().For(); 56 | ``` 57 | 58 | **By static New method:** 59 | ```csharp 60 | ISpecification firstnameIsFoo = Specification.New(p => p.Firstname == "Foo"); 61 | // Or by alias 62 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 63 | ``` 64 | 65 | ### Example 66 | ```csharp 67 | var collection = new List() { ... }; 68 | 69 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 70 | ISpecification firstnameIsBar = Spec.New(p => p.Firstname == "Bar"); 71 | 72 | ISpecification specification = firstnameIsFoo.Or(firstnameIsBar); 73 | 74 | var result = collection.ExeSpec(specification).ToList(); 75 | // result = Collection items satisfied by specification 76 | ``` 77 | The extension ```ExeSpec``` allows all types of ```ISpecification``` to be executed on ```IQueryable``` and ```IEnumerable```. 78 | 79 | ### Methods 80 | ```csharp 81 | ISpecification specification = Spec.All( 82 | new SomeSpecification(), 83 | new SomeOtherSpecification(), 84 | ... 85 | ); 86 | 87 | ISpecification specification = Spec.None( 88 | new SomeSpecification(), 89 | new SomeOtherSpecification(), 90 | ... 91 | ); 92 | 93 | ISpecification specification = Spec.Any( 94 | new SomeSpecification(), 95 | new SomeOtherSpecification(), 96 | ... 97 | ); 98 | ``` 99 | 100 | ### Extensions 101 | ```csharp 102 | ISpecification And(this ISpecification left, ISpecification right); 103 | ISpecification Or(this ISpecification left, ISpecification right); 104 | ISpecification Not(this ISpecification specification); 105 | bool IsSatisfiedBy(this ISpecification specification, TEntity entity); 106 | ISpecification Clone(this ISpecification specification); 107 | ``` 108 | 109 | **LinqBuilder** also extends the following extensions to support ```ISpecification``` on ```IQueryable``` and ```IEnumerable```. 110 | ```csharp 111 | IEnumerable collection = collection.Where(specification); 112 | bool result = collection.Any(specification); 113 | bool result = collection.All(specification); 114 | int result = collection.Count(specification); 115 | Entity result = collection.First(specification); 116 | Entity result = collection.FirstOrDefault(specification); 117 | Entity result = collection.Single(specification); 118 | Entity result = collection.SingleOrDefault(specification); 119 | ``` 120 | 121 | ## LinqBuilder OrderSpecifications 122 | 123 | ### Usage 124 | Order specifications can be constructed in almost the same way as regular specifications. 125 | 126 | **By extending OrderSpecification:** 127 | ```csharp 128 | public class FirstnameDescending : OrderSpecification 129 | { 130 | public DescNumberOrderSpecification() : base(Sort.Descending) { } 131 | 132 | public override Expression> AsExpression() 133 | { 134 | return person => person.Firstname; 135 | } 136 | } 137 | 138 | ISpecification firstnameDescending = new FirstnameDescending(); 139 | ``` 140 | 141 | **By static New method:** 142 | ```csharp 143 | ISpecification firstnameDescending = OrderSpecification.New(p => p.Firstname, Sort.Descending); 144 | // Or by alias 145 | ISpecification firstnameDescending = OrderSpec.New(p => p.Firstname, Sort.Descending); 146 | ``` 147 | 148 | ### Example 149 | ```csharp 150 | var collection = new List() { ... }; 151 | 152 | ISpecification firstnameDescending = OrderSpec.New(p => p.Firstname, Sort.Descending); 153 | ISpecification lastnameDescending = OrderSpec.New(p => p.Lastname, Sort.Descending); 154 | 155 | ISpecification specification = firstnameDescending.ThenBy(lastnameDescending); 156 | 157 | var result = collection.ExeSpec(specification).ToList(); 158 | // result = Collection ordered by descending number, then by other number 159 | ``` 160 | 161 | ### Methods 162 | ```csharp 163 | ISpecification specification = OrderSpec.New(p => p.Firstname) 164 | .Take(10); 165 | 166 | ISpecification specification = OrderSpec.New(p => p.Firstname) 167 | .Skip(5); 168 | 169 | ISpecification specification = OrderSpec.New(p => p.Firstname) 170 | .Paginate(2, 10); // Equals .Skip((2 - 1) * 10).Take(10) 171 | ``` 172 | 173 | ### Extensions 174 | ```csharp 175 | IOrderedEnumerable collection = collection 176 | .OrderBy(specification); 177 | .ThenBy(otherSpecification); 178 | ``` 179 | 180 | Order specifications can also be chained with regular LinqBuilder specifications. 181 | ```csharp 182 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 183 | ISpecification firstnameAscending = OrderSpec.New(p => p.Firstname); 184 | 185 | ISpecification specification = firstnameIsFoo.OrderBy(firstnameAscending); 186 | ``` 187 | 188 | Chained ```OrderSpecification```'s can also be attatched to a specification later. 189 | ```csharp 190 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 191 | ISpecification firstnameAscending = OrderSpec.New(p => p.Firstname); 192 | ISpecification lastnameAscending = OrderSpec.New(p => p.Firstname); 193 | 194 | ISpecification orderSpecification = firstnameAscending.ThenBy(lastnameAscending); 195 | 196 | ISpecification specification = firstnameIsFoo.UseOrdering(orderSpecification); 197 | ``` 198 | 199 | The following extensions will help to check what kind of ordering is applied. 200 | ```csharp 201 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 202 | ISpecification firstnameAscending = OrderSpec.New(p => p.Firstname); 203 | 204 | firstnameIsFoo.IsOrdered(); // Returns false 205 | 206 | ISpecification specification = firstnameIsFoo.OrderBy(firstnameAscending); 207 | specification.IsOrdered(); // Returns true 208 | ``` 209 | ```csharp 210 | ISpecification specification = Spec.New(p => p.Firstname == "Foo"); 211 | specification.HasSkip(); // Returns false 212 | 213 | ISpecification specification = specification.Skip(10); 214 | specification.HasSkip(); // Returns true 215 | ``` 216 | ```csharp 217 | ISpecification specification = Spec.New(p => p.Firstname == "Foo"); 218 | specification.HasTake(); // Returns false 219 | 220 | ISpecification specification = specification.Take(10); 221 | specification.HasTake(); // Returns true 222 | ``` 223 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/OrderBy/SpecificationExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using FluentAssertions; 4 | using LinqBuilder.OrderBy; 5 | using LinqBuilder.Tests.Data; 6 | using Xunit; 7 | 8 | namespace LinqBuilder.Tests.OrderBy; 9 | 10 | public class SpecificationExtensionsTests 11 | { 12 | private readonly ISpecification _emptySpecification = Spec.New(); 13 | private readonly ISpecification _value1ShouldBe1 = Spec.New(entity => entity.Value1 == 1); 14 | private readonly IOrderSpecification _orderValue1Asc = OrderSpec.New(entity => entity.Value1); 15 | private readonly IOrderSpecification _orderValue2Asc = OrderSpec.New(entity => entity.Value2); 16 | 17 | private readonly Fixture _fixture; 18 | 19 | public SpecificationExtensionsTests() 20 | { 21 | _fixture = new Fixture(); 22 | _fixture.AddToCollection(3, 1, 1); 23 | _fixture.AddToCollection(1, 1, 1); 24 | _fixture.AddToCollection(2, 2, 1); 25 | _fixture.AddToCollection(2, 1, 1); 26 | } 27 | 28 | [Fact] 29 | public void OrderBy_IQueryable_ShouldReturnCorrectResult() 30 | { 31 | var specification = _emptySpecification.OrderBy(_orderValue1Asc); 32 | var result = _fixture.Query.ExeSpec(specification).ToList(); 33 | result.Count.Should().Be(4); 34 | result[0].Value1.Should().Be(1); 35 | result[1].Value1.Should().Be(2); 36 | result[2].Value1.Should().Be(2); 37 | result[3].Value1.Should().Be(3); 38 | } 39 | 40 | [Fact] 41 | public void OrderBy_IEnumerable_ShouldReturnCorrectResult() 42 | { 43 | var specification = _emptySpecification.OrderBy(_orderValue1Asc); 44 | var result = _fixture.Collection.ExeSpec(specification).ToList(); 45 | result.Count.Should().Be(4); 46 | result[0].Value1.Should().Be(1); 47 | result[1].Value1.Should().Be(2); 48 | result[2].Value1.Should().Be(2); 49 | result[3].Value1.Should().Be(3); 50 | } 51 | 52 | [Fact] 53 | public void ThenBy_IQueryable_ShouldReturnCorrectResult() 54 | { 55 | var specification = _emptySpecification.OrderBy(_orderValue1Asc).ThenBy(_orderValue2Asc); 56 | var result = _fixture.Query.ExeSpec(specification).ToList(); 57 | result.Count.Should().Be(4); 58 | result[0].Value1.Should().Be(1); 59 | result[1].Value1.Should().Be(2); 60 | result[1].Value2.Should().Be(1); 61 | result[2].Value1.Should().Be(2); 62 | result[2].Value2.Should().Be(2); 63 | result[3].Value1.Should().Be(3); 64 | } 65 | 66 | [Fact] 67 | public void ThenBy_IEnumerable_ShouldReturnCorrectResult() 68 | { 69 | var specification = _emptySpecification.OrderBy(_orderValue1Asc).ThenBy(_orderValue2Asc); 70 | var result = _fixture.Collection.ExeSpec(specification).ToList(); 71 | result.Count.Should().Be(4); 72 | result[0].Value1.Should().Be(1); 73 | result[1].Value2.Should().Be(1); 74 | result[2].Value1.Should().Be(2); 75 | result[2].Value2.Should().Be(2); 76 | result[3].Value1.Should().Be(3); 77 | } 78 | 79 | [Fact] 80 | public void ThenByOrdered_IQueryable_ShouldReturnCorrectResult() 81 | { 82 | var specification = _orderValue1Asc.ThenBy(_orderValue2Asc); 83 | var result = _fixture.Query.ExeSpec(specification).ToList(); 84 | result.Count.Should().Be(4); 85 | result[0].Value1.Should().Be(1); 86 | result[1].Value1.Should().Be(2); 87 | result[1].Value2.Should().Be(1); 88 | result[2].Value1.Should().Be(2); 89 | result[2].Value2.Should().Be(2); 90 | result[3].Value1.Should().Be(3); 91 | } 92 | 93 | [Fact] 94 | public void ThenByOrdered_IEnumerable_ShouldReturnCorrectResult() 95 | { 96 | var specification = _orderValue1Asc.ThenBy(_orderValue2Asc); 97 | var result = _fixture.Collection.ExeSpec(specification).ToList(); 98 | result.Count.Should().Be(4); 99 | result[0].Value1.Should().Be(1); 100 | result[1].Value1.Should().Be(2); 101 | result[1].Value2.Should().Be(1); 102 | result[2].Value1.Should().Be(2); 103 | result[2].Value2.Should().Be(2); 104 | result[3].Value1.Should().Be(3); 105 | } 106 | 107 | [Fact] 108 | public void UseOrdering_IQueryable_ShouldReturnCorrectResult() 109 | { 110 | var ordering = _orderValue1Asc.Skip(1).Take(1); 111 | var specification = _emptySpecification.UseOrdering(ordering); 112 | var result = _fixture.Query.ExeSpec(specification).ToList(); 113 | result.Count.Should().Be(1); 114 | result[0].Value1.Should().Be(2); 115 | } 116 | 117 | [Fact] 118 | public void UseOrdering_IEnumerable_ShouldReturnCorrectResult() 119 | { 120 | var ordering = _orderValue1Asc.Skip(1).Take(1); 121 | var specification = _emptySpecification.UseOrdering(ordering); 122 | var result = _fixture.Collection.ExeSpec(specification).ToList(); 123 | result.Count.Should().Be(1); 124 | result[0].Value1.Should().Be(2); 125 | } 126 | 127 | [Fact] 128 | public void Skip_IQueryable_ShouldReturnCorrectResult() 129 | { 130 | var result = _fixture.Query.ExeSpec(_orderValue1Asc.Skip(1)).ToList(); 131 | result.Count.Should().Be(3); 132 | result[0].Value1.Should().Be(2); 133 | result[1].Value1.Should().Be(2); 134 | result[2].Value1.Should().Be(3); 135 | } 136 | 137 | [Fact] 138 | public void Skip_IEnumerable_ShouldReturnCorrectResult() 139 | { 140 | var result = _fixture.Collection.ExeSpec(_orderValue1Asc.Skip(1)).ToList(); 141 | result.Count.Should().Be(3); 142 | result[0].Value1.Should().Be(2); 143 | result[1].Value1.Should().Be(2); 144 | result[2].Value1.Should().Be(3); 145 | } 146 | 147 | [Fact] 148 | public void Take_IQueryable_ShouldReturnCorrectResult() 149 | { 150 | var result = _fixture.Query.ExeSpec(_orderValue1Asc.Take(2)).ToList(); 151 | result.Count.Should().Be(2); 152 | result[0].Value1.Should().Be(1); 153 | result[1].Value1.Should().Be(2); 154 | } 155 | 156 | [Fact] 157 | public void Take_IEnumerable_ShouldReturnCorrectResult() 158 | { 159 | var result = _fixture.Collection.ExeSpec(_orderValue1Asc.Take(2)).ToList(); 160 | result.Count.Should().Be(2); 161 | result[0].Value1.Should().Be(1); 162 | result[1].Value1.Should().Be(2); 163 | } 164 | 165 | [Fact] 166 | public void Paginate_PageNoAndSize_ShouldHaveCorrectValues() 167 | { 168 | var configuration = _orderValue1Asc.Paginate(2, 5).Internal; 169 | configuration.OrderSpecifications.Count.Should().Be(1); 170 | configuration.Skip.Should().Be(5); 171 | configuration.Take.Should().Be(5); 172 | } 173 | 174 | [Fact] 175 | public void Paginate_InvalidPageNo_ShouldThrowArgumentException() 176 | { 177 | Action act = () => _orderValue1Asc.Paginate(0, 5); 178 | 179 | act.Should().Throw(); 180 | } 181 | 182 | [Fact] 183 | public void Paginate_InvalidPageSize_ShouldThrowArgumentException() 184 | { 185 | Action act = () => _orderValue1Asc.Paginate(1, 0); 186 | 187 | act.Should().Throw(); 188 | } 189 | 190 | [Fact] 191 | public void IsOrdered_OrderedSpecification_ShouldBeTrue() 192 | { 193 | ISpecification specification = _value1ShouldBe1.OrderBy(_orderValue1Asc); 194 | specification 195 | .IsOrdered() 196 | .Should().BeTrue(); 197 | } 198 | 199 | [Fact] 200 | public void IsOrdered_Specification_ShouldBeTrue() 201 | { 202 | _orderValue1Asc 203 | .IsOrdered() 204 | .Should().BeTrue(); 205 | } 206 | 207 | [Fact] 208 | public void IsOrdered_Specification_ShouldBeFalse() 209 | { 210 | _value1ShouldBe1 211 | .IsOrdered() 212 | .Should().BeFalse(); 213 | } 214 | 215 | [Fact] 216 | public void HasSkip_Specification_ShouldBeTrue() 217 | { 218 | _value1ShouldBe1 219 | .Skip(10) 220 | .HasSkip() 221 | .Should().BeTrue(); 222 | } 223 | 224 | [Fact] 225 | public void HasSkip_Specification_ShouldBeFalse() 226 | { 227 | _value1ShouldBe1 228 | .HasSkip() 229 | .Should().BeFalse(); 230 | } 231 | 232 | [Fact] 233 | public void HasTake_Specification_ShouldBeTrue() 234 | { 235 | _value1ShouldBe1 236 | .Take(10) 237 | .HasTake() 238 | .Should().BeTrue(); 239 | } 240 | 241 | [Fact] 242 | public void HasTake_Specification_ShouldBeFalse() 243 | { 244 | _value1ShouldBe1 245 | .HasTake() 246 | .Should().BeFalse(); 247 | } 248 | 249 | [Fact] 250 | public void Clone_Specifications_ShouldNotBeEqual() 251 | { 252 | var spec1 = _orderValue1Asc.Paginate(2, 10); 253 | var spec2 = spec1.Clone(); 254 | spec1.Should().NotBe(spec2); 255 | spec1.Internal.QuerySpecification.Should().Be(spec2.Internal.QuerySpecification); 256 | spec1.Internal.OrderSpecifications.Should().Equal(spec2.Internal.OrderSpecifications); 257 | spec1.Internal.Skip.Should().Be(spec2.Internal.Skip); 258 | spec1.Internal.Take.Should().Be(spec2.Internal.Take); 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /src/LinqBuilder.Tests/LinqExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using FluentAssertions; 4 | using LinqBuilder.Tests.Data; 5 | using Xunit; 6 | 7 | namespace LinqBuilder.Tests; 8 | 9 | public class LinqExtensionsTests 10 | { 11 | private readonly ISpecification _emptySpecification = Spec.New(); 12 | private readonly ISpecification _value1ShouldBe1 = Spec.New(entity => entity.Value1 == 1); 13 | private readonly ISpecification _value1ShouldBe3 = Spec.New(entity => entity.Value1 == 3); 14 | private readonly ISpecification _value1ShouldBe4 = Spec.New(entity => entity.Value1 == 4); 15 | private readonly ISpecification _value2ShouldBe1 = Spec.New(entity => entity.Value2 == 1); 16 | 17 | private readonly Fixture _fixture; 18 | 19 | public LinqExtensionsTests() 20 | { 21 | _fixture = new Fixture(); 22 | _fixture.AddToCollection(3, 1); 23 | _fixture.AddToCollection(3, 1); 24 | _fixture.AddToCollection(1, 1); 25 | } 26 | 27 | [Fact] 28 | public void Where_IQueryable_ShouldReturnCorrectResult() 29 | { 30 | var result = _fixture.Query.Where(_value1ShouldBe3); 31 | result.Should().BeAssignableTo>(); 32 | result.Should().AllSatisfy(e => e.Value1.Should().Be(3)); 33 | } 34 | 35 | [Fact] 36 | public void Where_IEnumerable_ShouldReturnCorrectResult() 37 | { 38 | var result = _fixture.Collection.Where(_value1ShouldBe3).ToList(); 39 | result.Should().NotBeAssignableTo>(); 40 | result.Should().AllSatisfy(e => e.Value1.Should().Be(3)); 41 | } 42 | 43 | [Fact] 44 | public void WhereEmpty_IQueryable_ShouldReturnEqualQuery() 45 | { 46 | var result = _fixture.Query.Where(_emptySpecification); 47 | result.Should().BeAssignableTo>(); 48 | result.Should().Equal(_fixture.Query); 49 | } 50 | 51 | [Fact] 52 | public void WhereEmpty_IEnumerable_ShouldReturnEqualCollection() 53 | { 54 | var result = _fixture.Collection.Where(_emptySpecification).ToList(); 55 | result.Should().NotBeAssignableTo>(); 56 | result.Should().Equal(_fixture.Collection); 57 | } 58 | 59 | [Fact] 60 | public void Any_IQueryable_ShouldBeTrue() 61 | { 62 | _fixture.Query 63 | .Any(_value1ShouldBe3) 64 | .Should().BeTrue(); 65 | } 66 | 67 | [Fact] 68 | public void Any_IQueryable_ShouldBeFalse() 69 | { 70 | _fixture.Query 71 | .Any(_value1ShouldBe4) 72 | .Should().BeFalse(); 73 | } 74 | 75 | [Fact] 76 | public void Any_IEnumerable_ShouldBeTrue() 77 | { 78 | _fixture.Collection 79 | .Any(_value1ShouldBe3) 80 | .Should().BeTrue(); 81 | } 82 | 83 | [Fact] 84 | public void Any_IEnumerable_ShouldBeFalse() 85 | { 86 | _fixture.Collection 87 | .Any(_value1ShouldBe4) 88 | .Should().BeFalse(); 89 | } 90 | 91 | [Fact] 92 | public void AnyEmpty_IQueryable_ShouldBeTrue() 93 | { 94 | _fixture.Query 95 | .Any(_emptySpecification) 96 | .Should().BeTrue(); 97 | } 98 | 99 | [Fact] 100 | public void AnyEmpty_IEnumerable_ShouldBeTrue() 101 | { 102 | _fixture.Collection 103 | .Any(_emptySpecification) 104 | .Should().BeTrue(); 105 | } 106 | 107 | [Fact] 108 | public void All_IQueryable_ShouldBeTrue() 109 | { 110 | _fixture.Query 111 | .All(_value2ShouldBe1) 112 | .Should().BeTrue(); 113 | } 114 | 115 | [Fact] 116 | public void All_IQueryable_ShouldBeFalse() 117 | { 118 | _fixture.Query 119 | .All(_value1ShouldBe3) 120 | .Should().BeFalse(); 121 | } 122 | 123 | [Fact] 124 | public void All_IEnumerable_ShouldBeTrue() 125 | { 126 | _fixture.Collection 127 | .All(_value2ShouldBe1) 128 | .Should().BeTrue(); 129 | } 130 | 131 | [Fact] 132 | public void All_IEnumerable_ShouldBeFalse() 133 | { 134 | _fixture.Collection 135 | .All(_value1ShouldBe3) 136 | .Should().BeFalse(); 137 | } 138 | 139 | [Fact] 140 | public void AllEmpty_IQueryable_ShouldBeTrue() 141 | { 142 | _fixture.Query 143 | .All(_emptySpecification) 144 | .Should().BeTrue(); 145 | } 146 | 147 | [Fact] 148 | public void AllEmpty_IEnumerable_ShouldBeTrue() 149 | { 150 | _fixture.Collection 151 | .All(_emptySpecification) 152 | .Should().BeTrue(); 153 | } 154 | 155 | [Fact] 156 | public void Count_IQueryable_ShouldReturnCorrectResult() 157 | { 158 | _fixture.Query 159 | .Count(_value1ShouldBe3) 160 | .Should().Be(2); 161 | } 162 | 163 | [Fact] 164 | public void CountEmpty_IQueryable_ShouldReturnCorrectResult() 165 | { 166 | _fixture.Query 167 | .Count(_emptySpecification) 168 | .Should().Be(_fixture.Collection.Count()); 169 | } 170 | 171 | [Fact] 172 | public void Count_IEnumerable_ShouldReturnCorrectResult() 173 | { 174 | _fixture.Collection 175 | .Count(_value1ShouldBe3) 176 | .Should().Be(2); 177 | } 178 | 179 | [Fact] 180 | public void CountEmpty_IEnumerable_ShouldReturnEqualCount() 181 | { 182 | _fixture.Collection 183 | .Count(_emptySpecification) 184 | .Should().Be(_fixture.Collection.Count()); 185 | } 186 | 187 | [Fact] 188 | public void First_IQueryable_ShouldReturnCorrectResult() 189 | { 190 | _fixture.Query 191 | .First(_value1ShouldBe3) 192 | .Should().Be(_fixture.Store[0]); 193 | } 194 | 195 | [Fact] 196 | public void FirstEmpty_IQueryable_ShouldReturnCorrectResult() 197 | { 198 | _fixture.Query 199 | .First(_emptySpecification) 200 | .Should().Be(_fixture.Store[0]); 201 | } 202 | 203 | [Fact] 204 | public void First_IEnumerable_ShouldReturnCorrectResult() 205 | { 206 | _fixture.Collection 207 | .First(_value1ShouldBe3) 208 | .Should().Be(_fixture.Store[0]); 209 | } 210 | 211 | [Fact] 212 | public void FirstEmpty_IEnumerable_ShouldReturnCorrectResult() 213 | { 214 | _fixture.Collection 215 | .First(_emptySpecification) 216 | .Should().Be(_fixture.Store[0]); 217 | } 218 | 219 | [Fact] 220 | public void FirstOrDefault_IQueryable_ShouldReturnCorrectResult() 221 | { 222 | _fixture.Query 223 | .FirstOrDefault(_value1ShouldBe3) 224 | .Should().Be(_fixture.Store[0]); 225 | } 226 | 227 | [Fact] 228 | public void FirstOrDefaultEmpty_IQueryable_ShouldReturnCorrectResult() 229 | { 230 | _fixture.Query 231 | .FirstOrDefault(_emptySpecification) 232 | .Should().Be(_fixture.Store[0]); 233 | } 234 | 235 | [Fact] 236 | public void FirstOrDefault_IEnumerable_ShouldReturnCorrectResult() 237 | { 238 | _fixture.Collection 239 | .FirstOrDefault(_value1ShouldBe3) 240 | .Should().Be(_fixture.Store[0]); 241 | } 242 | 243 | [Fact] 244 | public void FirstOrDefaultEmpty_IEnumerable_ShouldReturnCorrectResult() 245 | { 246 | _fixture.Collection 247 | .FirstOrDefault(_emptySpecification) 248 | .Should().Be(_fixture.Store[0]); 249 | } 250 | 251 | [Fact] 252 | public void Single_IQueryable_ShouldReturnCorrectResult() 253 | { 254 | _fixture.Query 255 | .Single(_value1ShouldBe1) 256 | .Should().Be(_fixture.Store[2]); 257 | } 258 | 259 | [Fact] 260 | public void SingleEmpty_IQueryable_ShouldReturnCorrectResult() 261 | { 262 | Action act = () => _fixture.Query.Single(_emptySpecification); 263 | 264 | act.Should().Throw(); 265 | } 266 | 267 | [Fact] 268 | public void Single_IEnumerable_ShouldReturnCorrectResult() 269 | { 270 | _fixture.Collection 271 | .Single(_value1ShouldBe1) 272 | .Should().Be(_fixture.Store[2]); 273 | } 274 | 275 | [Fact] 276 | public void SingleEmpty_IEnumerable_ShouldReturnCorrectResult() 277 | { 278 | Action act = () => _fixture.Collection.Single(_emptySpecification); 279 | 280 | act.Should().Throw(); 281 | } 282 | 283 | [Fact] 284 | public void SingleOrDefault_IQueryable_ShouldReturnCorrectResult() 285 | { 286 | _fixture.Query 287 | .SingleOrDefault(_value1ShouldBe1) 288 | .Should().Be(_fixture.Store[2]); 289 | } 290 | 291 | [Fact] 292 | public void SingleOrDefaultEmpty_IQueryable_ShouldReturnCorrectResult() 293 | { 294 | Action act = () => _fixture.Query.SingleOrDefault(_emptySpecification); 295 | 296 | act.Should().Throw(); 297 | } 298 | 299 | [Fact] 300 | public void SingleOrDefault_IEnumerable_ShouldReturnCorrectResult() 301 | { 302 | _fixture.Collection 303 | .SingleOrDefault(_value1ShouldBe1) 304 | .Should().Be(_fixture.Store[2]); 305 | } 306 | 307 | [Fact] 308 | public void SingleOrDefaultEmpty_IEnumerable_ShouldReturnCorrectResult() 309 | { 310 | Action act = () => _fixture.Collection.SingleOrDefault(_emptySpecification); 311 | 312 | act.Should().Throw(); 313 | } 314 | } 315 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LinqBuilder 2 | [![Build status](https://ci.appveyor.com/api/projects/status/v0t8rfsv0d4q3hap?svg=true)](https://ci.appveyor.com/project/Baune8D/linqbuilder) 3 | [![codecov](https://codecov.io/gh/Baune8D/linqbuilder/branch/main/graph/badge.svg)](https://codecov.io/gh/Baune8D/linqbuilder) 4 | [![NuGet Badge](https://buildstats.info/nuget/LinqBuilder)](https://www.nuget.org/packages/LinqBuilder) 5 | 6 | **Available on NuGet:** [https://www.nuget.org/packages/LinqBuilder/](https://www.nuget.org/packages/LinqBuilder/) 7 | **MyGet development feed:** [https://www.myget.org/F/baunegaard/api/v3/index.json](https://www.myget.org/F/baunegaard/api/v3/index.json) 8 | 9 | LinqBuilder is based on the specification pattern. 10 | 11 | ## Table of Contents 12 | 1. [LinqBuilder Specifications](#linqbuilder-specifications) 13 | 2. [LinqBuilder OrderSpecifications](#linqbuilder-orderspecifications) 14 | 3. [LinqBuilder.EFCore / LinqBuilder.EF6](#linqbuilderefcore--linqbuilderef6) 15 | 4. [Full Example](#full-example) 16 | 17 | ## LinqBuilder Specifications 18 | 19 | ### Usage 20 | Specifications can be constructed in three different ways. 21 | 22 | **By extending Specification:** 23 | ```csharp 24 | public class FirstnameIsFoo : Specification 25 | { 26 | public override Expression> AsExpression() 27 | { 28 | return person => person.Firstname == "Foo"; 29 | } 30 | } 31 | 32 | ISpecification firstnameIsFoo = new FirstnameIsFoo(); 33 | ``` 34 | 35 | **By extending DynamicSpecification:** 36 | ```csharp 37 | public class FirstnameIs : DynamicSpecification 38 | { 39 | public override Expression> AsExpression() 40 | { 41 | return person => person.Firstname == Value; 42 | } 43 | } 44 | 45 | ISpecification firstnameIsFoo = new FirstnameIs().Set("Foo"); 46 | ``` 47 | 48 | **By extending MultiSpecification:** 49 | ```csharp 50 | public class FirstnameIsFoo : MultiSpecification 51 | { 52 | public override Expression> AsExpressionForEntity1() 53 | { 54 | return person => person.Firstname == "Foo"; 55 | } 56 | 57 | public override Expression> AsExpressionForEntity2() 58 | { 59 | return person => person.Firstname == "Foo"; 60 | } 61 | } 62 | 63 | ISpecification firstnameIsFoo = new FirstnameIsFoo(); // First generic is default 64 | ISpecification firstnameIsFoo = new FirstnameIsFoo().For(); 65 | ISpecification firstnameIsFoo = new FirstnameIsFoo().For(); 66 | ``` 67 | 68 | **By static New method:** 69 | ```csharp 70 | ISpecification firstnameIsFoo = Specification.New(p => p.Firstname == "Foo"); 71 | // Or by alias 72 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 73 | ``` 74 | 75 | ### Example 76 | ```csharp 77 | var collection = new List() { ... }; 78 | 79 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 80 | ISpecification firstnameIsBar = Spec.New(p => p.Firstname == "Bar"); 81 | 82 | ISpecification specification = firstnameIsFoo.Or(firstnameIsBar); 83 | 84 | var result = collection.ExeSpec(specification).ToList(); 85 | // result = Collection items satisfied by specification 86 | ``` 87 | The extension ```ExeSpec``` allows all types of ```ISpecification``` to be executed on ```IQueryable``` and ```IEnumerable```. 88 | 89 | ### Methods 90 | ```csharp 91 | ISpecification specification = Spec.All( 92 | new SomeSpecification(), 93 | new SomeOtherSpecification(), 94 | ... 95 | ); 96 | 97 | ISpecification specification = Spec.None( 98 | new SomeSpecification(), 99 | new SomeOtherSpecification(), 100 | ... 101 | ); 102 | 103 | ISpecification specification = Spec.Any( 104 | new SomeSpecification(), 105 | new SomeOtherSpecification(), 106 | ... 107 | ); 108 | ``` 109 | 110 | ### Extensions 111 | ```csharp 112 | ISpecification And(this ISpecification left, ISpecification right); 113 | ISpecification Or(this ISpecification left, ISpecification right); 114 | ISpecification Not(this ISpecification specification); 115 | bool IsSatisfiedBy(this ISpecification specification, TEntity entity); 116 | ISpecification Clone(this ISpecification specification); 117 | ``` 118 | 119 | **LinqBuilder** also extends the following extensions to support ```ISpecification``` on ```IQueryable``` and ```IEnumerable```. 120 | ```csharp 121 | IEnumerable collection = collection.Where(specification); 122 | bool result = collection.Any(specification); 123 | bool result = collection.All(specification); 124 | int result = collection.Count(specification); 125 | Entity result = collection.First(specification); 126 | Entity result = collection.FirstOrDefault(specification); 127 | Entity result = collection.Single(specification); 128 | Entity result = collection.SingleOrDefault(specification); 129 | ``` 130 | 131 | ## LinqBuilder OrderSpecifications 132 | 133 | ### Usage 134 | Order specifications can be constructed in almost the same way as regular specifications. 135 | 136 | **By extending OrderSpecification:** 137 | ```csharp 138 | public class FirstnameDescending : OrderSpecification 139 | { 140 | public DescNumberOrderSpecification() : base(Sort.Descending) { } 141 | 142 | public override Expression> AsExpression() 143 | { 144 | return person => person.Firstname; 145 | } 146 | } 147 | 148 | ISpecification firstnameDescending = new FirstnameDescending(); 149 | ``` 150 | 151 | **By static New method:** 152 | ```csharp 153 | ISpecification firstnameDescending = OrderSpecification.New(p => p.Firstname, Sort.Descending); 154 | // Or by alias 155 | ISpecification firstnameDescending = OrderSpec.New(p => p.Firstname, Sort.Descending); 156 | ``` 157 | 158 | ### Example 159 | ```csharp 160 | var collection = new List() { ... }; 161 | 162 | ISpecification firstnameDescending = OrderSpec.New(p => p.Firstname, Sort.Descending); 163 | ISpecification lastnameDescending = OrderSpec.New(p => p.Lastname, Sort.Descending); 164 | 165 | ISpecification specification = firstnameDescending.ThenBy(lastnameDescending); 166 | 167 | var result = collection.ExeSpec(specification).ToList(); 168 | // result = Collection ordered by descending number, then by other number 169 | ``` 170 | 171 | ### Methods 172 | ```csharp 173 | ISpecification specification = OrderSpec.New(p => p.Firstname) 174 | .Take(10); 175 | 176 | ISpecification specification = OrderSpec.New(p => p.Firstname) 177 | .Skip(5); 178 | 179 | ISpecification specification = OrderSpec.New(p => p.Firstname) 180 | .Paginate(2, 10); // Equals .Skip((2 - 1) * 10).Take(10) 181 | ``` 182 | 183 | ### Extensions 184 | ```csharp 185 | IOrderedEnumerable collection = collection 186 | .OrderBy(specification); 187 | .ThenBy(otherSpecification); 188 | ``` 189 | 190 | Order specifications can also be chained with regular LinqBuilder specifications. 191 | ```csharp 192 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 193 | ISpecification firstnameAscending = OrderSpec.New(p => p.Firstname); 194 | 195 | ISpecification specification = firstnameIsFoo.OrderBy(firstnameAscending); 196 | ``` 197 | 198 | Chained ```OrderSpecification```'s can also be attatched to a specification later. 199 | ```csharp 200 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 201 | ISpecification firstnameAscending = OrderSpec.New(p => p.Firstname); 202 | ISpecification lastnameAscending = OrderSpec.New(p => p.Firstname); 203 | 204 | ISpecification orderSpecification = firstnameAscending.ThenBy(lastnameAscending); 205 | 206 | ISpecification specification = firstnameIsFoo.UseOrdering(orderSpecification); 207 | ``` 208 | 209 | The following extensions will help to check what kind of ordering is applied. 210 | ```csharp 211 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 212 | ISpecification firstnameAscending = OrderSpec.New(p => p.Firstname); 213 | 214 | firstnameIsFoo.IsOrdered(); // Returns false 215 | 216 | ISpecification specification = firstnameIsFoo.OrderBy(firstnameAscending); 217 | specification.IsOrdered(); // Returns true 218 | ``` 219 | ```csharp 220 | ISpecification specification = Spec.New(p => p.Firstname == "Foo"); 221 | specification.HasSkip(); // Returns false 222 | 223 | ISpecification specification = specification.Skip(10); 224 | specification.HasSkip(); // Returns true 225 | ``` 226 | ```csharp 227 | ISpecification specification = Spec.New(p => p.Firstname == "Foo"); 228 | specification.HasTake(); // Returns false 229 | 230 | ISpecification specification = specification.Take(10); 231 | specification.HasTake(); // Returns true 232 | ``` 233 | 234 | ## LinqBuilder.EFCore / LinqBuilder.EF6 235 | | Package | Version | 236 | | -------------------|:---------------------------------------------------------------------------------------------------------------------:| 237 | | LinqBuilder.EFCore | [![NuGet Badge](https://buildstats.info/nuget/LinqBuilder.EFCore)](https://www.nuget.org/packages/LinqBuilder.EFCore) | 238 | | LinqBuilder.EF6 | [![NuGet Badge](https://buildstats.info/nuget/LinqBuilder.EF6)](https://www.nuget.org/packages/LinqBuilder.EF6) | 239 | 240 | ### Extensions 241 | **LinqBuilder.EF** packages extends the following extensions to support ```ISpecification```. 242 | ```csharp 243 | bool result = await _sampleContext.Entities.AnyAsync(specification); 244 | bool result = await _sampleContext.Entities.AllAsync(specification); 245 | int result = await _sampleContext.Entities.CountAsync(specification); 246 | Entity result = await _sampleContext.Entities.FirstAsync(specification); 247 | Entity result = await _sampleContext.Entities.FirstOrDefaultAsync(specification); 248 | Entity result = await _sampleContext.Entities.SingleAsync(specification); 249 | Entity result = await _sampleContext.Entities.SingleOrDefaultAsync(specification); 250 | ``` 251 | 252 | ## Full example 253 | 254 | ```csharp 255 | public class Person 256 | { 257 | public int Id { get; set; } 258 | public string Firstname { get; set; } 259 | public string Lastname { get; set; } 260 | } 261 | 262 | public class SampleDbContext : DbContext // Simplified DbContext 263 | { 264 | public virtual DbSet Persons { get; set; } 265 | } 266 | 267 | public class DbService where TEntity : class 268 | { 269 | private readonly DbSet _dbSet; 270 | 271 | public DbService(SampleDbContext context) 272 | { 273 | _dbSet = context.Set(); 274 | } 275 | 276 | public int Count(ISpecification specification) 277 | { 278 | return _dbSet.Count(specification); 279 | } 280 | 281 | public List Get(ISpecification specification) 282 | { 283 | return _dbSet.ExeSpec(specification).ToList(); 284 | } 285 | 286 | public (List items, int count) GetAndCount(ISpecification specification) 287 | { 288 | return (Get(specification), Count(specification)); 289 | } 290 | } 291 | 292 | ISpecification firstnameIsFoo = Spec.New(p => p.Firstname == "Foo"); 293 | ISpecification lastnameIsBar = Spec.New(p => p.Lastname == "Bar"); 294 | ISpecification idDescending = OrderSpec.New(p => p.Id, Sort.Descending); 295 | 296 | ISpecification specification = firstnameIsFoo.And(lastnameIsBar) 297 | .OrderBy(idDescending) 298 | .Paginate(1, 5); // pageNo = 1, pageSize = 5 299 | 300 | using (var context = new SampleDbContext()) 301 | { 302 | var result = new DbService(context).GetAndCount(specification); 303 | // result.items = Paginated list of Person's with name: Foo Bar 304 | // result.count = Total unpaginated result count 305 | } 306 | ``` 307 | -------------------------------------------------------------------------------- /src/LinqBuilder/LinqExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.CodeAnalysis; 4 | using System.Linq; 5 | using LinqBuilder.Internal; 6 | 7 | namespace LinqBuilder 8 | { 9 | public static class LinqExtensions 10 | { 11 | public static IQueryable Where(this IQueryable query, ISpecification specification) 12 | where TEntity : class 13 | { 14 | if (specification == null) 15 | { 16 | throw new ArgumentNullException(nameof(specification)); 17 | } 18 | 19 | var expression = specification.Internal.QuerySpecification.AsExpression(); 20 | return expression == null ? query : query.Where(expression); 21 | } 22 | 23 | public static IEnumerable Where(this IEnumerable collection, ISpecification specification) 24 | where TEntity : class 25 | { 26 | if (specification == null) 27 | { 28 | throw new ArgumentNullException(nameof(specification)); 29 | } 30 | 31 | var func = specification.Internal.QuerySpecification.AsFunc(); 32 | return func == null ? collection : collection.Where(func); 33 | } 34 | 35 | public static bool Any(this IQueryable query, ISpecification specification) 36 | where TEntity : class 37 | { 38 | if (specification == null) 39 | { 40 | throw new ArgumentNullException(nameof(specification)); 41 | } 42 | 43 | var expression = specification.Internal.QuerySpecification.AsExpression(); 44 | return expression == null ? query.Any() : query.Any(expression); 45 | } 46 | 47 | public static bool Any(this IEnumerable collection, ISpecification specification) 48 | where TEntity : class 49 | { 50 | if (specification == null) 51 | { 52 | throw new ArgumentNullException(nameof(specification)); 53 | } 54 | 55 | var func = specification.Internal.QuerySpecification.AsFunc(); 56 | return func == null ? collection.Any() : collection.Any(func); 57 | } 58 | 59 | public static bool All(this IQueryable query, ISpecification specification) 60 | where TEntity : class 61 | { 62 | if (specification == null) 63 | { 64 | throw new ArgumentNullException(nameof(specification)); 65 | } 66 | 67 | var expression = specification.Internal.QuerySpecification.AsExpression(); 68 | return expression == null || query.All(expression); 69 | } 70 | 71 | public static bool All(this IEnumerable collection, ISpecification specification) 72 | where TEntity : class 73 | { 74 | if (specification == null) 75 | { 76 | throw new ArgumentNullException(nameof(specification)); 77 | } 78 | 79 | var func = specification.Internal.QuerySpecification.AsFunc(); 80 | return func == null || collection.All(func); 81 | } 82 | 83 | public static int Count(this IQueryable query, ISpecification specification) 84 | where TEntity : class 85 | { 86 | if (specification == null) 87 | { 88 | throw new ArgumentNullException(nameof(specification)); 89 | } 90 | 91 | var expression = specification.Internal.QuerySpecification.AsExpression(); 92 | return expression == null ? query.Count() : query.Count(expression); 93 | } 94 | 95 | public static int Count(this IEnumerable collection, ISpecification specification) 96 | where TEntity : class 97 | { 98 | if (specification == null) 99 | { 100 | throw new ArgumentNullException(nameof(specification)); 101 | } 102 | 103 | var func = specification.Internal.QuerySpecification.AsFunc(); 104 | return func == null ? collection.Count() : collection.Count(func); 105 | } 106 | 107 | public static TEntity First(this IQueryable query, ISpecification specification) 108 | where TEntity : class 109 | { 110 | if (specification == null) 111 | { 112 | throw new ArgumentNullException(nameof(specification)); 113 | } 114 | 115 | var expression = specification.Internal.QuerySpecification.AsExpression(); 116 | return expression == null ? query.First() : query.First(expression); 117 | } 118 | 119 | public static TEntity First(this IEnumerable collection, ISpecification specification) 120 | where TEntity : class 121 | { 122 | if (specification == null) 123 | { 124 | throw new ArgumentNullException(nameof(specification)); 125 | } 126 | 127 | var func = specification.Internal.QuerySpecification.AsFunc(); 128 | return func == null ? collection.First() : collection.First(func); 129 | } 130 | 131 | public static TEntity? FirstOrDefault(this IQueryable query, ISpecification specification) 132 | where TEntity : class 133 | { 134 | if (specification == null) 135 | { 136 | throw new ArgumentNullException(nameof(specification)); 137 | } 138 | 139 | var expression = specification.Internal.QuerySpecification.AsExpression(); 140 | return expression == null ? query.FirstOrDefault() : query.FirstOrDefault(expression); 141 | } 142 | 143 | public static TEntity? FirstOrDefault(this IEnumerable collection, ISpecification specification) 144 | where TEntity : class 145 | { 146 | if (specification == null) 147 | { 148 | throw new ArgumentNullException(nameof(specification)); 149 | } 150 | 151 | var func = specification.Internal.QuerySpecification.AsFunc(); 152 | return func == null ? collection.FirstOrDefault() : collection.FirstOrDefault(func); 153 | } 154 | 155 | [SuppressMessage("Microsoft.Naming", "CA1720", Justification = "Wrapper around existing Single function.")] 156 | public static TEntity Single(this IQueryable query, ISpecification specification) 157 | where TEntity : class 158 | { 159 | if (specification == null) 160 | { 161 | throw new ArgumentNullException(nameof(specification)); 162 | } 163 | 164 | var expression = specification.Internal.QuerySpecification.AsExpression(); 165 | return expression == null ? query.Single() : query.Single(expression); 166 | } 167 | 168 | [SuppressMessage("Microsoft.Naming", "CA1720", Justification = "Wrapper around existing Single function.")] 169 | public static TEntity Single(this IEnumerable collection, ISpecification specification) 170 | where TEntity : class 171 | { 172 | if (specification == null) 173 | { 174 | throw new ArgumentNullException(nameof(specification)); 175 | } 176 | 177 | var func = specification.Internal.QuerySpecification.AsFunc(); 178 | return func == null ? collection.Single() : collection.Single(func); 179 | } 180 | 181 | public static TEntity? SingleOrDefault(this IQueryable query, ISpecification specification) 182 | where TEntity : class 183 | { 184 | if (specification == null) 185 | { 186 | throw new ArgumentNullException(nameof(specification)); 187 | } 188 | 189 | var expression = specification.Internal.QuerySpecification.AsExpression(); 190 | return expression == null ? query.SingleOrDefault() : query.SingleOrDefault(expression); 191 | } 192 | 193 | public static TEntity? SingleOrDefault(this IEnumerable collection, ISpecification specification) 194 | where TEntity : class 195 | { 196 | if (specification == null) 197 | { 198 | throw new ArgumentNullException(nameof(specification)); 199 | } 200 | 201 | var func = specification.Internal.QuerySpecification.AsFunc(); 202 | return func == null ? collection.SingleOrDefault() : collection.SingleOrDefault(func); 203 | } 204 | 205 | public static IQueryable ExeSpec(this IQueryable query, ISpecification specification, bool skipSort = false) 206 | where TEntity : class 207 | { 208 | if (specification == null) 209 | { 210 | throw new ArgumentNullException(nameof(specification)); 211 | } 212 | 213 | var configuration = specification.Internal; 214 | var querySpecification = configuration.QuerySpecification; 215 | var orderSpecifications = configuration.OrderSpecifications; 216 | var expression = querySpecification.AsExpression(); 217 | 218 | if (expression != null) 219 | { 220 | query = query.Where(expression); 221 | } 222 | 223 | if (skipSort) 224 | { 225 | return query; 226 | } 227 | 228 | IOrderedQueryable? ordered = null; 229 | foreach (var orderSpecification in orderSpecifications) 230 | { 231 | ordered = ordered == null 232 | ? orderSpecification.InvokeSort(query) 233 | : orderSpecification.InvokeSort(ordered); 234 | } 235 | 236 | return SkipTake(ordered ?? query, configuration); 237 | } 238 | 239 | public static IEnumerable ExeSpec(this IEnumerable collection, ISpecification specification, bool skipSort = false) 240 | where TEntity : class 241 | { 242 | if (specification == null) 243 | { 244 | throw new ArgumentNullException(nameof(specification)); 245 | } 246 | 247 | var configuration = specification.Internal; 248 | var querySpecification = configuration.QuerySpecification; 249 | var orderSpecifications = configuration.OrderSpecifications; 250 | var func = querySpecification.AsFunc(); 251 | 252 | if (func != null) 253 | { 254 | collection = collection.Where(func); 255 | } 256 | 257 | if (skipSort) 258 | { 259 | return collection; 260 | } 261 | 262 | var list = collection.ToList(); 263 | 264 | IOrderedEnumerable? ordered = null; 265 | foreach (var orderSpecification in orderSpecifications) 266 | { 267 | ordered = ordered == null 268 | ? orderSpecification.InvokeSort(list) 269 | : orderSpecification.InvokeSort(ordered); 270 | } 271 | 272 | return SkipTake(ordered ?? collection, configuration); 273 | } 274 | 275 | private static IQueryable SkipTake(IQueryable query, SpecificationBase configuration) 276 | where TEntity : class 277 | { 278 | if (configuration.Skip.HasValue) 279 | { 280 | query = query.Skip(configuration.Skip.Value); 281 | } 282 | 283 | if (configuration.Take.HasValue) 284 | { 285 | query = query.Take(configuration.Take.Value); 286 | } 287 | 288 | return query; 289 | } 290 | 291 | private static IEnumerable SkipTake(IEnumerable collection, SpecificationBase configuration) 292 | where TEntity : class 293 | { 294 | if (configuration.Skip.HasValue) 295 | { 296 | collection = collection.Skip(configuration.Skip.Value); 297 | } 298 | 299 | if (configuration.Take.HasValue) 300 | { 301 | collection = collection.Take(configuration.Take.Value); 302 | } 303 | 304 | return collection; 305 | } 306 | } 307 | } 308 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | 400 | ######################################## 401 | 402 | ## JetBrains 403 | ## 404 | ## Get latest from https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 405 | 406 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 407 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 408 | 409 | # User-specific stuff 410 | .idea/**/workspace.xml 411 | .idea/**/tasks.xml 412 | .idea/**/usage.statistics.xml 413 | .idea/**/dictionaries 414 | .idea/**/shelf 415 | 416 | # AWS User-specific 417 | .idea/**/aws.xml 418 | 419 | # Generated files 420 | .idea/**/contentModel.xml 421 | 422 | # Sensitive or high-churn files 423 | .idea/**/dataSources/ 424 | .idea/**/dataSources.ids 425 | .idea/**/dataSources.local.xml 426 | .idea/**/sqlDataSources.xml 427 | .idea/**/dynamic.xml 428 | .idea/**/uiDesigner.xml 429 | .idea/**/dbnavigator.xml 430 | 431 | # Gradle 432 | .idea/**/gradle.xml 433 | .idea/**/libraries 434 | 435 | # Gradle and Maven with auto-import 436 | # When using Gradle or Maven with auto-import, you should exclude module files, 437 | # since they will be recreated, and may cause churn. Uncomment if using 438 | # auto-import. 439 | .idea/artifacts 440 | .idea/compiler.xml 441 | .idea/jarRepositories.xml 442 | .idea/modules.xml 443 | .idea/*.iml 444 | .idea/modules 445 | *.iml 446 | *.ipr 447 | 448 | # CMake 449 | cmake-build-*/ 450 | 451 | # Mongo Explorer plugin 452 | .idea/**/mongoSettings.xml 453 | 454 | # File-based project format 455 | *.iws 456 | 457 | # IntelliJ 458 | out/ 459 | 460 | # mpeltonen/sbt-idea plugin 461 | .idea_modules/ 462 | 463 | # JIRA plugin 464 | atlassian-ide-plugin.xml 465 | 466 | # Cursive Clojure plugin 467 | .idea/replstate.xml 468 | 469 | # SonarLint plugin 470 | .idea/sonarlint/ 471 | 472 | # Crashlytics plugin (for Android Studio and IntelliJ) 473 | com_crashlytics_export_strings.xml 474 | crashlytics.properties 475 | crashlytics-build.properties 476 | fabric.properties 477 | 478 | # Editor-based Rest Client 479 | .idea/httpRequests 480 | 481 | # Android studio 3.1+ serialized cache file 482 | .idea/caches/build_file_checksums.ser 483 | 484 | ######################################## 485 | 486 | ## Visual Studio Code 487 | ## 488 | ## Get latest from https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore 489 | 490 | .vscode/* 491 | !.vscode/settings.json 492 | !.vscode/tasks.json 493 | !.vscode/launch.json 494 | !.vscode/extensions.json 495 | !.vscode/*.code-snippets 496 | 497 | # Local History for Visual Studio Code 498 | .history/ 499 | 500 | # Built Visual Studio Code Extensions 501 | *.vsix 502 | 503 | ######################################## 504 | 505 | ## Windows 506 | ## 507 | ## Get latest from https://github.com/github/gitignore/blob/main/Global/Windows.gitignore 508 | 509 | # Windows thumbnail cache files 510 | Thumbs.db 511 | Thumbs.db:encryptable 512 | ehthumbs.db 513 | ehthumbs_vista.db 514 | 515 | # Dump file 516 | *.stackdump 517 | 518 | # Folder config file 519 | [Dd]esktop.ini 520 | 521 | # Recycle Bin used on file shares 522 | $RECYCLE.BIN/ 523 | 524 | # Windows Installer files 525 | *.cab 526 | *.msi 527 | *.msix 528 | *.msm 529 | *.msp 530 | 531 | # Windows shortcuts 532 | *.lnk 533 | 534 | ######################################## 535 | 536 | ## macOS 537 | ## 538 | ## Get latest from https://github.com/github/gitignore/blob/main/Global/macOS.gitignore 539 | 540 | # General 541 | .DS_Store 542 | .AppleDouble 543 | .LSOverride 544 | 545 | # Icon must end with two \r 546 | Icon 547 | 548 | # Thumbnails 549 | ._* 550 | 551 | # Files that might appear in the root of a volume 552 | .DocumentRevisions-V100 553 | .fseventsd 554 | .Spotlight-V100 555 | .TemporaryItems 556 | .Trashes 557 | .VolumeIcon.icns 558 | .com.apple.timemachine.donotpresent 559 | 560 | # Directories potentially created on remote AFP share 561 | .AppleDB 562 | .AppleDesktop 563 | Network Trash Folder 564 | Temporary Items 565 | .apdisk 566 | 567 | ######################################## 568 | 569 | ## Custom Rules 570 | 571 | coverage 572 | --------------------------------------------------------------------------------