EasyEventPublisher 0.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyEventPublisher --version 0.0.3                
NuGet\Install-Package EasyEventPublisher -Version 0.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EasyEventPublisher" Version="0.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasyEventPublisher --version 0.0.3                
#r "nuget: EasyEventPublisher, 0.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EasyEventPublisher as a Cake Addin
#addin nuget:?package=EasyEventPublisher&version=0.0.3

// Install EasyEventPublisher as a Cake Tool
#tool nuget:?package=EasyEventPublisher&version=0.0.3                

EventPublisher

This lightweight library allows you to publish events and define as many handlers as you need. It is very simple to use as defined in this example:

public class NotificationEvent : IEvent
{
 public string Message {get;set}
 public DateTime Date {get;set;}
}


public class WhatsAppEventHandler : IEventHandler<NotificationEvent>
{
    private ILogger<WhatsAppEventHandler> _logger { get; set; }

    public WhatsAppEventHandler(ILogger<WhatsAppEventHandler> logger)
    {
        _logger = logger;
    }

    public async Task HandleAsync(NotificationEvent @event, CancellationToken cancellationToken)
    {
        ...
        _logger.LogInformation("Whatsapp Sent");
    }
}

public class EmailEventHandler : IEventHandler<NotificationEvent>
{
    private ILogger<EmailEventHandler> _logger;

    public EmailEventHandler(ILogger<EmailEventHandler> logger)
    {
        _logger = logger;
    }



    public async Task HandleAsync(NotificationEvent @event, CancellationToken cancellationToken)
    {
        ...
        _logger.LogInformation("Email Sent");
    }
}


In Program.cs or using an extension method of ICollectionService, events should be registered as follows:

services.RegisterEvents();

RegisterEvents() allows a parameter of type EventInjectingType which is used to specify how events handlers should be registered in DI container, the possible values are: Singleton, Scoped, Transcient.

By default EventInjectingType.Singleton will be used, however if you intend to inject in the event handler counstructor any scoped services (as DbContext) then the handlers should be registered as EventInjectingType.Scoped

To publish events IEventManager must be injected wherever you want to publish the event like this:

public class CreateNewProductService : ICreateNewProductService
{
  private readonly IEventManager _eventManager;
  
  public CreateNewProduct(IEventManager eventManager)
  {
   _eventManager = eventManager;
  }
  
  public Task CreateNewProduct(ProductDto product, CancellationToken ctx)
  {
    //Add new product
    ...
    _eventManager.PublishAsync(new NotificationEvent
    {
        Date = DateTime.Now,
        Message = "New Product was added."
    },
    fireAndForget: true, paralelismDegree: 2);
  }
}

PublichAsync() parameters are:

  1. Event model: Entity of previosly defined class implementing IEvent.
  2. fireAndForget (boolean) : If process must await for all events handlers to finish or not.
  3. paralelismDegree: Degree of parallelism in which the handlers must execute
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.