libc.eventbus
7.1.2
dotnet add package libc.eventbus --version 7.1.2
NuGet\Install-Package libc.eventbus -Version 7.1.2
<PackageReference Include="libc.eventbus" Version="7.1.2" />
paket add libc.eventbus --version 7.1.2
#r "nuget: libc.eventbus, 7.1.2"
// 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 | Versions 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. |
-
.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.