Cayd.AspNetCore.Mediator
2.0.0
dotnet add package Cayd.AspNetCore.Mediator --version 2.0.0
NuGet\Install-Package Cayd.AspNetCore.Mediator -Version 2.0.0
<PackageReference Include="Cayd.AspNetCore.Mediator" Version="2.0.0" />
<PackageVersion Include="Cayd.AspNetCore.Mediator" Version="2.0.0" />
<PackageReference Include="Cayd.AspNetCore.Mediator" />
paket add Cayd.AspNetCore.Mediator --version 2.0.0
#r "nuget: Cayd.AspNetCore.Mediator, 2.0.0"
#:package Cayd.AspNetCore.Mediator@2.0.0
#addin nuget:?package=Cayd.AspNetCore.Mediator&version=2.0.0
#tool nuget:?package=Cayd.AspNetCore.Mediator&version=2.0.0
About
This is a mediator library that handles request-handler-response and request-stream flows for ASP.NET Core. It automatically matches requests with corresponding handlers and streams, and it supports interceptors for all requets and/or specific ones.
Quick Start
Request-Handler-Response
After installing the package, you can set up your own request-handler-response flow by using IAsyncRequest<> and IAsyncHandler<,> interfaces. After creating the classes, you need to register them for the dependency injection, so that you can use the SendAsync method of the IMediator interface.
- Creating Classes:
using Cayd.AspNetCore.Mediator.Abstractions;
public class MyRequest : IAsyncRequest<MyResponse>
{
// ... properties of the request
}
public class MyHandler : IAsyncHandler<MyRequest, MyResponse>
{
// Example dependency injection
private readonly ILogger<MyHandler> _logger;
// You can inject dependencies if needed
public MyHandler(ILogger<MyHandler> logger)
{
_logger = logger; // example
}
public async Task<MyResponse> HandleAsync(MyRequest request, CancellationToken cancellationToken)
{
// ... handle the request and return the corresponding response
}
}
public class MyResponse
{
// ... properties of the response
}
- Adding Mediator and Flows:
/**
* Minimal API - Program.cs
*/
using Cayd.AspNetCore.Mediator.DependencyInjection;
builder.Services.AddMediator(config =>
{
// Add flows from one assembly
config.AddFlowsFromAssembly(Assembly.GetAssembly(typeof(MyRequest)!));
// ... or from multiple assemblies
config.AddFlowsFromAssemblies(Assembly.GetAssembly(typeof(MyRequest))!,
Assembly.GetAssembly(typeof(MyClassInAnotherAssembly))!);
});
/**
* .NET 5 - Startup.cs
*/
using Cayd.AspNetCore.Mediator.DependencyInjection;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMediator(config =>
{
// Add flows from one assembly
config.AddFlowsFromAssembly(Assembly.GetAssembly(typeof(MyRequest)!));
// ... or from multiple assemblies
config.AddFlowsFromAssemblies(Assembly.GetAssembly(typeof(MyRequest))!,
Assembly.GetAssembly(typeof(MyClassInAnotherAssembly))!);
});
}
}
- Sending Request:
using Cayd.AspNetCore.Mediator;
[ApiController]
public class MyController : ControllerBase
{
private readonly IMediator _mediator;
public MyController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet("example")]
public async Task<IActionResult> Example()
{
var response = await _mediator.SendAsync(new MyRequest()
{
// ... properties of the request
});
// ... handle response
}
[HttpGet("example-with-cancellation")]
public async Task<IActionResult> ExampleCancellation(CancellationToken cancellationToken)
{
var response = await _mediator.SendAsync(new MyRequest()
{
// ... properties of the request
}, cancellationToken);
// ... handle response
}
}
Request-Stream
For request-stream flows, you need to use IAsyncStreamRequest<> and IAsyncStreamHandler<,> interfaces instead. After creating the required classes, you then need to register them for the dependency injection in the same way you do for request-handler-response flows. The usage is also the same as request-handler-response flows.
- Creating Classes:
using Cayd.AspNetCore.Mediator.Abstractions;
public class MyStreamRequest : IAsyncStreamRequest<MyStreamResponse>
{
// ... properties of the request
}
public class MyStreamHandler : IAsyncStreamHandler<MyStreamRequest, MyStreamResponse>
{
// Example dependency injection
private readonly ILogger<MyStreamHandler> _logger;
// You can inject dependencies if needed
public MyStreamHandler(ILogger<MyStreamHandler> logger)
{
_logger = logger; // example
}
public async IAsyncEnumerable<MyStreamResponse> HandleAsync(MyRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
{
// ... handle the request and return the corresponding response
}
}
public class MyStreamResponse
{
// ... properties of the response
}
NOTE: Do not forget to add the EnumeratorCancellation attribute to the cancellation token in the handler.
- Adding Mediator and Flows:
/**
* Minimal API - Program.cs
*/
using Cayd.AspNetCore.Mediator.DependencyInjection;
builder.Services.AddMediator(config =>
{
// Add flows from one assembly
config.AddFlowsFromAssembly(Assembly.GetAssembly(typeof(MyStreamRequest)!));
// ... or from multiple assemblies
config.AddFlowsFromAssemblies(Assembly.GetAssembly(typeof(MyStreamRequest))!,
Assembly.GetAssembly(typeof(MyClassInAnotherAssembly))!);
});
/**
* .NET 5 - Startup.cs
*/
using Cayd.AspNetCore.Mediator.DependencyInjection;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMediator(config =>
{
// Add flows from one assembly
config.AddFlowsFromAssembly(Assembly.GetAssembly(typeof(MyStreamRequest)!));
// ... or from multiple assemblies
config.AddFlowsFromAssemblies(Assembly.GetAssembly(typeof(MyStreamRequest))!,
Assembly.GetAssembly(typeof(MyClassInAnotherAssembly))!);
});
}
}
- Sending Request:
using Cayd.AspNetCore.Mediator;
[ApiController]
public class MyController : ControllerBase
{
private readonly IMediator _mediator;
public MyController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet("stream-example")]
public async IAsyncEnumerable<MyStreamResponse> StreamExample()
{
var request = new MyStreamRequest()
{
// ... properties of the request
};
await foreach (var item in _mediator.SendAsync(request))
{
yield return item;
}
}
[HttpGet("stream-example-with-cancellation")]
public async IAsyncEnumerable<IActionResult> StreamExampleCancellation([EnumeratorCancellation] CancellationToken cancellationToken)
{
var request = new MyStreamRequest()
{
// ... properties of the request
};
await foreach (var item in _mediator.SendAsync(request, cancellationToken))
{
yield return item;
}
}
}
NOTE: Do not forget to add the EnumeratorCancellation attribute to the cancellation token in the controller.
Customizations & Implementations
You can customize the default request-handler-response or request-stream flow by adding interceptors. For more details, you can check out the GitHub Wiki to learn about the customizations, as well as the implementation of the library in detail.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
- Request-stream flows are added
- Request-handler-response flows are updated
- GitHub Wiki pages are added