Skip to main content

INotificationHandler

Namespace: RA.Utilities.Feature.Abstractions

The INotificationHandler<TNotification> interface defines the contract for a class that handles a specific INotification. It is the "subscriber" in the publish-subscribe pattern used for eventing within the CQRS architecture.

๐ŸŽฏ Purposeโ€‹

The primary purpose of an INotificationHandler is to contain the logic for a single side effect that should occur in response to an event. When a notification is published using the mediator, the mediator will find all registered handlers for that specific notification type and execute their HandleAsync methods.

This pattern allows for clean separation of concerns. The code that publishes the event does not need to know what actions will be taken in response to it.

This interface is the "subscriber" part of the publish-subscribe pattern:

  • Publisher: Some part of your code (like a command handler) publishes an INotification.
  • Subscribers: One or more INotificationHandler classes that are registered to listen for that specific notification type will have their HandleAsync method invoked.

๐Ÿ”‘ Key Characteristics:โ€‹

FeatureDescription
PatternNotification โ†’ Many Handlers (1-to-many)
InterfaceINotificationHandler<TNotification>
Return valueTask (no return)
Mediator methodPublish()
Typical use caseDomain events, system events, sending emails, logging, etc. โ€” where multiple handlers may react independently
  1. One-to-Many: A single INotification can be handled by zero, one, or many INotificationHandler implementations.
  2. Decoupled Logic: It allows you to add new behaviors (like sending emails, logging, or invalidating caches) without modifying the code that triggers the event.
  3. Fire-and-Forget: The HandleAsync method returns a Task, not a Task<T>. This indicates that the handler performs an action but does not return a value to the publisher.

โš™๏ธ How It Worksโ€‹

  1. Implement the Interface: You create a class that implements INotificationHandler<TNotification>, where TNotification is the specific event class you want to handle.
  2. Implement HandleAsync: You place your business logic for reacting to the event inside the HandleAsync method.
  3. Register the Handler: You register your handler in the dependency injection container. The mediator will then automatically discover and invoke it when the corresponding notification is published.

๐Ÿš€ Usage Exampleโ€‹

Following the example from the INotification documentation, after a ProductCreatedNotification is published, you might have two separate handlers to perform different actions.

Cache Invalidation Handlerโ€‹

This handler is responsible for clearing the cache when a new product is created.

// In Features/Caching/CacheInvalidationHandler.cs
public class CacheInvalidationHandler : INotificationHandler<ProductCreatedNotification>
{
public Task HandleAsync(ProductCreatedNotification notification, CancellationToken cancellationToken)
{
// Invalidate product cache logic here...
Console.WriteLine($"Cache invalidated for product ID: {notification.ProductId}");
return Task.CompletedTask;
}
}

Audit Log Handlerโ€‹

This handler is responsible for writing an audit trail entry.

// In Features/Auditing/AuditLogHandler.cs
public class AuditLogHandler : INotificationHandler<ProductCreatedNotification>
{
public Task HandleAsync(ProductCreatedNotification notification, CancellationToken cancellationToken)
{
// Write to audit log logic here...
Console.WriteLine($"Audit log: Product '{notification.ProductName}' created.");
return Task.CompletedTask;
}
}

IRequestHandler ๐Ÿ†š INotificationHandlerโ€‹

FeatureIRequestHandlerINotificationHandler
PurposeRequestโ€“Response (command/query)Publishโ€“Subscribe (event)
Mediator MethodSend()Publish()
Return TypeHas a response (TResponse)No response (void / Task)
HandlersOne handler per requestMultiple handlers per notification
Use CaseExecute logic and return a valueBroadcast an event to many subscribers
tip

This is fundamentally different from an IRequestHandler, which has a one-to-one relationship with its request. An INotification can have zero, one, or many handlers.