Skip to content

Commit 906b514

Browse files
Copilothasanxdev
andauthored
feat(sample): add open-generic INotificationHandler<TNotification> demo
Agent-Logs-Url: https://github.com/hasanxdev/DispatchR/sessions/9ef271ed-cd29-415a-a675-f6a2be488540 Co-authored-by: hasanxdev <30981174+hasanxdev@users.noreply.github.com>
1 parent dc8dc01 commit 906b514

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using DispatchR.Abstractions.Notification;
2+
3+
namespace Sample.DispatchR.Notification;
4+
5+
/// <summary>
6+
/// Open-generic notification handler — handles every INotification published through DispatchR.
7+
/// Useful for cross-cutting concerns such as logging, auditing or telemetry.
8+
/// </summary>
9+
public sealed class AllNotificationsLogger<TNotification>(ILogger<AllNotificationsLogger<TNotification>> logger)
10+
: INotificationHandler<TNotification>
11+
where TNotification : INotification
12+
{
13+
public ValueTask Handle(TNotification notification, CancellationToken cancellationToken)
14+
{
15+
logger.LogInformation("[Generic] Received notification of type {NotificationType}: {@Notification}",
16+
typeof(TNotification).Name, notification);
17+
return ValueTask.CompletedTask;
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using DispatchR.Abstractions.Notification;
2+
3+
namespace Sample.DispatchR.Notification;
4+
5+
public sealed record GenericAwareNotification(Guid Id, string Message) : INotification;

src/Sample/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@
124124
return "It works";
125125
});
126126

127+
// Demonstrates open-generic INotificationHandler<TNotification>:
128+
// AllNotificationsLogger<TNotification> is registered once and handles every INotification.
129+
// GenericAwareNotification is handled by both its own specific handler (if any) and the generic one.
130+
app.MapGet("/Notification/DispatchR/Generic", async (DispatchR.IMediator mediator, ILogger<Program> logger) =>
131+
{
132+
var notification = new DispatchRNotificationSample.GenericAwareNotification(Guid.NewGuid(), "Hello from open-generic handler!");
133+
await mediator.Publish(notification, CancellationToken.None);
134+
return "It works";
135+
});
136+
127137
app.Run();
128138

129139
namespace Sample

0 commit comments

Comments
 (0)