Skip to content

NetHacksPack.Notifications.Abstractions

Edney Batista da Silva edited this page Jul 3, 2021 · 3 revisions

This package contains the abstractions to implements a set of notifications handlers that will be used by the same client, without creating dependencies among specific notifications types.

Here we use the Open/Closed principle where we can extends new features to the software without modify the core.

Usage

To use it in your project you can dowload diretly from Nuget:

dotnet add NetHacksPack.Notifications.Abstractions

To use this package in any project, after the download configure your dependency injection container registering the de dependencies.

using NetHacksPack.Notifications.Abstractions.DependencyInjection;

public void ConfigureServices(IServiceCollection services)
{
   services.AddNotifications();
}

The method above register the main interfaces dependecies used to send the notifications, INotificationClient and INotificatorProvider.

Provided Interfaces

To let possible different kind of notifications be handled by the same client and make it extensible the package provide a set of interfaces that must be implemented.

INotificationClient - this interface represents the client that will use the providers to hub the notifications to handler it properly. This interface is alredy implemented in the package with the class NotificationClient, but you can override this implemention if you wish change the flow and customize the pipeline or how to resolve the handlers.

public class MyController : Controller
{
   private readonly INotificationClient notificationClient;
   public MyController(INotificationClient notificationClient)
   {
      this.notificationClient = notificationClient;
   }

   [HttpPost]
   public async Task<IActionResult> Post([FromBody] Foo foo)
   {
      // do some business thing here.
      if(fooSucceded)
      {
         Notification successNotification = CreateNotificationForSuccessFoo();
         await notificationClient.SendAsync(successNotification);
         return Ok();
      }
      else
      {
         Notification failNotification = CreateNotificationForFailFoo();
         await notificationClient.SendAsync(failNotification );
         return StatusCode(400);
      }
   }
}

Clone this wiki locally