EasyEventPublisher 0.0.1
See the version list below for details.
dotnet add package EasyEventPublisher --version 0.0.1
NuGet\Install-Package EasyEventPublisher -Version 0.0.1
<PackageReference Include="EasyEventPublisher" Version="0.0.1" />
paket add EasyEventPublisher --version 0.0.1
#r "nuget: EasyEventPublisher, 0.0.1"
// Install EasyEventPublisher as a Cake Addin #addin nuget:?package=EasyEventPublisher&version=0.0.1 // Install EasyEventPublisher as a Cake Tool #tool nuget:?package=EasyEventPublisher&version=0.0.1
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:
- Event model: Entity of previosly defined class implementing IEvent.
- fireAndForget (boolean) : If process must await for all events handlers to finish or not.
- paralelismDegree: Degree of parallelism in which the handlers must execute
Product | Versions 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. |
-
net6.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.