C
C#2y ago
ero

Creating and using an incremental source generator

I wanna create an incremental source generator for my web API project, but I cannot for the life of me understand how to create a source generator at all. I'm even having a hard time understanding the documentation that does exist. Is there anyone that's able to help? I have a pretty clear idea of what I wanna do: I have a collection of HTTP endpoints in one class, which get turned into controllers, commands, and DTOs.
namespace Endpoints;

[GenerateHttpEndpoints] // better name ideas?
public partial class User
{
[HttpPost("/api/users")]
public async Task<ActionResult<Entities.User>> Create(
string name,
string email,
string password)
{
// ...

return Ok(createdUser);
}
}
namespace Endpoints;

[GenerateHttpEndpoints] // better name ideas?
public partial class User
{
[HttpPost("/api/users")]
public async Task<ActionResult<Entities.User>> Create(
string name,
string email,
string password)
{
// ...

return Ok(createdUser);
}
}
UserEndpoints.g.cs
file record CreateUserDto(
string Name,
string Email,
string Password);

// other things related to handling the command
file record CreateUserDto(
string Name,
string Email,
string Password);

// other things related to handling the command
30 Replies
Patrick
Patrick2y ago
what are you actually trying to achieve with GenerateHttpEndpoints
ero
eroOP2y ago
generate controllers, commands, command handlers, dtos
Patrick
Patrick2y ago
From what
ero
eroOP2y ago
what do you mean from what? it's all laid out in the post
Patrick
Patrick2y ago
You already have a controller there You can’t just generate something from nothing How would it know what the DTO is supposed to even be? Based on what?
ero
eroOP2y ago
from the method arguments...?
Patrick
Patrick2y ago
So who’s using the DTO then?
ero
eroOP2y ago
like
Patrick
Patrick2y ago
You’re going to need the DTO in the first place to post to an endpoint if you’re not getting these from query parameters
ero
eroOP2y ago
the Endpoints.User class is basically nothing. it's not used anywhere. it's not a controller. it only exists to generate the actual controller
Patrick
Patrick2y ago
What you’ve posted looks like a controller
ero
eroOP2y ago
and i'm saying it isn't one
Patrick
Patrick2y ago
So you’re going to write a class that looks like a controller, is supposed to be a controller but isn’t because you want to… source generate it
ero
eroOP2y ago
it's supposed to look like one so people reading the source have an idea of what's going on but i simply cannot be bothered writing full controllers and services and commands and command handlers and dtos for every single thing i implement
Patrick
Patrick2y ago
But what you have is 90% a controller…. I don’t get it If you don’t want to write any code, write a swagger doc and generate all controllers and types
ero
eroOP2y ago
look if you don't wanna help, you don't have to that's fine
Patrick
Patrick2y ago
i don't think you're understanding what you want to do is backward
Patrick
Patrick2y ago
this is 99% a controller which you say you don't want to write
ero
eroOP2y ago
it's not anything. you can't post to an endpoint like that, can you? it doesn't take a DTO after all
Patrick
Patrick2y ago
right... so just change it to take a DTO and then inherit ControllerBase and put [ApiController] on the class 99%
ero
eroOP2y ago
and then i need a command. and a command handler. and maybe a service if i feel like it like there's so much more that needs to be generated i don't know what you're talking about
Patrick
Patrick2y ago
i dont know what a command or command handler is i assume you mean mediatr
ero
eroOP2y ago
or something
Patrick
Patrick2y ago
that's a completely different topic and no you don't need that
ero
eroOP2y ago
i decided i want it
Patrick
Patrick2y ago
people building APIs only to have controllers => mediatr => handler => service end up building a lot of bloat your desire to generate code comes from over engineering a solution
ero
eroOP2y ago
this is meant to be a completely feature rich, public api, everything open source, with potentially thousands of daily users
Patrick
Patrick2y ago
ok where does mediatr come into play
ero
eroOP2y ago
you really make people wanna ask questions in this server you know
Patrick
Patrick2y ago
"potentially thousands of daily users" you haven't got 1 yet controller => service what's wrong with that?
Want results from more Discord servers?
Add your server