-
-
Notifications
You must be signed in to change notification settings - Fork 1
DbConnection Interception
LAP-CHRIS\chris edited this page Mar 10, 2026
·
2 revisions
Language: English | Português (Brasil)
The core package includes an ADO.NET interception pipeline that wraps any DbConnection so you can compose diagnostics, logging, fault injection, and related behaviors around the same connection used by your tests.
WithInterceptors(...)WithInterception(...)WithInterceptionFactory(...)WithRegisteredInterceptors(...)DbInterceptionPipeline.Wrap(...)DbMockConnectionFactory.Create*WithTablesIntercepted(...)
RecordingDbConnectionInterceptorFaultInjectionDbConnectionInterceptorLoggingDbConnectionInterceptorTextWriterDbConnectionInterceptorILoggerDbConnectionInterceptorDiagnosticListenerDbConnectionInterceptorActivitySourceDbConnectionInterceptor
using var intercepted = connection.WithInterceptors(
new RecordingDbConnectionInterceptor(),
new LoggingDbConnectionInterceptor(Console.WriteLine));using var intercepted = connection.WithInterception(options =>
{
options.UseRecording();
options.UseLogging(Console.WriteLine);
options.UseFaultInjection(new FaultInjectionDbConnectionInterceptor
{
Latency = TimeSpan.FromMilliseconds(25)
});
});var factory = new Func<DbConnection>(() => new SqliteConnectionMock(new SqliteDbMock()))
.WithInterceptionFactory(options => options.UseRecording());
using var interceptedConnection = factory.CreateOpenConnection();For container-based setups, the interception story also includes helpers such as:
AddDbInterception(...)AddDbInterceptionRecording(...)AddDbInterceptionLogging(...)AddDbInterceptionLogger(...)AddDbInterceptionTextWriter(...)AddDbInterceptionConnectionFactory(...)
Use WithRegisteredInterceptors(serviceProvider) when the interceptors are resolved from DI later in the same test or host flow.
- Recording connection, command, and transaction events in tests
- Publishing normalized logs without changing test code structure
- Injecting deterministic latency or failures in resilience scenarios
- Reusing the same interception pipeline through factories or DI
- Keeping Dapper and other consumer libraries on top of a standard
DbConnection
The wrappers stay on the normal DbConnection surface, so Dapper can keep using the intercepted connection directly.
- Home
- Getting Started
- DotNet Fiddle Quick Start
- Provider Selection
- Providers and Compatibility
- ADO.NET and Dapper
- DbConnection Interception
- DI and Interception Setup
- Testing Strategy
- Diagnostics and Execution Plans
- Limitations and Known Gaps
- Recipes and Common Scenarios
- Transactions and Volatile Data
- Schema Snapshots and Replay
- FAQ and Troubleshooting
- Observability and Debugging
- Benchmark Results
- Provider-Specific Recipes
- Examples by Architecture
- Migration from Real DB Tests
- Performance of Test Suites
- Compatibility Matrix Summary
- Glossary
- Extensions and Ecosystem
- ORM and Test Integrations
- MiniProfiler Integration
- Visual Studio Extension
- VS Code Extension
- Maintainer Resources
- Início
- Começando
- Começando no .NET Fiddle
- Escolha do Provider
- Provedores e Compatibilidade
- ADO.NET e Dapper
- Interceptação de DbConnection
- Setup de DI e Interceptação
- Estratégia de Testes
- Diagnóstico e Planos de Execução
- Limitações e Gaps Conhecidos
- Receitas e Cenários Comuns
- Transações e Dados Voláteis
- Snapshots de Schema e Replay
- FAQ e Troubleshooting