C
C#3y 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
Patrick3y ago
what are you actually trying to achieve with GenerateHttpEndpoints
ero
eroOP3y ago
generate controllers, commands, command handlers, dtos
Patrick
Patrick3y ago
From what
ero
eroOP3y ago
what do you mean from what? it's all laid out in the post
Patrick
Patrick3y 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
eroOP3y ago
from the method arguments...?
Patrick
Patrick3y ago
So who’s using the DTO then?
ero
eroOP3y ago
like
Patrick
Patrick3y 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
eroOP3y 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
Patrick3y ago
What you’ve posted looks like a controller
ero
eroOP3y ago
and i'm saying it isn't one
Patrick
Patrick3y 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
eroOP3y 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
Patrick3y 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
eroOP3y ago
look if you don't wanna help, you don't have to that's fine
Patrick
Patrick3y ago
i don't think you're understanding what you want to do is backward
Patrick
Patrick3y ago
this is 99% a controller which you say you don't want to write
ero
eroOP3y 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
Patrick3y ago
right... so just change it to take a DTO and then inherit ControllerBase and put [ApiController] on the class 99%
ero
eroOP3y 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
Patrick3y ago
i dont know what a command or command handler is i assume you mean mediatr
ero
eroOP3y ago
or something
Patrick
Patrick3y ago
that's a completely different topic and no you don't need that
ero
eroOP3y ago
i decided i want it
Patrick
Patrick3y 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
eroOP3y ago
this is meant to be a completely feature rich, public api, everything open source, with potentially thousands of daily users
Patrick
Patrick3y ago
ok where does mediatr come into play
ero
eroOP3y ago
you really make people wanna ask questions in this server you know
Patrick
Patrick3y 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