/EventBus

Simple event bus implementation.

Primary LanguageC#

EventBus

Simple event bus implementation.

Samples

Basic

Subscribe another service:

IEventBus bus; // injected

AnotherService service = new AnotherService();
bus.Subscribe(service);

Someone publish the event:

IEventBus bus; // injected 

bus.Publish(new EventA());

Another service handles the event:

public class AnotherService : IEventListener<EventA>
{
  public void OnListen(EventA args)
  {
    // do some magic
  }
}

Multiple Events

Service can handles multiple events:

public class SampleService : IEventListener<EventA>, IEventListener<EventB>
{
  public void OnListen(EventA args)
  {
    // do some magic 1
  }
  
  public void OnListen(EventB args)
  {
    // do some magic 2
  }
}

In this case need subscribe every listener:

IEventBus bus; // injected

SampleService service = new SampleService();
bus.Subscribe<EventA>(service);
bus.Subscribe<EventB>(service);

Hierarchical Events

In example, simple Command event:

public class BaseCommand
{
  private readonly IEventBus _bus;
  
  public BaseCommand(IEventBus bus) =>
    _bus = bus;
    
  public void DoSomething() =>
    _bus.Publish(this);
}

And another one:

public class AnotherCommand : BaseCommand
{
  public AnotherCommand(IEventBus bus) : base(bus) { }
}

Handlers can handle AnotherCommand:

public class Handler : IEventListener<AnotherCommand>
{
  public void OnListen(AnotherCommand command)
  {
    Console.WriteLine($"{nameof(Handler)} Listen {command}");
  }
}
IEventBus bus; // injected

AnotherCommand command = new AnotherCommand(bus);
Handler handler = new Handler();

bus.Subscribe(handler);
command.DoSomething(); // output: Handler Listen AnotherCommand