https://github.com/netlah/event-aggregator
Event Aggregator for dotnet, intergrated with Scoped DependencyInjection in .NETCore and ASP.NETCore
https://github.com/netlah/event-aggregator
aggregator aspnetcore dotnet event event-aggregator event-sourcing netcore nuget sourcing
Last synced: about 1 month ago
JSON representation
Event Aggregator for dotnet, intergrated with Scoped DependencyInjection in .NETCore and ASP.NETCore
- Host: GitHub
- URL: https://github.com/netlah/event-aggregator
- Owner: NetLah
- License: mit
- Created: 2021-05-08T04:35:00.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-21T08:19:43.000Z (about 1 year ago)
- Last Synced: 2025-03-25T16:55:36.658Z (about 2 months ago)
- Topics: aggregator, aspnetcore, dotnet, event, event-aggregator, event-sourcing, netcore, nuget, sourcing
- Language: C#
- Homepage:
- Size: 78.1 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NetLah.Extensions.EventAggregator - .NET Library
[NetLah.Extensions.EventAggregator](https://www.nuget.org/packages/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
[](https://www.nuget.org/packages/NetLah.Extensions.EventAggregator/)
## Build Status
[](https://actions-badge.atrox.dev/NetLah/event-aggregator/goto?ref=main)
## Getting started
### Scoped and singleton
```C#
services.AddEventAggregator();services.AddSingleton();
services.SubscribeAsync(lifetime: SubscriberLifetime.Singleton);services.AddScoped(); // IAsyncSubscriber
services.AddScoped(); // ISubscriber
services.AddScoped(); // IAsyncSubscriber
services.AddScoped(); // ISubscriberservices.SubscribeAsync();
services.Subscribe();
services.SubscribeAsync(
(ev, sp, ct) => sp.GetRequiredService().HandleAsync(ev, ct));
services.Subscribe(
(ev, sp) => sp.GetRequiredService().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
```C#
services.AddEventAggregator();services.AddSingleton(); // IAsyncSubscriber
services.AddSingleton();
services.SubscribeAsync(lifetime: SubscriberLifetime.Singleton);
services.Subscribe(
(ev, sp) => sp.GetRequiredService().Handle(ev),
lifetime: SubscriberLifetime.Singleton);// IRootEventAggregator rootEventAggregator
await rootEventAggregator.PublishAsync(new Event1 { Message = message1 });
await rootEventAggregator.PublishAsync(new Event2 { Payload = payload2 });
```## Interface subscriber
```C#
public interface IAsyncSubscriber where TEvent : IEvent
{
Task HandleAsync(TEvent @event, CancellationToken cancellationToken = default);
}public interface ISubscriber where TEvent : IEvent
{
void Handle(TEvent @event);
}
```## Delegate subscriber
```C#
public static IServiceCollection SubscribeAsync(this IServiceCollection services,
Func handler,
SubscriberLifetime lifetime = SubscriberLifetime.Scoped);public static IServiceCollection SubscribeAsync(this IServiceCollection services,
Func handler,
SubscriberLifetime lifetime = SubscriberLifetime.Scoped);public static IServiceCollection Subscribe(this IServiceCollection services,
Action handler,
SubscriberLifetime lifetime = SubscriberLifetime.Scoped);
```