EasyEventPublisher 1.0.3
dotnet add package EasyEventPublisher --version 1.0.3
NuGet\Install-Package EasyEventPublisher -Version 1.0.3
<PackageReference Include="EasyEventPublisher" Version="1.0.3" />
paket add EasyEventPublisher --version 1.0.3
#r "nuget: EasyEventPublisher, 1.0.3"
// 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
- Define your events models
public class NotificationEvent
{
public string Message {get; set}
public DateTime Date {get; set;}
}
- 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");
}
}
- 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:
- event model: Previosly event model class defined.
- fireAndForget (boolean) : If defines the behavior of events handlers, wait for all to finish execution or fire and forget.
- paralelismDegree: This make sense if fireAndForget is false. It defines the parallelism degree of handlers execution.
- cancellation token.
Product | Versions 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. |
-
.NETStandard 2.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.