# Intent.AspNetCore.Controllers.Dispatch.Mediator

Wires ASP.NET Core controllers to dispatch Commands and Queries through Mediator's `ISender`, so each controller action hands its request to the handler generated by `Intent.Application.Mediator`.


## What This Module Does

The module has no templates of its own. It replaces the registration of the `ControllerTemplate` from `Intent.AspNetCore.Controllers` for CQRS elements and extends the result:

- `ImplicitControllerTemplateRegistration` groups every Command and Query that has HTTP settings by its parent folder in the Services designer and creates one controller for each group. Elements directly in the package root go into a `Default` controller.
- `MediatorControllerDispatchExtension` adds a `Mediator.ISender _sender` constructor parameter (with a null guard) to each of those controllers and emits the dispatch call in every action. It also registers the Mediator command and query contracts as type sources for the controller.

## Generated Dispatch

The request is sent with `ISender.Send`. The response type is inferred from the Command or Query contract, so there is no type argument:

```csharp
var result = await _sender.Send(command, cancellationToken);   // returns a value
await _sender.Send(command, cancellationToken);                // void command
```

When the action has no body payload, the request is built from the route and query parameters, with constructor syntax if the Command or Query has a parameterised constructor and object-initialiser syntax otherwise:

```csharp
new GetProductByIdQuery(id: id)
new GetProductByIdQuery { Id = id }
```

The extension also generates, before the dispatch:

- parameter-to-payload assignments, so a route or query value populates a matching property of a body payload when that property was left at its default;
- a `BadRequest()` guard when a route value and the same property on the body payload disagree;
- the stream handling for file upload operations.

The action then ends with the return statement `Intent.AspNetCore.Controllers` generates for the operation.

## One Dispatch Module Per Application

`Intent.AspNetCore.Controllers.Dispatch.Mediator`, `Intent.AspNetCore.Controllers.Dispatch.Wolverine` and `Intent.AspNetCore.Controllers.Dispatch.MediatR` each register the controller template for the same Commands and Queries. Install only one of them in an application.

## Prerequisites

- `Intent.AspNetCore.Controllers` - owns the `ControllerTemplate` this module extends.
- `Intent.Application.Mediator` - generates the Commands, Queries and handlers, and brings in the `Mediator` library that provides `ISender`.
- `Intent.Modelers.Services.CQRS` - the Command and Query designer elements.

Installing `Intent.AspNetCore.Controllers` into an application that already has `Intent.Application.Mediator` installs this module (the core declares it as interoperability).

## Related Modules

- [Intent.AspNetCore.Controllers](https://docs.intentarchitect.com/articles/modules-dotnet/intent-aspnetcore-controllers/intent-aspnetcore-controllers.html) - owns the controller template this module extends.
- [Intent.AspNetCore.Controllers.Dispatch.Wolverine](https://docs.intentarchitect.com/articles/modules-dotnet/intent-aspnetcore-controllers-dispatch-wolverine/intent-aspnetcore-controllers-dispatch-wolverine.html) - the Wolverine equivalent.
- `Intent.AspNetCore.Controllers.Dispatch.MediatR` - the MediatR equivalent (no published page).
- `Intent.FastEndpoints.Dispatch.Mediator` - equivalent dispatch wiring for FastEndpoints (no published page yet).
- `Intent.Application.Mediator` - the core module of this family (no published page yet).
