The EfCore Extensions provide comprehensive enhancements to Entity Framework Core that implement repository patterns, domain events, data access abstractions, and advanced EF Core functionality specifically designed for Domain-Driven Design (DDD) and Onion Architecture patterns.
- DKNet.EfCore.Abstractions - Core abstractions and interfaces
- DKNet.EfCore.Repos.Abstractions - Repository pattern abstractions
- DKNet.EfCore.Repos - Repository pattern implementations
- DKNet.EfCore.Specifications - Specification pattern for flexible query composition
- DKNet.EfCore.Events - Domain event handling and dispatching
- DKNet.EfCore.Hooks - Lifecycle hooks for EF Core operations
- DKNet.EfCore.DataAuthorization - Data authorization and access control
- DKNet.EfCore.Extensions - EF Core functionality enhancements
- DKNet.EfCore.DtoGenerator - Compile-time DTO generation from entities
- DKNet.EfCore.Relational.Helpers - Relational database utilities
The EfCore Extensions form the Infrastructure Layer in the Onion Architecture, providing all data access concerns while maintaining proper dependency inversion:
┌─────────────────────────────────────────────────────────────────┐
│ 🌐 Presentation Layer │
│ (Controllers, API Endpoints) │
└─────────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────────┴───────────────────────────────────────┐
│ 🎯 Application Layer │
│ (Use Cases, Application Services) │
│ │
│ Dependencies: Repository Interfaces (from Domain) │
└─────────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────────┴───────────────────────────────────────┐
│ 💼 Domain Layer │
│ (Entities, Aggregates, Domain Services) │
│ │
│ • IEventEntity (from EfCore.Abstractions) │
│ • Domain Events (published via EfCore.Events) │
│ • Repository Interfaces (from EfCore.Repos.Abstractions) │
└─────────────────────────┬───────────────────────────────────────┘
│
┌─────────────────────────┴───────────────────────────────────────┐
│ 🗄️ Infrastructure Layer │
│ (Data Access, Persistence) │
│ │
│ 🗃️ DKNet.EfCore.Repos - Repository Implementations │
│ 📋 DKNet.EfCore.Events - Domain Event Dispatching │
│ 🔒 DKNet.EfCore.DataAuthorization - Access Control │
│ ⚡ DKNet.EfCore.Hooks - Lifecycle Management │
│ ⚙️ DKNet.EfCore.Extensions - EF Core Enhancements │
│ 🏗️ DKNet.EfCore.DtoGenerator - Compile-Time DTO Generation │
│ 🔧 DKNet.EfCore.Relational.Helpers - DB Utilities │
│ 📐 DKNet.EfCore.Specifications - Query Specification Pattern │
└─────────────────────────────────────────────────────────────────┘
- Abstractions: Define contracts in the domain layer
- Implementations: Concrete implementations in infrastructure layer
- Generic Repositories: Common CRUD operations with type safety
- Specialized Repositories: Domain-specific data access patterns
- Event Sourcing: Entities can raise domain events
- Event Dispatching: Automatic event publishing after successful operations
- Event Handlers: Decoupled event processing
- Integration Events: Support for external system communication
- Transaction Management: Coordinate multiple repository operations
- Change Tracking: EF Core change tracking integration
- Bulk Operations: Efficient batch operations
- Concurrency Control: Optimistic concurrency support
- Role-Based Access Control: Permission-based data filtering
- Policy-Based Authorization: Flexible authorization rules
- Field-Level Security: Granular access control
- Audit Logging: Comprehensive audit trail
- Flexible Query Logic: Reusable query specifications without repository pattern overhead
- Composable Specifications: Combine specifications using AND/OR operators
- Business Rule Encapsulation: Query logic that reflects domain terminology
- Type-Safe Queries: Generic specifications with compile-time type checking
- Entity base classes enforce identity and concurrency rules
- Event entities support domain event publishing
- Repository patterns maintain aggregate boundaries
- Loosely coupled domain logic through events
- Cross-aggregate communication without direct dependencies
- Integration with external systems via published events
- Repository interfaces use domain terminology
- Entity configurations reflect business rules
- Static data attributes maintain reference data consistency
- Separate DbContexts for different bounded contexts
- Context-specific repository implementations
- Cross-context event integration
- Domain layer defines repository interfaces
- Infrastructure layer implements concrete repositories
- Application layer depends on abstractions, not implementations
- Repository abstractions enable easy mocking
- Domain events can be tested in isolation
- Business logic is independent of data access technology
- Abstract repositories can be implemented with any data access technology
- Domain layer is completely unaware of EF Core
- Easy to swap out persistence implementations
- Each component has a single, well-defined responsibility
- Cross-cutting concerns (authorization, events) are properly separated
- Infrastructure concerns don't leak into domain logic
public class Order : Entity, IEventEntity
{
// Domain properties and business logic
public void CompleteOrder()
{
Status = OrderStatus.Completed;
AddEvent(new OrderCompletedEvent(Id, CustomerId));
}
}public interface IOrderRepository : IRepository<Order>
{
Task<Order?> GetByOrderNumberAsync(string orderNumber);
}
public class OrderRepository : GenericRepository<Order>, IOrderRepository
{
// Implementation in infrastructure layer
}public class OrderCompletedEventHandler : IEventHandler<OrderCompletedEvent>
{
public async Task Handle(OrderCompletedEvent evt)
{
// Handle cross-aggregate concerns
// Send notifications, update read models, etc.
}
}public class CustomerService
{
public async Task<List<Customer>> GetTargetCustomersAsync(string region)
{
var activeCustomers = new ActiveCustomersSpec();
var inRegion = new CustomersInRegionSpec(region);
var highValue = new HighValueCustomersSpec(1000m);
// Combine business rules using logical operators
var specification = activeCustomers & inRegion & highValue;
return await _customerRepository.SpecsListAsync(specification);
}
}- Query Optimization: IQueryable support for efficient database queries
- Change Tracking: Optimized EF Core change tracking patterns
- Bulk Operations: Support for high-performance batch operations
- Connection Management: Proper DbContext lifecycle management
- Caching Integration: Support for distributed caching patterns
- Data Authorization: Comprehensive access control mechanisms
- Audit Logging: Built-in audit trail capabilities
- Encryption Support: Integration with encryption services
- Compliance Ready: GDPR and other regulatory compliance support