A simple commands & queries extension for the great MediatR library.
Startup.cs::ConfigureServices()
services.AddMediatR(new[] { typeof(WeatherForecastsQuery).Assembly });
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(DummyQueryBehavior<,>));
Startup.cs::Configure()
TODO
Controller
[ApiController]
[Route("weatherforecasts")]
public class WeatherForecastController : ControllerBase
{
...
[HttpGet]
public async Task<IEnumerable<WeatherForecastResponse>> Get()
{
return await this.mediator.Send(new WeatherForecastsQuery()).ConfigureAwait(false);
}
}
or without Controllers see Startup.cs
services.AddCommandEndpoints(); // ConfigureServices
app.UseEndpoints(endpoints => // Configure
{
...
endpoints.MapGet<UserFindAllQuery>(
"/users", "User");
endpoints.MapGet<UserFindByIdQuery>(
"/users/{userId}", "User");
...
}