DispatchEndpoints it's a library what provide source generator what generate controllers/actions based on your class where you use DispatchEndpoint
attribute.
Install-Package DispatchEndpoints
Register service
services.AddControllers().AddDispatchEndpoints()
[DispatchEndpoint(
RequestMethod = HttpRequestMethods.GET,
ProducesResponseTypes = new[]
{
HttpStatusCodes.Ok,
HttpStatusCodes.BadRequest
}
)]
public static partial class GetAll
{
public partial record Query;
public record Customer(string Name);
public static async Task<IEnumerable<Customer>> Handler()
{
var customers = new List<Customer>();
return await Task.FromResult(customers);
}
}
[DispatchEndpoint(
RequestMethod = HttpRequestMethods.POST,
ProducesResponseTypes = new[]
{
HttpStatusCodes.Ok,
HttpStatusCodes.BadRequest
}
)]
public static partial class CreateCustomer
{
public partial record Command;
public static async Task Handler()
{
/* logic */
}
}
public partial record Query(string Id)
{
public static void AddValidation(AbstractValidator<Query> v)
{
v.RuleFor(x => x.Id)
.NotEmpty();
}
}
[Route("customers")]
public partial class CustomersController : ApiControllerBase
{
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IEnumerable<Customer>>> GetAll([FromQuery] GetAll.Query request)
{
var query = await Dispatcher.Query(request);
return Ok(query);
}
}
It's basically same for command/query
public partial class GetAll
{
public partial record Query : IRequest<IEnumerable<Customer>> { }
private class QueryHandlerCore : IRequestHandler<GetAll.Query, IEnumerable<Customer>>
{
public async Task<IEnumerable<Customer>> Handle(Query request, CancellationToken cancellationToken)
{
return await Handler();
}
}
}
[AttributeUsage(AttributeTargets.Class)]
public class DispatchEndpointAttribute : Attribute
{
public string? Controller { get; set; } // Set controller name, if you leave it empty source generator will get the directory name where endpoint is located by namespace and use it as controller name
public string? Route { get; set; } // Set route for endpoint
public HttpRequestMethods RequestMethod { get; set; } // Set http rquest method for method
public HttpStatusCodes[]? ProducesResponseTypes { get; set; } // Set produces response types status codes, first one is that what endpoint will return
public bool Auth { get; set; } // Set auth to true
public string? Policy { get; set; } // Set policy for auth
}
public partial record Command(string Name);
Then you need to pass that property as first parameter of your handler like this
public static async Task Handler(Command request)
DI is automatic here, you just need to pass Query/Command
as first parameter then you can pass as many you want interface parameters like this
public static async Task Handler(Command request, ICustomersRepository repo, INotificationService service)
Source generator will create constructor for you in generated file