https://github.com/litenova/LiteBus
LiteBus is an easy-to-use and ambitious in-process mediator providing the foundation to implement Command Query Separation (CQS). It is implemented with minimal reflection and instead utilizes covariance and contravariance to provide its core functionality.
https://github.com/litenova/LiteBus
cqrs cqs csharp ddd-patterns dotnet dotnet-core eventbus mediator mediator-pattern
Last synced: 12 months ago
JSON representation
LiteBus is an easy-to-use and ambitious in-process mediator providing the foundation to implement Command Query Separation (CQS). It is implemented with minimal reflection and instead utilizes covariance and contravariance to provide its core functionality.
- Host: GitHub
- URL: https://github.com/litenova/LiteBus
- Owner: litenova
- License: mit
- Created: 2020-12-04T00:14:35.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-06-09T13:42:12.000Z (12 months ago)
- Last Synced: 2025-06-10T07:16:44.516Z (12 months ago)
- Topics: cqrs, cqs, csharp, ddd-patterns, dotnet, dotnet-core, eventbus, mediator, mediator-pattern
- Language: C#
- Homepage:
- Size: 802 KB
- Stars: 105
- Watchers: 6
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE
Awesome Lists containing this project
- fucking-awesome-dotnet - LiteBus - An easy-to-use and ambitious in-process mediator providing the foundation to implement Command Query Separation (CQS) (Event aggregator and messenger)
- awesome-dotnet - LiteBus - An easy-to-use and ambitious in-process mediator providing the foundation to implement Command Query Separation (CQS) (Event aggregator and messenger)
README
LiteBus
A lightweight, flexible in-process mediator for implementing Command Query Separation (CQS)
For detailed documentation and examples, please visit the Wiki.
## Key Features
- **Built in .NET 8**
- **Zero external dependencies** - Completely standalone with no third-party dependencies
- **Reduced reflection usage** - Optimized for performance with minimal reflection
- **DDD-friendly design** - Support for plain domain events without library dependencies, keeping your domain model clean
- **Comprehensive messaging types**:
- `ICommand` / `ICommand` - For state-changing operations
- `IQuery` - For data retrieval operations
- `IStreamQuery` - For streaming large datasets via `IAsyncEnumerable`
- `IEvent` - For notifications and event-driven architecture
- Support for POCO objects as messages without library interfaces
- **Rich handler ecosystem**:
- Pre-handlers for validation and pre-processing
- Post-handlers for notifications and side effects
- Error handlers for centralized exception management
- Support for generic handlers and messages
- Handler ordering control
- Contextual handler selection via tags and filters
- **Advanced features**:
- Covariant type handling for polymorphic dispatch
- Execution context for cross-cutting concerns
- Aborting execution flow when needed
- Microsoft Dependency Injection integration
## Quick Example
```csharp
// Define the command result
public record CreateProductCommandResult(Guid Id);
// Define a command with a result
public record CreateProductCommand(string Title) : ICommand;
// Implement a command validator
public class CreateProductCommandValidator : ICommandValidator
{
public Task ValidateAsync(CreateProductCommand command, CancellationToken cancellationToken = default)
{
if (string.IsNullOrWhiteSpace(command.Title))
throw new ValidationException("Product title cannot be empty");
return Task.CompletedTask;
}
}
// Implement a command handler
public class CreateProductCommandHandler : ICommandHandler
{
private readonly IProductRepository _repository;
public CreateProductCommandHandler(IProductRepository repository)
{
_repository = repository;
}
public async Task HandleAsync(CreateProductCommand command, CancellationToken cancellationToken = default)
{
var product = new Product(Guid.NewGuid(), command.Title);
await _repository.SaveAsync(product, cancellationToken);
return new CreateProductCommandResult(product.Id);
}
}
// Configure in ASP.NET Core
services.AddLiteBus(liteBus =>
{
liteBus.AddCommandModule(module =>
{
module.RegisterFromAssembly(typeof(CreateProductCommand).Assembly);
});
});
// Use in a controller or service
public class ProductsController : ControllerBase
{
private readonly ICommandMediator _commandMediator;
public ProductsController(ICommandMediator commandMediator)
{
_commandMediator = commandMediator;
}
[HttpPost]
public async Task> CreateProduct(CreateProductCommand command)
{
var result = await _commandMediator.SendAsync(command);
return Ok(result);
}
}
```
## Documentation
For comprehensive documentation, including detailed explanations, advanced features, and best practices, please visit the [Wiki](https://github.com/litenova/LiteBus/wiki).
## Installation
LiteBus is available as NuGet packages:
```
dotnet add package LiteBus
dotnet add package LiteBus.Extensions.MicrosoftDependencyInjection
```
Or specific modules:
```
dotnet add package LiteBus.Commands
dotnet add package LiteBus.Queries
dotnet add package LiteBus.Events
```
## License
LiteBus is licensed under the MIT License. See the LICENSE file for details.