Listen to this Post
Entity Framework (EF) Core interceptors allow developers to intercept and modify database operations at various stages of execution. They are powerful tools for adding custom logic such as audit logging, domain event publishing, and outbox message handling.
🔗 Learn more about EF Core Interceptors: https://lnkd.in/euGKh9Tf
You Should Know:
1. Audit Logging with Interceptors
Intercept database operations to log changes automatically.
Example Code:
public class AuditInterceptor : DbCommandInterceptor { public override InterceptionResult<int> NonQueryExecuting( DbCommand command, CommandEventData eventData, InterceptionResult<int> result) { LogCommand(command); return base.NonQueryExecuting(command, eventData, result); } private void LogCommand(DbCommand command) { Console.WriteLine($"Executed SQL: {command.CommandText}"); } } // Register in DbContext optionsBuilder.AddInterceptors(new AuditInterceptor());
2. Publishing Domain Events
Capture changes and trigger domain events.
Example Code:
public class DomainEventInterceptor : SaveChangesInterceptor { public override InterceptionResult<int> SavingChanges( DbContextEventData eventData, InterceptionResult<int> result) { var domainEvents = eventData.Context.ChangeTracker .Entries<IAggregateRoot>() .SelectMany(e => e.Entity.DomainEvents) .ToList(); PublishEvents(domainEvents); return base.SavingChanges(eventData, result); } private void PublishEvents(List<IDomainEvent> events) { foreach (var domainEvent in events) { // Publish to a message bus Console.WriteLine($"Published: {domainEvent.GetType().Name}"); } } }
3. Storing Outbox Messages
Ensure reliable message delivery in distributed systems.
Example Code:
public class OutboxInterceptor : SaveChangesInterceptor { public override ValueTask<InterceptionResult<int>> SavingChangesAsync( DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default) { var outboxMessages = eventData.Context.ChangeTracker .Entries<OutboxMessage>() .Where(e => e.State == EntityState.Added) .Select(e => e.Entity) .ToList(); if (outboxMessages.Any()) { // Store in outbox table Console.WriteLine($"Stored {outboxMessages.Count} outbox messages."); } return base.SavingChangesAsync(eventData, result, cancellationToken); } }
Linux & Windows Commands for Debugging EF Core
– Log SQL Queries in .NET:
dotnet ef migrations script --verbose
– Monitor SQL Server Queries (Linux):
sudo tcpdump -i any -s 0 -w ef_queries.pcap port 1433
– Windows Performance Monitor for EF:
perfmon /sys
What Undercode Say
EF Core interceptors provide a seamless way to inject cross-cutting concerns into database operations. Whether for logging, event handling, or ensuring data consistency, they enhance maintainability and scalability.
🔗 Additional Resources:
Expected Output:
EF Core Interceptors: Extending Query Behavior in .NET [Rest of the structured article...]
References:
Reported By: Milan Jovanovic – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅