NetLah.Extensions.EventAggregator 0.2.0 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved

.NET Standard 2.0
Install-Package NetLah.Extensions.EventAggregator -Version 0.2.0
dotnet add package NetLah.Extensions.EventAggregator --version 0.2.0
<PackageReference Include="NetLah.Extensions.EventAggregator" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetLah.Extensions.EventAggregator --version 0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetLah.Extensions.EventAggregator, 0.2.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install NetLah.Extensions.EventAggregator as a Cake Addin
#addin nuget:?package=NetLah.Extensions.EventAggregator&version=0.2.0

// Install NetLah.Extensions.EventAggregator as a Cake Tool
#tool nuget:?package=NetLah.Extensions.EventAggregator&version=0.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

NetLah.Extensions.EventAggregator - .NET Library

NetLah.Extensions.EventAggregator is a library which contains a set of reusable classes implement Event Aggregator pattern integrated with dependency injection Microsoft.Extensions.DependencyInjection. These classes/interface are IEventAggregator, IRootEventAggregator, IAsyncSubscriber, ISubscriber.

Nuget package

NuGet

Build Status

Build Status

Getting started

Scoped and singleton

services.AddEventAggregator();

services.AddSingleton<RootEvent1Subscriber>();
services.SubscribeAsync<BaseEvent1, RootEvent1Subscriber>(lifetime: SubscriberLifetime.Singleton);

services.AddScoped<Event1Subscriber>();             // IAsyncSubscriber<TEvent>
services.AddScoped<Event2Subscriber>();             // ISubscriber<TEvent>
services.AddScoped<Event3Subscriber>();             // IAsyncSubscriber<TEvent>
services.AddScoped<Event4Subscriber>();             // ISubscriber<TEvent>

services.SubscribeAsync<Event1, Event1Subscriber>();
services.Subscribe<Event2, Event2Subscriber>();
services.SubscribeAsync<IEvent3>(
    (ev, sp, ct) => sp.GetRequiredService<Event3Subscriber>().HandleAsync(ev, ct));
services.Subscribe<Event4>(
    (ev, sp) => sp.GetRequiredService<Event4Subscriber>().Handle(ev));

// IEventAggregator eventAggregator
// both RootEvent1Subscriber and Event1Subscriber subscribe on Event1
await eventAggregator.PublishAsync(new Event1 { Message = message1 });
await eventAggregator.PublishAsync(new Event2 { Payload = payload2 });
await eventAggregator.PublishAsync(new Event3());
await eventAggregator.PublishAsync(new Event4());

Singleton only

services.AddEventAggregator();

services.AddSingleton<RootEvent1Subscriber>();      // IAsyncSubscriber<TEvent>
services.AddSingleton<RootEvent2Subscriber>();
services.SubscribeAsync<Event1, RootEvent1Subscriber>(lifetime: SubscriberLifetime.Singleton);
services.Subscribe<Event2>(
    (ev, sp) => sp.GetRequiredService<RootEvent2Subscriber>().Handle(ev),
    lifetime: SubscriberLifetime.Singleton);

// IRootEventAggregator rootEventAggregator
await rootEventAggregator.PublishAsync(new Event1 { Message = message1 });
await rootEventAggregator.PublishAsync(new Event2 { Payload = payload2 });

Interface subscriber

public interface IAsyncSubscriber<in TEvent> where TEvent : IEvent
{
    Task HandleAsync(TEvent @event, CancellationToken cancellationToken = default);
}

public interface ISubscriber<in TEvent> where TEvent : IEvent
{
    void Handle(TEvent @event);
}

Delegate subscriber

public static IServiceCollection SubscribeAsync<TEvent>(this IServiceCollection services,
    Func<TEvent, IServiceProvider, CancellationToken, Task> handler,
    SubscriberLifetime lifetime = SubscriberLifetime.Scoped);

public static IServiceCollection SubscribeAsync<TEvent>(this IServiceCollection services,
    Func<TEvent, IServiceProvider, Task> handler,
    SubscriberLifetime lifetime = SubscriberLifetime.Scoped);

public static IServiceCollection Subscribe<TEvent>(this IServiceCollection services,
    Action<TEvent, IServiceProvider> handler,
    SubscriberLifetime lifetime = SubscriberLifetime.Scoped);
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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
0.2.0 227 11/15/2021
0.2.0-rc2 35,766 10/18/2021
0.2.0-rc1.1 30,189 10/6/2021
0.2.0-rc1 15,554 9/30/2021
0.1.0 97,245 5/19/2021
0.1.0-preview1 116 5/17/2021