AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore
1.0.27
dotnet add package AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore --version 1.0.27
NuGet\Install-Package AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore -Version 1.0.27
<PackageReference Include="AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore" Version="1.0.27" />
<PackageVersion Include="AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore" Version="1.0.27" />
<PackageReference Include="AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore" />
paket add AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore --version 1.0.27
#r "nuget: AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore, 1.0.27"
#:package AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore@1.0.27
#addin nuget:?package=AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore&version=1.0.27
#tool nuget:?package=AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore&version=1.0.27
AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore
Clean Architecture Infrastructure Layer - EF Core Integration, Repository Implementations, and Data Access
Overview
The Infrastructure layer provides implementations for data access, external services, and framework integrations. This package specifically implements Entity Framework Core repositories and specification evaluators for database operations.
Purpose
This layer provides:
- Repository implementations using Entity Framework Core
- Specification evaluators for translating domain specifications to EF Core queries
- Database context extensions for common configurations
- Data access abstractions implementations
Key Components
Repository Implementations
ReadOnlyEfRepository<TDbContext, T, TKey>: Read-only repository base classEfRepository<TDbContext, T, TKey>: Full repository with CRUD operations- Supports both tracking and no-tracking queries
- Built-in pagination and sorting
Specification Evaluator
EfCoreSpecificationEvaluator<T>: Translates domain specifications to EF Core queries- Supports filtering, sorting, pagination, and includes
- Optimized query generation
EF Core Extensions
ModelBuilderExtensions: Extension methods for model configurationEntityTypeBuilderExtensions: Extension methods for entity configuration
Features
- Repository Pattern: Generic repository with specification support
- EF Core Integration: Seamless Entity Framework Core integration
- Query Optimization: Efficient query generation with tracking control
- Specification Support: Translate domain specifications to database queries
- Pagination: Built-in pagination support
- Performance: Support for split queries and query tags
Installation
dotnet add package AkbarAmd.SharedKernel.Infrastructure.EntityFrameworkCore
Usage Examples
Creating a Repository
public class UserRepository : EfRepository<ApplicationDbContext, User, Guid>
{
public UserRepository(ApplicationDbContext dbContext)
: base(dbContext, enableTracking: false)
{
}
// Custom query methods can be added here
public async Task<User?> GetByEmailAsync(string email, CancellationToken cancellationToken = default)
{
return await _dbSet
.AsNoTracking()
.FirstOrDefaultAsync(u => u.Email == email, cancellationToken);
}
}
Using Specifications with Repository
// Define specification
var activeUsersSpec = new CriteriaBuilder<User>()
.Where(u => u.IsActive)
.OrderBy(u => u.CreatedAt)
.Take(10)
.Build();
// Use with repository
var users = await _userRepository.GetAsync(activeUsersSpec, cancellationToken);
Repository Operations
// Add entity
var user = new User("user@example.com", "John Doe");
await _userRepository.AddAsync(user, cancellationToken);
await _userRepository.SaveChangesAsync(cancellationToken);
// Update entity
user.UpdateName("Jane Doe");
await _userRepository.UpdateAsync(user, cancellationToken);
await _userRepository.SaveChangesAsync(cancellationToken);
// Delete entity
await _userRepository.DeleteAsync(userId, cancellationToken);
await _userRepository.SaveChangesAsync(cancellationToken);
// Get by ID
var user = await _userRepository.GetByIdAsync(userId, cancellationToken);
// Get all with pagination
var paginatedResult = await _userRepository.GetPaginatedAsync(
pageNumber: 1,
pageSize: 10,
cancellationToken: cancellationToken);
Configuring DbContext
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Apply configurations
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
// Configure audit properties
modelBuilder.ConfigureAuditProperties();
}
}
Dependencies
- Entity Framework Core 8.0: ORM framework
- Microsoft.AspNetCore.Authorization: Authorization support
- Microsoft.AspNetCore.Identity.EntityFrameworkCore: Identity integration
Architecture Principles
This layer follows Clean Architecture principles:
- Implements Domain abstractions: Provides concrete implementations for repository interfaces
- Framework-specific: Contains EF Core-specific code
- Isolated from Domain: Domain layer has no knowledge of this implementation
- Testable: Can be replaced with in-memory implementations for testing
Performance Considerations
- No-Tracking by Default: Read-only operations use
AsNoTracking()for better performance - Split Queries: Support for split queries to avoid cartesian explosion
- Query Tags: Support for query tags for debugging and monitoring
- Identity Resolution: Configurable identity resolution for no-tracking queries
License
MIT License - see LICENSE file for details.
Author
Akbar Ahmadi Saray - GitHub
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- AkbarAmd.SharedKernel.Domain (>= 1.0.26)
- Bonyan (>= 1.5.7)
- Microsoft.AspNetCore.Authorization (>= 8.0.18)
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 8.0.18)
- Microsoft.EntityFrameworkCore (>= 8.0.18)
- Microsoft.EntityFrameworkCore.Relational (>= 8.0.18)
- Microsoft.EntityFrameworkCore.SqlServer (>= 8.0.18)
- Microsoft.Extensions.Localization (>= 8.0.18)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version 1.0.26:
- Updated to support new specification fluent API (Where-first pattern)
- Compatible with AndGroup/OrGroup changes from Domain layer
- All tests updated and passing
Version 1.0.25:
- Removed IConcurrentAudit interface and all related concurrency audit functionality
- Updated ConfigureFullAuditableEntity methods to remove IConcurrentAudit constraint
- Removed ConfigureConcurrentAudit methods from EntityTypeBuilderExtensions and ModelBuilderExtensions
Version 1.0.24:
- Synchronized version with Domain and Application layers
Version 1.0.23:
- Synchronized version with Domain and Application layers
Version 1.0.19:
- Enhanced EfCoreSpecificationEvaluator with full support for nested include chains
- Added automatic handling of nullable navigation properties in ThenInclude chains
- Improved include chain evaluation with proper type resolution
- Full support for Include().ThenInclude().ThenInclude() chains in EF Core queries
- Enhanced specification pattern integration with Entity Framework Core
Version 1.0.18:
- Removed Version property configuration from ConfigureAggregateRoots method
- Updated ConfigureModificationAudit to use ModifiedAt/ModifiedBy instead of LastModifiedAt/LastModifiedBy
- Updated index names and filter expressions to reflect property name changes
- Breaking change: Entity Framework Core configurations now use ModifiedAt/ModifiedBy property names