EasyEventPublisher 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyEventPublisher --version 1.0.0                
NuGet\Install-Package EasyEventPublisher -Version 1.0.0                
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="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasyEventPublisher --version 1.0.0                
#r "nuget: EasyEventPublisher, 1.0.0"                
#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=1.0.0

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

EasyEventPublisher

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:

Install package:

Install-Package EasyEventPublisher -Version 1.0.0
  1. Define your events models
public class NotificationEvent
{
 public string Message {get; set}
 public DateTime Date {get; set;}
}
  1. Use IEventHandler<T> interface to implement event handlers that will be executed once yo publish an event of type T.
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");
    }
}


  1. Use extension method of RegisterEvents to automatically register each event handler found in defined assembly

RegisterEvents() has two parameters: EventInjectingType which is used to specify how events handlers should be registered in DI container, the possible values are: (Singleton, Scoped, Transcient) and params Type[] handlerAssemblyMarkerTypes which are types from assemblies where Event Handlers should me searched.

var builder = WebApplication.CreateBuilder();

// In this case we are registering events handlers from two different assemblies and two differents InjectingType.
// In case EventInjectingType value is the same it can be done like this:
// builder.Services.RegisterEvents(EventInjectingType.Singleton, typeof(Program), typeof(DummyClass));

builder.Services.RegisterEvents(EventInjectingType.Singleton, typeof(Program));

builder.Services.RegisterEvents(EventInjectingType.Scoped, typeof(DummyClass));

var app = builder.Build();

app.Run();

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 just 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, ctx);
  }
}

PublichAsync() parameters are:

  1. event model: Previosly event model class defined.
  2. fireAndForget (boolean) : If defines the behavior of events handlers, wait for all to finish execution or fire and forget.
  3. paralelismDegree: This make sense if fireAndForget is false. It defines the parallelism degree of handlers execution.
  4. cancellation token.
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.