Core abstractions for event-driven communication in .NET applications, including event bus, event handlers, and integration events.
$ dotnet add package Mubai.EventBusEnglish | 简体中文
Core abstractions for an event-driven architecture in .NET: event base types, handler contracts, and a minimal IEventBus interface. Designed for modular monoliths and integration-style messaging without prescribing a specific transport.
IntegrationEvent, IEventBus, IIntegrationEventHandler<TEvent>.EventNameAttribute to decouple wire names from CLR type names.Mubai.EventBus.InMemory for in-process dispatch.dotnet add package Mubai.EventBus
Define an event:
using Mubai.EventBus.Events;
public record OrderCreated(Guid OrderId)
: IntegrationEvent(); // uses default Id/OccurredOn
Implement a handler:
using Mubai.EventBus.Abstractions;
public sealed class OrderCreatedHandler : IIntegrationEventHandler<OrderCreated>
{
public Task HandleAsync(OrderCreated @event, CancellationToken ct = default)
{
// business logic
return Task.CompletedTask;
}
}
Publish via an IEventBus implementation (e.g., Mubai.EventBus.InMemory):
await eventBus.PublishAsync(new OrderCreated(orderId), ct);
Use EventNameAttribute when you need a stable name independent of the CLR type:
[EventName("InventoryReserved")]
public record InventoryReserved(Guid OrderId) : IntegrationEvent();
IEventBus to send/receive events.Mubai.EventBus.InMemory.IntegrationEvent has a default constructor that sets a new Id and OccurredOn timestamp; you can still pass explicit values via the parameterized constructor.请参考仓库中的 README.zh-CN.md 获取中文说明。