https://github.com/marthijn/sidio.mediator
An implementation mediator pattern in .NET
https://github.com/marthijn/sidio.mediator
cqrs cqrs-pattern cqs mediator mediator-pattern
Last synced: 3 months ago
JSON representation
An implementation mediator pattern in .NET
- Host: GitHub
- URL: https://github.com/marthijn/sidio.mediator
- Owner: marthijn
- License: mit
- Created: 2025-05-20T11:19:51.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-10-20T03:51:20.000Z (3 months ago)
- Last Synced: 2025-10-20T07:31:36.019Z (3 months ago)
- Topics: cqrs, cqrs-pattern, cqs, mediator, mediator-pattern
- Language: C#
- Homepage:
- Size: 164 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sidio.Mediator
A simple implementation of the [mediator](https://en.wikipedia.org/wiki/Mediator_pattern) pattern in .NET.
[](https://github.com/marthijn/Sidio.Mediator/actions/workflows/build.yml)
[](https://coveralls.io/github/marthijn/Sidio.Mediator?branch=main)
## Core package (request abstractions)
[](https://www.nuget.org/packages/Sidio.Mediator/)
## Request validation package
[](https://www.nuget.org/packages/Sidio.Mediator.Validation/)
## Source generation for the Mediator service
[](https://www.nuget.org/packages/Sidio.Mediator.SourceGenerator/)
# Usage
## Requests and requests handlers
```csharp
// Define a request and request handler
public class MyRequest : IRequest
{
public string Name { get; init; }
}
public class MyRequestHandler : IRequestHandler
{
public Task> HandleAsync(MyRequest request, CancellationToken cancellationToken = default)
{
var result = Result.Success($"Hello {request.Name}");
// Or: Result.Failure("error code", "error message");
return Task.FromResult(result);
}
}
// Provide an arbitrary type to register all request handlers in the assembly of the type:
services.AddMediatorRequestHandlers(typeof(MyRequest));
// Get the request handler from the service provider
var requestHander = serviceProvider.GetRequiredService>();
var result = await requestHander.HandleAsync(new MyRequest { Name = "World" });
// or use dependency injection
public class MyClass
{
public MyClass(IRequestHandler requestHandler)
{
}
}
```
### Http requests
```csharp
// Define a request and request handler
public class MyHttpRequest : IHttpRequest
{
public string Name { get; init; }
}
public class MyHttpRequestHandler : IHttpRequestHandler
{
public Task> HandleAsync(MyRequest request, CancellationToken cancellationToken = default)
{
var result = HttpResult.Ok($"Hello {request.Name}");
// Or for example: HttpResult.Unauthorized();
return Task.FromResult(result);
}
}
```
## Request validation
Request validation uses [FluentValidation](https://docs.fluentvalidation.net/).
```csharp
// Define a validator
public class MyRequestValidator : AbstractValidator
{
public MyRequestValidator()
{
RuleFor(x => x.Name).NotEmpty();
}
}
// Provide an arbitrary type to register all validators in the assembly of the type:
services
.AddMediatorRequestHandlers(typeof(MyRequest))
.AddMediatorValidation(typeof(MyRequest));
```
## Source generators (v2.0+)
In version 2.0 and later, [Sidio.Mediator.SourceGenerator](https://www.nuget.org/packages/Sidio.Mediator.SourceGenerator/) includes source generators that create an `IMediator` service implementation at
compile time. This service works both with requests and request validation.
The `IMediator` implementation contains a method for each request. For example, a request named `MyRequest`:
```csharp
public class MyRequest : IRequest;
```
The generated `IMediator` will have a method:
```csharp
Task> MyRequestAsync(MyRequest request, CancellationToken cancellationToken = default);
```
### Setup
- Add package reference to `Sidio.Mediator.SourceGenerator` in the project that contain the `IRequest` or `IHttpRequest` implementations.
- Register the `IMediator` service in your `Startup.cs` or `Program.cs`:
```csharp
services
.AddMediatorRequestHandlers(typeof(MyRequest)) // register the request handlers
.AddMediatorValidation(typeof(MyRequest)) // register the request validators
.AddMediatorService(); // register the IMediator service
```
### Limitations
- Requests should have a unique name across the project which implements the source generator.
- Requests should not be nested in other classes.
- Requests should always implement `IRequest`, `IRequest` or `IHttpRequest`. Inheritance of base/abstract requests is not supported. Inheritance of request handlers should work.
- Types used in requests that are part of the parent namespace of that request should be included in global usings, e.g. in the csproj file:
```xml
```
- The IMediator service will be located in namespace `Sidio.Mediator`.