Mediator Pattern Implementation in C#
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Mediator Pattern runs in O(n) time and O(n) space, in C# as in every other language.
- ✓C# has events as a language feature: a delegate field with restricted access, subscribed with the plus-equals operator and unsubscribed with minus-equals, and raised only from inside the declaring type.
- ✓Raise events with the null-conditional invoke operator, since an event with no subscribers is null and calling it directly throws.
- ✓The C# trap: Subscribing to an event creates a strong reference from the PUBLISHER to the SUBSCRIBER, which is backwards from what most people picture, so a short-lived view subscribing to a long-lived service is kept alive by that service for the process lifetime and the leak grows with every navigation.
- ✓A multicast delegate invocation walks an invocation list and calls each target, so it is linear and each call is indirect and rarely inlined.
What Mediator Pattern actually is
Mediator Pattern in C#: what you actually reach for
C# implementation
public sealed class UserSavedEventArgs(string id) : EventArgs
{
public string Id { get; } = id;
}
public sealed class UserService
{
public event EventHandler<UserSavedEventArgs>? Saved;
public void Save(string id)
{
// ?.Invoke: an event with NO subscribers is null and would throw.
Saved?.Invoke(this, new UserSavedEventArgs(id));
}
}
// The PUBLISHER holds the SUBSCRIBER alive, the reference runs the way most
// people do not expect, so a long-lived service pins every short-lived view.
public sealed class Screen : IDisposable
{
private readonly UserService service;
public Screen(UserService service)
{
this.service = service;
service.Saved += OnSaved;
}
private void OnSaved(object? sender, UserSavedEventArgs e) { }
// Without this, the service keeps this Screen alive for the process lifetime.
public void Dispose() => service.Saved -= OnSaved;
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe C#-specific trap
Performance in C#, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which C# type or library should I use for Mediator Pattern?
C# has events as a language feature: a delegate field with restricted access, subscribed with the plus-equals operator and unsubscribed with minus-equals, and raised only from inside the declaring type. IObservable and IObserver are the framework interfaces for push sequences and System.Reactive builds on them. MediatR is the near-ubiquitous library for in-process request and notification dispatch, and System.Threading.Channels is the modern queue when the hub should be asynchronous.
What is the most common C# mistake when implementing Mediator Pattern?
Subscribing to an event creates a strong reference from the PUBLISHER to the SUBSCRIBER, which is backwards from what most people picture, so a short-lived view subscribing to a long-lived service is kept alive by that service for the process lifetime and the leak grows with every navigation. Nothing warns, and the object graph looks correct. WPF has WeakEventManager precisely for this, and the general fix is that whoever subscribes must unsubscribe. The second trap is that raising an event invokes subscribers synchronously in registration order and one that throws prevents the rest from running.
How does Mediator Pattern actually perform in C#?
A multicast delegate invocation walks an invocation list and calls each target, so it is linear and each call is indirect and rarely inlined. Adding and removing handlers allocates a new invocation list each time, because delegates are immutable, so a hot subscribe-unsubscribe cycle allocates continuously. MediatR resolves handlers through the container per request and uses reflection for the generic dispatch, which is fine at request rates and unsuitable for a high-frequency in-memory bus. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Devin offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.