Giter Site home page Giter Site logo

sfmohassel / libc.eventbus Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 6.0 71 KB

An in-memory event bus without any dependency in C#

Home Page: https://sfmohassel.medium.com/event-aggregator-an-implementation-in-c-17fad5e6ed28

License: MIT License

C# 100.00%
net-standard csharp eventbus event-bus in-memory domain-driven-design domain-events domain-event

libc.eventbus's Introduction

An in-memory event bus without any dependency in C

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

Read More

You can checkout 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

libc.eventbus's People

Contributors

jeffreymb avatar sfmohassel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

libc.eventbus's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.