libc.eventbus 7.1.2

dotnet add package libc.eventbus --version 7.1.2                
NuGet\Install-Package libc.eventbus -Version 7.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="libc.eventbus" Version="7.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add libc.eventbus --version 7.1.2                
#r "nuget: libc.eventbus, 7.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install libc.eventbus as a Cake Addin
#addin nuget:?package=libc.eventbus&version=7.1.2

// Install libc.eventbus as a Cake Tool
#tool nuget:?package=libc.eventbus&version=7.1.2                

An in-memory event bus without any dependency in C

Why bother?

A simple and lightweight event bus that can be used in any C# project without ceremony. Can be used effectively in an Outbox pattern to process events in the same transaction synchronously while keeping application parts loosely coupled.

Read More

You can check out my article on EventAggregator HERE

Installation

The package is available on nuget.org:

PM> Install-Package libc.eventbus

Simple Usage

Let's say we have a simple message event that has a text:

public class SimpleMessage : IEvent {
    public string Text { get; private set; }

    public SimpleMessage(string text) {
        Text = text;
    }
}

Now we want that when a SimpleMessage is raised, print it in two distinct ways:

public class PrintMessageRaw : IEventHandler<SimpleMessage> {
    public Task Handle(SimpleMessage ev) {
        // print message
        Console.WriteLine($"Raw: {ev.Text}");
        return Task.CompletedTask;
    }
}

public class PrintMessagePretty : IEventHandler<SimpleMessage> {
    public Task Handle(SimpleMessage ev) {
        // print message
        Console.WriteLine($"Pretty: {ev.Text}");
        return Task.CompletedTask;
    }
}

As you can see both PrintMessageRaw and PrintMessagePretty implement _ _IEventHandler<SimpleMessage>__.

Put it together

Read the comments please

public void Showcase_WithoutCatchAll() {
    // 1- create an event bus
    var bus = new DefaultEventBus();

    // 2- subscribe to SimpleMessage event via PrintMessageRaw event handler
    bus.Subscribe<SimpleMessage, PrintMessageRaw>(new PrintMessageRaw());

    // 3- subscribe to SimpleMessage event via PrintMessagePretty event handler
    var x = new PrintMessagePretty();
    bus.Subscribe<SimpleMessage, PrintMessagePretty>(x);

    // 4- remember subscribing to a message with the same handler instance, has no effect!
    bus.Subscribe<SimpleMessage, PrintMessagePretty>(x);

    // 5- create the event
    var message = new SimpleMessage("a simple message");

    // 6- publish the event
    bus.Publish(message);
}

The console will show:

Raw: a simple message
Pretty: a simple message

Usage 2

There are times that we need to catch all the event (for example for logging purposes). There's a ICatchAllEventHandler interface that you can register in the bus. To test it, first add a new event:

public class PrivateMessage : IEvent {
    public string Secret { get; private set; }

    public PrivateMessage(string secret) {
        Secret = secret;
    }
}

then an implementation of ICatchAllEventHandler:

public class CatchAllMessages : ICatchAllEventHandler {
    public Task Handle(IEvent ev) {
        if (ev is SimpleMessage) {
            Console.WriteLine($"Caught SimpleMessage: {(ev as SimpleMessage).Text}");
        } else if (ev is PrivateMessage) {
            Console.WriteLine($"Caught PrivateMessage: {(ev as PrivateMessage).Secret}");
        }
        return Task.CompletedTask;
    }
}

Put it together

Read the comments please

public void Showcase_WithCatchAll() {
    // 1- create an event bus
    var bus = new DefaultEventBus();

    // 2- subscribe to SimpleMessage event via PrintMessageRaw event handler
    bus.Subscribe<SimpleMessage, PrintMessageRaw>(new PrintMessageRaw());

    // 3- subscribe to SimpleMessage event via PrintMessagePretty event handler
    bus.Subscribe<SimpleMessage, PrintMessagePretty>(new PrintMessagePretty());

    // 4- register a catch-all event handler
    bus.RegisterCatchAllHandler(new CatchAllMessages());

    // 5- create the event
    var message = new SimpleMessage("a simple message");

    // 6- publish the event
    bus.Publish(message);
}

The console will show:

Raw: a simple message
Pretty: a simple message
Caught SimpleMessage: a simple message

To see the full showcase click here

Contributions are welcome

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on libc.eventbus:

Package Downloads
libw.core

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.1.2 83 11/21/2024
7.1.1 698 8/4/2024
7.0.1 1,285 12/3/2023
7.0.0 638 3/19/2023
6.1.0 460 9/3/2022
6.0.1 525 1/31/2022
6.0.0 293 11/27/2021
4.0.1 421 6/13/2021
4.0.0 410 1/6/2021
3.0.0 8,843 9/17/2020
2.0.0 3,698 9/15/2020
1.0.1 15,102 2/19/2020
1.0.0 2,899 2/6/2020