DimonSmart.Specification.EntityFrameworkCore
1.0.2
Prefix Reserved
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
<PackageReference Include="DimonSmart.Specification.EntityFrameworkCore" Version="1.0.2" />
paket add DimonSmart.Specification.EntityFrameworkCore --version 1.0.2
#r "nuget: DimonSmart.Specification.EntityFrameworkCore, 1.0.2"
// 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 | Versions 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. |
-
net6.0
- DimonSmart.Specification (>= 1.0.2)
- Microsoft.EntityFrameworkCore (>= 7.0.9)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.