An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Sidio.Mediator
A simple implementation of the [mediator](https://en.wikipedia.org/wiki/Mediator_pattern) pattern in .NET.

[![build](https://github.com/marthijn/Sidio.Mediator/actions/workflows/build.yml/badge.svg)](https://github.com/marthijn/Sidio.Mediator/actions/workflows/build.yml)
[![Coverage Status](https://coveralls.io/repos/github/marthijn/Sidio.Mediator/badge.svg?branch=main)](https://coveralls.io/github/marthijn/Sidio.Mediator?branch=main)

## Core package (request abstractions)
[![NuGet Version](https://img.shields.io/nuget/v/Sidio.Mediator)](https://www.nuget.org/packages/Sidio.Mediator/)

## Request validation package
[![NuGet Version](https://img.shields.io/nuget/v/Sidio.Mediator.Validation)](https://www.nuget.org/packages/Sidio.Mediator.Validation/)

## Source generation for the Mediator service
[![NuGet Version](https://img.shields.io/nuget/v/Sidio.Mediator.SourceGenerator)](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`.