| id | 0044-instance-based-mediator-for-testability | ||||
|---|---|---|---|---|---|
| title | Instance-Based Mediator for Testability | ||||
| status | Proposed | ||||
| author |
|
||||
| created | 2025-12-23 | ||||
| summary | Converts the static OutboxProducerMediator singleton to an instance field in CommandProcessor, enforcing singleton semantics via DI registration to enable parallel test execution with isolated mediator instances per test. | ||||
| tags |
|
Date: 2025-12-23
Proposed
ADR 0033 established that OutboxProducerMediator should be a static singleton to ensure one outbox per application. However, this creates testability issues:
- Tests cannot run in parallel - require
[Collection("CommandProcessor")]serialization - Tests require explicit
ClearServiceBus()calls in teardown - Static state bleeds between tests causing flaky failures
InMemoryOutboxinstances are shared across tests unexpectedly
The singleton requirement is valid, but enforcing it via static fields is the wrong mechanism.
- Convert static to instance-based: Change
s_mediatorto_mediatorinstance field inCommandProcessor - Preserve singleton via DI: Register
IAmAnOutboxProducerMediatorwithServiceLifetime.Singleton - Add service provider overloads: Enable
Func<IServiceProvider, T>configuration forAddBrighter,AddProducers,AddConsumers - Integrate Options pattern: Allow
PostConfigure<BrighterOptions>for test-time overrides - Deprecate ClearServiceBus: Mark obsolete (still functional for transition)
Positive:
- Tests can run in parallel without
[Collection]attributes - Each test can have isolated
CommandProcessorwith own mediator - Standard .NET Options pattern enables test overrides
InMemoryOutboxproperly isolated per test
Negative:
- Migration effort for existing tests (optional - old pattern still works)
- Slight API surface increase with new overloads
Backward Compatibility:
- All existing
Action<T>overloads unchanged ClearServiceBus()still works (deprecated)- DI registration patterns unchanged