EasyEventPublisher 1.0.3

dotnet add package EasyEventPublisher --version 1.0.3
NuGet\Install-Package EasyEventPublisher -Version 1.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="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasyEventPublisher --version 1.0.3
#r "nuget: EasyEventPublisher, 1.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=1.0.3

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

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.3

Compatibility: netstandard2.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 net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.

Version Downloads Last updated
1.0.3 677 11/26/2022
1.0.2 268 11/23/2022
1.0.1 1,553 11/20/2022
1.0.0 273 11/20/2022
0.0.3 1,526 8/23/2022
0.0.2 347 8/22/2022
0.0.1 361 8/22/2022