Notification, Notification Handler and Notification Publisher


Notification

Notifications enable the broadcasting of messages to one or more handlers, facilitating the communication of events or modifications to the application’s components. The Crystal Sharp framework simplifies the process of creating, publishing, and managing notifications. The code snippet below demonstrates an example of how to create a notification:

public class OrderCreatedNotification : INotification
{
    public string OrderCode { get; set; }
}

The code snippet above demonstrates the usage of the OrderCreatedNotification class, which acts as a simple implementation of the INotification interface. It is mandatory for all notifications within the Crystal Sharp framework to incorporate this interface.

IMPORTANT

It is necessary for every CQRS-based notification to implement the “INotification” interface.

Notification Handler

To effectively handle the notification, a handler is required. The execution of a notification takes place within a notification handler. The code snippet below illustrates an instance of a notification handler:

public class OrderCreatedNotificationHandler : NotificationHandler<OrderCreatedNotification>
{
    public override async Task Handle(OrderCreatedNotification notification, CancellationToken cancellationToken = default)
    {
        // Handle logic here
    }
}

The code snippet provided above represents the implementation of the OrderCreatedNotificationHandler class, which is responsible for managing the OrderCreatedNotification. To ensure the correct operation of the notification handler class, it is necessary to inherit from the NotificationHandler<TNotification> class and override the Handle(TNotification notification, CancellationToken cancellationToken = default) method. The TNotification parameter in the Handle method represents the notification that is being handled.

Notification Publisher

In order to publish notifications, a notification publisher is required. The notification publisher is responsible for sending the notification to the appropriate notification handlers. The Crystal Sharp framework incorporates an interface named INotificationPublisher that serves the purpose of publishing notifications. The following code snippet illustrates an example of a notification publisher:

public class OrderController : ControllerBase
{
    private readonly INotificationPublisher _notificationPublisher;

    public OrderController(INotificationPublisher notificationPublisher)
    {
        _notificationPublisher = notificationPublisher;
    }

    [HttpPost]
    public async Task<ActionResult> Post([FromBody] OrderInfo orderInfo)
    {
        OrderCreatedNotification notification = new() { Id = orderInfo.Id, Status = orderInfo.Status };

        await _notificationPublisher.Publish(notification, CancellationToken.None).ConfigureAwait(false);

        return Ok("Order notification sent.");
    }
}

The provided code snippet utilizes constructor injection to inject the INotificationPublisher interface into the OrderController class. Within the Post method of the OrderController, a new notification is generated and then published by the notification publisher.

The INotificationPublisher interface has the following method:

Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification Publishes notifications to one or more notification handlers. The parameter notification could be any class that implements the interface INotification. The parameter cancellationToken is optional.