DispatchEndpoints 1.0.6

dotnet add package DispatchEndpoints --version 1.0.6                
NuGet\Install-Package DispatchEndpoints -Version 1.0.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DispatchEndpoints" Version="1.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DispatchEndpoints --version 1.0.6                
#r "nuget: DispatchEndpoints, 1.0.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DispatchEndpoints as a Cake Addin
#addin nuget:?package=DispatchEndpoints&version=1.0.6

// Install DispatchEndpoints as a Cake Tool
#tool nuget:?package=DispatchEndpoints&version=1.0.6                

DispatchEndpoints

NuGet NuGet

DispatchEndpoints it's a library what provide source generator what generate controllers/actions based on your class where you use DispatchEndpoint attribute.

Installation

Install-Package DispatchEndpoints

Getting started

Register service

services.AddControllers().AddDispatchEndpoints()

Creating endpoint

Query endpoint
[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);
    }
}
Command endpoint
[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 */
    }
}
Request With Fluent Validaton
public partial record Query(string Id)
{
    public static void AddValidation(AbstractValidator<Query> v)
    {
        v.RuleFor(x => x.Id)
            .NotEmpty();
    }
}

Generated Source

Generated Controller

[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);
    }
}    

Generated Dispatcher

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();
        }
    }
}

Wiki

Dispatch Endpoint Attribute

[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
}

Passing arguments to Query/Command property

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)

Dependency injection

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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 404 7/3/2022
1.0.5 384 6/28/2022
1.0.4 377 6/28/2022
1.0.3 382 6/27/2022
1.0.2 375 6/27/2022
1.0.1 383 6/27/2022
1.0.0 384 6/24/2022