https://github.com/sfmohassel/libc.eventbus
An in-memory event bus without any dependency in C#
https://github.com/sfmohassel/libc.eventbus
csharp domain-driven-design domain-event domain-events event-bus eventbus in-memory net-standard
Last synced: 4 months ago
JSON representation
An in-memory event bus without any dependency in C#
- Host: GitHub
- URL: https://github.com/sfmohassel/libc.eventbus
- Owner: sfmohassel
- License: mit
- Created: 2020-02-04T12:33:03.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-03T08:49:55.000Z (over 1 year ago)
- Last Synced: 2024-12-03T09:35:26.805Z (over 1 year ago)
- Topics: csharp, domain-driven-design, domain-event, domain-events, event-bus, eventbus, in-memory, net-standard
- Language: C#
- Homepage: https://sfmohassel.medium.com/event-aggregator-an-implementation-in-c-17fad5e6ed28
- Size: 87.9 KB
- Stars: 10
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# An in-memory event aggregator without any dependency
## Why bother?
In Domain-Driven Design (DDD) pattern, there are times when we want to inform other parts of application about occurrence of an event. This is the primary goal of [DOMAIN EVENTS](https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/domain-events-design-implementation#:~:text=A%20domain%20event%20is%2C%20something,effects%20can%20be%20expressed%20explicitly.)
## Read More
You can checkout my article on EventAggregator [HERE](https://sfmohassel.medium.com/event-aggregator-an-implementation-in-c-17fad5e6ed28)
# Installation
The package is available on nuget.org:
```powershell
PM> Install-Package libc.eventbus
```
# Simple Usage
Let's say we have a simple message event that has a text:
```csharp
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:
```csharp
public class PrintMessageRaw : IEventHandler {
public Task Handle(SimpleMessage ev) {
// print message
Console.WriteLine($"Raw: {ev.Text}");
return Task.CompletedTask;
}
}
public class PrintMessagePretty : IEventHandler {
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`__.
## Put it together
_Read the comments please_
```csharp
public void Showcase_WithoutCatchAll() {
// 1- create an event bus
var bus = new DefaultEventBus();
// 2- subscribe to SimpleMessage event via PrintMessageRaw event handler
bus.Subscribe(new PrintMessageRaw());
// 3- subscribe to SimpleMessage event via PrintMessagePretty event handler
var x = new PrintMessagePretty();
bus.Subscribe(x);
// 4- remember subscribing to a message with the same handler instance, has no effect!
bus.Subscribe(x);
// 5- create the event
var message = new SimpleMessage("a simple message");
// 6- publish the event
bus.Publish(message);
}
```
The console will show:
```text
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:
```csharp
public class PrivateMessage : IEvent {
public string Secret { get; private set; }
public PrivateMessage(string secret) {
Secret = secret;
}
}
```
then an implementation of `ICatchAllEventHandler`:
```csharp
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_
```csharp
public void Showcase_WithCatchAll() {
// 1- create an event bus
var bus = new DefaultEventBus();
// 2- subscribe to SimpleMessage event via PrintMessageRaw event handler
bus.Subscribe(new PrintMessageRaw());
// 3- subscribe to SimpleMessage event via PrintMessagePretty event handler
bus.Subscribe(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:
```text
Raw: a simple message
Pretty: a simple message
Caught SimpleMessage: a simple message
```
To see the full showcase __[click here](./libc.eventbus.tests/ShowCase.cs)__
## Contributions are welcome