Review generated report notifier

from Delegates and events
C# 14 / .NET 10 advanced 6 min 4 issues to find

Review this generated notifier before it handles confidential reports in a long-running process.

Allow zero listeners, await each dashboard save before PublishAsync returns, preserve failures, avoid logging report contents, and release the dashboard on disposal.

csharp
using System;
using System.Threading.Tasks;
public sealed class ReportSource
{
    public event EventHandler<string>? Ready;
    public async Task PublishAsync(string report)
    {
        Ready!.Invoke(this, report);
        await Task.CompletedTask;
    }
}
public sealed class Dashboard : IDisposable
{
    private readonly ReportSource _source;
    public Dashboard(ReportSource source) {
        _source = source;
        _source.Ready += async (_, report) =>
        {
            Console.WriteLine($"saving {report}");
            await SaveAsync(report);
        };
    }
    public void Dispose() => _source.Ready -= async (_, report) => await SaveAsync(report);
    private static Task SaveAsync(string report) => Task.Delay(10);
}

generated code is illustrative, not from any one model

Open in playground
Report an error