DimonSmart.Specification.EntityFrameworkCore 1.0.7

Prefix Reserved
dotnet add package DimonSmart.Specification.EntityFrameworkCore --version 1.0.7                
NuGet\Install-Package DimonSmart.Specification.EntityFrameworkCore -Version 1.0.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DimonSmart.Specification.EntityFrameworkCore" Version="1.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DimonSmart.Specification.EntityFrameworkCore --version 1.0.7                
#r "nuget: DimonSmart.Specification.EntityFrameworkCore, 1.0.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DimonSmart.Specification.EntityFrameworkCore as a Cake Addin
#addin nuget:?package=DimonSmart.Specification.EntityFrameworkCore&version=1.0.7

// Install DimonSmart.Specification.EntityFrameworkCore as a Cake Tool
#tool nuget:?package=DimonSmart.Specification.EntityFrameworkCore&version=1.0.7                

Specification Pattern Implementation in C#

Overview

The Specification Pattern is a behavioral design pattern that allows you to define reusable and composable query specifications. It is commonly used in scenarios where you need to filter, order, and include related entities in a query. The main idea behind this pattern is to encapsulate the logic of a query into separate specification classes, which can then be combined and reused to build complex queries.

In this C# implementation of the Specification Pattern, we have two parts:

1. Classical Specification (Specification class)

The classical specification provides the core functionality to build query specifications. It contains methods to define filtering criteria (Where), sorting (OrderBy, OrderByDesc), and logical operations (And, Or) to combine multiple specifications together.

Usage Example:
var specification = Specification<Student>
    .Create()
    .Where(s => s.Age < 21)
    .OrderBy(s => s.Name)
    .OrderByDesc(s => s.Age);

2. Specification for use with EntityFrameworkCore (EFCoreSpecification)

The EntityFrameworkCore support extends the classical specification with EF-specific operations, such as Include and ThenInclude, to eagerly load related entities in the query.

Usage Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .Where(s => s.Age < 21)
    .Include(s => s.School.MainBook.Author);

Executing Specifications

To execute the specifications, you can use extension methods on the DBContext provided.

var under21 = _testDBContext.BySpecification(specification).ToList();

Custom Specifications

Developers can create their custom specification classes by inheriting from the Specification<TEntity> class and pass parameters through the constructor.

Example:
public class CustomStudentSpecification : Specification<Student>
{
    public CustomStudentSpecification(int minAge, string schoolName)
    {
        Where(s => s.Age >= minAge && s.School.Name == schoolName);
    }
}

Conclusion

The Specification Pattern in C# allows you to create reusable and composable query specifications for different data access scenarios. By providing EntityFrameworkCore support, you can efficiently use these specifications in Entity Framework queries, enhancing query performance and readability.

Remember that you can also create custom specification classes by inheriting from the base Specification class, making the pattern even more flexible and adaptable to various business requirements.

Supported functions

Where(Expression<Func<T, bool>> expr)

Specifies a filtering condition for the data query.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .Where(s => s.Age < 21);

OrderBy(Expression<Func<T, object>> orderByExpression)

Specifies an ascending order for the data query.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .OrderBy(s => s.Name);

OrderByDesc(Expression<Func<T, object>> orderByExpression)

Specifies an ascending order for the data query

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .OrderByDesc(s => s.Name);

Take(int take)

Specifies the number of elements to be skipped from the query.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .Take(10);

Skip(int skip)

Specifies the number of elements to be skipped from the query.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .Skip(20);

Include(Expression<Func<T, TProperty>> includeExpression) / ThenInclude

Adds an "Include" statement to the query, specifying related entities to be loaded.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .Include(s => s.Orders)
        .ThenInclude(order => order.OrderDetails);

AsNoTracking()

AsNoTracking specifies that the query should be executed with "NoTracking" behavior.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .AsNoTracking();

AsNoTrackingWithIdentityResolution()

AsNoTrackingWithIdentityResolution specifies that the query should be executed with "NoTracking" and identity resolution behavior.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .AsNoTrackingWithIdentityResolution();

AsSplitQuery()

AsSplitQuery specifies that the query should be executed with "SplitQuery" behavior.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .AsSplitQuery();

AsSingleQuery()

AsSingleQuery Specifies that the query should be executed with "SingleQuery" behavior.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .AsSingleQuery();

IgnoreAutoIncludes()

IgnoreAutoIncludes specifies to ignore automatically included related entities in the query.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .IgnoreAutoIncludes();

IgnoreQueryFilters()

IgnoreQueryFilters specifies to ignore query filters defined in the model.

Example:
var specification = EFCoreSpecification<Student>
    .Create()
    .IgnoreQueryFilters();

Or(IEFCoreSpecification<T> or)

Combines the current specification with another specification using the logical OR operator.

Example:
var youngStudentsSpec = EFCoreSpecification<Student>
    .Create()
    .Where(s => s.Age < 21);

var studentsFromNewYorkSpec = EFCoreSpecification<Student>
    .Create()
    .Where(s => s.City == "New York");

var combinedSpecificationWithOr = youngStudentsSpec.Or(studentsFromNewYorkSpec);

And(IEFCoreSpecification<T> and)

Combines the current specification with another specification using the logical AND operator.

Example:
var youngStudentsSpec = EFCoreSpecification<Student>
    .Create()
    .Where(s => s.Age < 21);

var studentsFromNewYorkSpec = EFCoreSpecification<Student>
    .Create()
    .Where(s => s.City == "New York");

var combinedSpecificationWithAnd = youngStudentsSpec.And(studentsFromNewYorkSpec);
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.7 201 8/21/2023
1.0.6 159 8/21/2023
1.0.5 165 8/14/2023
1.0.4 168 8/13/2023
1.0.3 159 8/10/2023
1.0.2 194 8/6/2023 1.0.2 is deprecated because it has critical bugs.