An open API service indexing awesome lists of open source software.

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

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

[![NuGet](https://img.shields.io/nuget/v/NetLah.Extensions.EventAggregator.svg?style=flat-square&label=nuget&colorB=00b200)](https://www.nuget.org/packages/NetLah.Extensions.EventAggregator/)

## Build Status

[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2FNetLah%2Fevent-aggregator%2Fbadge%3Fref%3Dmain&style=flat)](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(); // ISubscriber

services.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);
```