fullstackhero / dotnet-starter-kit

Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
https://fullstackhero.net/dotnet-webapi-boilerplate/
MIT License
5.3k stars 1.59k forks source link

[v2] added code generation #953

Closed wtmaxim closed 4 months ago

wtmaxim commented 5 months ago

use https://github.com/Drizin/CodegenCS

wtmaxim commented 5 months ago

Example

public static class GetTodoEndpoint
{
    internal static RouteHandlerBuilder MapGetTodoEndpoint(this IEndpointRouteBuilder endpoints)
    {
        return endpoints.MapGet("/{id:guid}", async (Guid id, ISender mediator, ICodeGenerationService codeGenerationService) =>
        {
            var response = await mediator.Send(new GetTodoRequest(id));

            // Utilisation du service de génération de code
            var template = new CodeGenTemplate(response);
            codeGenerationService.GenerateCode(template, "codegen.cs");

            return Results.Ok(response);
        })
        .WithName(nameof(GetTodoEndpoint))
        .WithSummary("gets todo item by id")
        .WithDescription("gets todo item by id")
        .Produces<GetTodoResponse>()
        .RequirePermission("Permissions.Todos.View")
        .MapToApiVersion(1);
    }

}``` 

``` public class CodeGenTemplate : ICodeTemplate
{
    private readonly GetTodoResponse _response;
    public CodeGenTemplate(GetTodoResponse response)
    {
        _response = response;
    }
    public void Generate(CodegenTextWriter writer)
    {
        writer.WriteLine("public class GetTodoResponse");
        writer.WriteLine("{");

        writer.WriteLine($"public Guid Id {{ get; set; }} = Guid.NewGuid();");
        writer.WriteLine($"public string Title {{ get; set; }} = \"{_response.Title}\";");
        writer.WriteLine($"public string Description {{ get; set; }} = \"{_response.Notes}\";");

        writer.WriteLine("}");
    }
}

Output

image