DimonSmart.Specification.EntityFrameworkCore 1.0.2

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package DimonSmart.Specification.EntityFrameworkCore --version 1.0.2                
NuGet\Install-Package DimonSmart.Specification.EntityFrameworkCore -Version 1.0.2                
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.2" />                
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.2                
#r "nuget: DimonSmart.Specification.EntityFrameworkCore, 1.0.2"                
#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.2

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

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

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. EntityFrameworkCore Support

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 = EFSpecification<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 IQueryable<TEntity> provided by EntityFrameworkCore.

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.

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 199 8/21/2023
1.0.6 157 8/21/2023
1.0.5 163 8/14/2023
1.0.4 164 8/13/2023
1.0.3 157 8/10/2023
1.0.2 192 8/6/2023 1.0.2 is deprecated because it has critical bugs.